Skip to content

Commit

Permalink
👔 up(env, str): update the env var parse and strutil.Valid()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 1, 2023
1 parent 6aaea78 commit 5193849
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 0 additions & 1 deletion internal/comfunc/comfunc.go
Expand Up @@ -66,7 +66,6 @@ func ParseEnvVar(val string, getFn func(string) string) (newVal string) {
if len(ss) == 2 {
name, def = strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
} else {
def = eVar // use raw value
name = strings.TrimSpace(ss[0])
}

Expand Down
10 changes: 10 additions & 0 deletions strutil/strutil.go
Expand Up @@ -18,6 +18,16 @@ func OrElse(s, newVal string) string {
return newVal
}

// Valid return first not empty element.
func Valid(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}

// Replaces replace multi strings
//
// pairs: {old1: new1, old2: new2, ...}
Expand Down
10 changes: 10 additions & 0 deletions strutil/strutil_test.go
Expand Up @@ -13,6 +13,16 @@ func TestSimilarity(t *testing.T) {
is.True(ok)
}

func TestValid(t *testing.T) {
is := assert.New(t)

is.Eq("ab", strutil.Valid("ab", ""))
is.Eq("ab", strutil.Valid("ab", "cd"))
is.Eq("cd", strutil.Valid("", "cd"))
is.Eq("cd", strutil.OrElse("", "cd"))
is.Eq("ab", strutil.OrElse("ab", "cd"))
}

func TestRenderTemplate(t *testing.T) {
tpl := "hi, My name is {{ .name | upFirst }}, age is {{ .age }}"
assert.Eq(t, "hi, My name is Inhere, age is 2000", strutil.RenderTemplate(tpl, map[string]any{
Expand Down

0 comments on commit 5193849

Please sign in to comment.