feat: support env tag in config (#2577)
* feat: support env tag in config * chore: add more tests * chore: add more tests, add stringx.Join * fix: test fail * chore: remove print code * chore: rename variable
This commit is contained in:
@@ -69,6 +69,33 @@ func HasEmpty(args ...string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Join joins any number of elements into a single string, separating them with given sep.
|
||||
// Empty elements are ignored. However, if the argument list is empty or all its elements are empty,
|
||||
// Join returns an empty string.
|
||||
func Join(sep byte, elem ...string) string {
|
||||
var size int
|
||||
for _, e := range elem {
|
||||
size += len(e)
|
||||
}
|
||||
if size == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
buf := make([]byte, 0, size+len(elem)-1)
|
||||
for _, e := range elem {
|
||||
if len(e) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(buf) > 0 {
|
||||
buf = append(buf, sep)
|
||||
}
|
||||
buf = append(buf, e...)
|
||||
}
|
||||
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
// NotEmpty checks if all strings are not empty in args.
|
||||
func NotEmpty(args ...string) bool {
|
||||
return !HasEmpty(args...)
|
||||
|
||||
@@ -147,6 +147,42 @@ func TestFirstN(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
name: "all blanks",
|
||||
input: []string{"", ""},
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
name: "two values",
|
||||
input: []string{"012", "abc"},
|
||||
expect: "012.abc",
|
||||
},
|
||||
{
|
||||
name: "last blank",
|
||||
input: []string{"abc", ""},
|
||||
expect: "abc",
|
||||
},
|
||||
{
|
||||
name: "first blank",
|
||||
input: []string{"", "abc"},
|
||||
expect: "abc",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assert.Equal(t, test.expect, Join('.', test.input...))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
cases := []struct {
|
||||
input []string
|
||||
|
||||
Reference in New Issue
Block a user