mirror of https://github.com/go-gitea/gitea
Remove legacy `unknwon/com` package (#19298)
Follows: #19284 * The `CopyDir` is only used inside test code * Rewrite `ToSnakeCase` with more test cases * The `RedisCacher` only put strings into cache, here we use internal `toStr` to replace the legacy `ToStr` * The `UniqueQueue` can use string as ID directly, no need to call `ToStr`pull/19300/head^2
parent
4c5cb1e2f2
commit
4f27c28947
@ -0,0 +1,103 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package unittest |
||||
|
||||
import ( |
||||
"errors" |
||||
"io" |
||||
"os" |
||||
"path" |
||||
"strings" |
||||
|
||||
"code.gitea.io/gitea/modules/util" |
||||
) |
||||
|
||||
// Copy copies file from source to target path.
|
||||
func Copy(src, dest string) error { |
||||
// Gather file information to set back later.
|
||||
si, err := os.Lstat(src) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Handle symbolic link.
|
||||
if si.Mode()&os.ModeSymlink != 0 { |
||||
target, err := os.Readlink(src) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
// NOTE: os.Chmod and os.Chtimes don't recognize symbolic link,
|
||||
// which will lead "no such file or directory" error.
|
||||
return os.Symlink(target, dest) |
||||
} |
||||
|
||||
sr, err := os.Open(src) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer sr.Close() |
||||
|
||||
dw, err := os.Create(dest) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer dw.Close() |
||||
|
||||
if _, err = io.Copy(dw, sr); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Set back file information.
|
||||
if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil { |
||||
return err |
||||
} |
||||
return os.Chmod(dest, si.Mode()) |
||||
} |
||||
|
||||
// CopyDir copy files recursively from source to target directory.
|
||||
//
|
||||
// The filter accepts a function that process the path info.
|
||||
// and should return true for need to filter.
|
||||
//
|
||||
// It returns error when error occurs in underlying functions.
|
||||
func CopyDir(srcPath, destPath string, filters ...func(filePath string) bool) error { |
||||
// Check if target directory exists.
|
||||
if _, err := os.Stat(destPath); !errors.Is(err, os.ErrNotExist) { |
||||
return errors.New("file or directory already exists: " + destPath) |
||||
} |
||||
|
||||
err := os.MkdirAll(destPath, os.ModePerm) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Gather directory info.
|
||||
infos, err := util.StatDir(srcPath, true) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
var filter func(filePath string) bool |
||||
if len(filters) > 0 { |
||||
filter = filters[0] |
||||
} |
||||
|
||||
for _, info := range infos { |
||||
if filter != nil && filter(info) { |
||||
continue |
||||
} |
||||
|
||||
curPath := path.Join(destPath, info) |
||||
if strings.HasSuffix(info, "/") { |
||||
err = os.MkdirAll(curPath, os.ModePerm) |
||||
} else { |
||||
err = Copy(path.Join(srcPath, info), curPath) |
||||
} |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,88 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package util |
||||
|
||||
import "github.com/yuin/goldmark/util" |
||||
|
||||
func isSnakeCaseUpper(c byte) bool { |
||||
return 'A' <= c && c <= 'Z' |
||||
} |
||||
|
||||
func isSnakeCaseLowerOrNumber(c byte) bool { |
||||
return 'a' <= c && c <= 'z' || '0' <= c && c <= '9' |
||||
} |
||||
|
||||
// ToSnakeCase convert the input string to snake_case format.
|
||||
//
|
||||
// Some samples.
|
||||
// "FirstName" => "first_name"
|
||||
// "HTTPServer" => "http_server"
|
||||
// "NoHTTPS" => "no_https"
|
||||
// "GO_PATH" => "go_path"
|
||||
// "GO PATH" => "go_path" // space is converted to underscore.
|
||||
// "GO-PATH" => "go_path" // hyphen is converted to underscore.
|
||||
//
|
||||
func ToSnakeCase(input string) string { |
||||
if len(input) == 0 { |
||||
return "" |
||||
} |
||||
|
||||
var res []byte |
||||
if len(input) == 1 { |
||||
c := input[0] |
||||
if isSnakeCaseUpper(c) { |
||||
res = []byte{c + 'a' - 'A'} |
||||
} else if isSnakeCaseLowerOrNumber(c) { |
||||
res = []byte{c} |
||||
} else { |
||||
res = []byte{'_'} |
||||
} |
||||
} else { |
||||
res = make([]byte, 0, len(input)*4/3) |
||||
pos := 0 |
||||
needSep := false |
||||
for pos < len(input) { |
||||
c := input[pos] |
||||
if c >= 0x80 { |
||||
res = append(res, c) |
||||
pos++ |
||||
continue |
||||
} |
||||
isUpper := isSnakeCaseUpper(c) |
||||
if isUpper || isSnakeCaseLowerOrNumber(c) { |
||||
end := pos + 1 |
||||
if isUpper { |
||||
// skip the following upper letters
|
||||
for end < len(input) && isSnakeCaseUpper(input[end]) { |
||||
end++ |
||||
} |
||||
if end-pos > 1 && end < len(input) && isSnakeCaseLowerOrNumber(input[end]) { |
||||
end-- |
||||
} |
||||
} |
||||
// skip the following lower or number letters
|
||||
for end < len(input) && (isSnakeCaseLowerOrNumber(input[end]) || input[end] >= 0x80) { |
||||
end++ |
||||
} |
||||
if needSep { |
||||
res = append(res, '_') |
||||
} |
||||
res = append(res, input[pos:end]...) |
||||
pos = end |
||||
needSep = true |
||||
} else { |
||||
res = append(res, '_') |
||||
pos++ |
||||
needSep = false |
||||
} |
||||
} |
||||
for i := 0; i < len(res); i++ { |
||||
if isSnakeCaseUpper(res[i]) { |
||||
res[i] += 'a' - 'A' |
||||
} |
||||
} |
||||
} |
||||
return util.BytesToReadOnlyString(res) |
||||
} |
@ -0,0 +1,48 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package util |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestToSnakeCase(t *testing.T) { |
||||
cases := map[string]string{ |
||||
// all old cases from the legacy package
|
||||
"HTTPServer": "http_server", |
||||
"_camelCase": "_camel_case", |
||||
"NoHTTPS": "no_https", |
||||
"Wi_thF": "wi_th_f", |
||||
"_AnotherTES_TCaseP": "_another_tes_t_case_p", |
||||
"ALL": "all", |
||||
"_HELLO_WORLD_": "_hello_world_", |
||||
"HELLO_WORLD": "hello_world", |
||||
"HELLO____WORLD": "hello____world", |
||||
"TW": "tw", |
||||
"_C": "_c", |
||||
|
||||
" sentence case ": "__sentence_case__", |
||||
" Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case", |
||||
|
||||
// new cases
|
||||
" ": "_", |
||||
"A": "a", |
||||
"A0": "a0", |
||||
"a0": "a0", |
||||
"Aa0": "aa0", |
||||
"啊": "啊", |
||||
"A啊": "a啊", |
||||
"Aa啊b": "aa啊b", |
||||
"A啊B": "a啊_b", |
||||
"Aa啊B": "aa啊_b", |
||||
"TheCase2": "the_case2", |
||||
"ObjIDs": "obj_i_ds", // the strange database column name which already exists
|
||||
} |
||||
for input, expected := range cases { |
||||
assert.Equal(t, expected, ToSnakeCase(input)) |
||||
} |
||||
} |
Loading…
Reference in new issue