Skip to content

Commit

Permalink
🐛 fix(env, str): fix the envutil test error and update some str util
Browse files Browse the repository at this point in the history
- re-gen readme docs
  • Loading branch information
inhere committed Feb 1, 2023
1 parent 5193849 commit 2e7aa63
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -178,6 +178,8 @@ ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"}
func NewBuffer() *Buffer
// source at byteutil/byteutil.go
func FirstLine(bs []byte) []byte
func StrOrErr(bs []byte, err error) (string, error)
func SafeString(bs []byte, err error) string
// source at byteutil/bytex.go
func Md5(src any) []byte
// source at byteutil/encoder.go
Expand Down Expand Up @@ -1163,6 +1165,7 @@ func SplitInlineComment(val string) (string, string)
func FirstLine(output string) string
// source at strutil/strutil.go
func OrElse(s, newVal string) string
func Valid(ss ...string) string
func Replaces(str string, pairs map[string]string) string
func PrettyJSON(v any) (string, error)
func RenderTemplate(input string, data any, fns template.FuncMap, isFile ...bool) string
Expand Down
3 changes: 3 additions & 0 deletions README.zh-CN.md
Expand Up @@ -177,6 +177,8 @@ ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"}
func NewBuffer() *Buffer
// source at byteutil/byteutil.go
func FirstLine(bs []byte) []byte
func StrOrErr(bs []byte, err error) (string, error)
func SafeString(bs []byte, err error) string
// source at byteutil/bytex.go
func Md5(src any) []byte
// source at byteutil/encoder.go
Expand Down Expand Up @@ -1164,6 +1166,7 @@ func SplitInlineComment(val string) (string, string)
func FirstLine(output string) string
// source at strutil/strutil.go
func OrElse(s, newVal string) string
func Valid(ss ...string) string
func Replaces(str string, pairs map[string]string) string
func PrettyJSON(v any) (string, error)
func RenderTemplate(input string, data any, fns template.FuncMap, isFile ...bool) string
Expand Down
11 changes: 6 additions & 5 deletions envutil/envutil_test.go
Expand Up @@ -13,7 +13,7 @@ func TestParseEnvValue(t *testing.T) {
eKey, eVal, rVal, nVal string
}{
{"EnvKey", "EnvKey val", "${EnvKey}", "EnvKey val"},
{"EnvKey", "", "${EnvKey}", "${EnvKey}"},
{"EnvKey", "", "${EnvKey}", ""},
{"EnvKey0", "EnvKey0 val", "${ EnvKey0 }", "EnvKey0 val"},
{"EnvKey1", "EnvKey1 val", "${EnvKey1|defValue}", "EnvKey1 val"},
{"EnvKey1", "", "${EnvKey1|defValue}", "defValue"},
Expand All @@ -38,11 +38,12 @@ func TestParseEnvValue(t *testing.T) {
}

// test multi ENV key
rVal := "${FirstEnv}/${ SecondEnv }"
rVal := "${FirstEnv}/${ SecondEnv | def_val}"
is.Eq("", Getenv("FirstEnv"))
is.Eq("", Getenv("SecondEnv"))
is.Eq(rVal, ParseEnvValue(rVal))
is.Eq(rVal, VarParse(rVal))
is.Eq("/def_val", ParseEnvValue(rVal))
is.Eq("/def_val", VarParse(rVal))
is.Eq("/", VarReplace(rVal)) // use os.ExpandEnv()

testutil.MockEnvValues(map[string]string{
"FirstEnv": "abc",
Expand All @@ -59,6 +60,6 @@ func TestParseEnvValue(t *testing.T) {
}, func() {
is.Eq("abc", Getenv("FirstEnv"))
is.Eq("", Getenv("SecondEnv"))
is.Eq("abc/${ SecondEnv }", ParseEnvValue(rVal))
is.Eq("abc/def_val", ParseEnvValue(rVal))
})
}
69 changes: 37 additions & 32 deletions strutil/value.go
Expand Up @@ -15,79 +15,84 @@ func (s *Value) Set(val string) error {
}

// IsEmpty check
func (s *Value) IsEmpty() bool {
return string(*s) == ""
func (s Value) IsEmpty() bool {
return string(s) == ""
}

// IsBlank check
func (s Value) IsBlank() bool {
return strings.TrimSpace(string(s)) == ""
}

// IsStartWith prefix
func (s *Value) IsStartWith(sub string) bool {
return strings.HasPrefix(string(*s), sub)
func (s Value) IsStartWith(sub string) bool {
return strings.HasPrefix(string(s), sub)
}

// HasPrefix prefix
func (s *Value) HasPrefix(sub string) bool {
return strings.HasPrefix(string(*s), sub)
func (s Value) HasPrefix(sub string) bool {
return strings.HasPrefix(string(s), sub)
}

// IsEndWith suffix
func (s *Value) IsEndWith(sub string) bool {
return strings.HasSuffix(string(*s), sub)
func (s Value) IsEndWith(sub string) bool {
return strings.HasSuffix(string(s), sub)
}

// HasSuffix suffix
func (s *Value) HasSuffix(sub string) bool {
return strings.HasSuffix(string(*s), sub)
func (s Value) HasSuffix(sub string) bool {
return strings.HasSuffix(string(s), sub)
}

// Bytes string to bytes
func (s *Value) Bytes() []byte {
return []byte(*s)
func (s Value) Bytes() []byte {
return []byte(s)
}

// Val string
func (s *Value) Val() string {
return string(*s)
func (s Value) Val() string {
return string(s)
}

// Int convert
func (s *Value) Int() int {
return QuietInt(string(*s))
func (s Value) Int() int {
return QuietInt(string(s))
}

// Int64 convert
func (s *Value) Int64() int64 {
return QuietInt64(string(*s))
func (s Value) Int64() int64 {
return QuietInt64(string(s))
}

// Bool convert
func (s *Value) Bool() bool {
return QuietBool(string(*s))
func (s Value) Bool() bool {
return QuietBool(string(s))
}

// Value string
func (s *Value) String() string {
return string(*s)
func (s Value) String() string {
return string(s)
}

// OrElse string
func (s *Value) OrElse(or string) string {
if *s != "" {
return string(*s)
func (s Value) OrElse(or string) string {
if s != "" {
return string(s)
}
return or
}

// Split string
func (s *Value) Split(sep string) []string {
return strings.Split(string(*s), sep)
func (s Value) Split(sep string) []string {
return strings.Split(string(s), sep)
}

// SplitN string
func (s *Value) SplitN(sep string, n int) []string {
return strings.SplitN(string(*s), sep, n)
func (s Value) SplitN(sep string, n int) []string {
return strings.SplitN(string(s), sep, n)
}

// TrimSpace string and return new
func (s *Value) TrimSpace() Value {
return Value(strings.TrimSpace(string(*s)))
// WithTrimSpace string and return new
func (s Value) WithTrimSpace() Value {
return Value(strings.TrimSpace(string(s)))
}
5 changes: 5 additions & 0 deletions strutil/value_test.go
Expand Up @@ -13,12 +13,17 @@ func TestValue_usage(t *testing.T) {
assert.True(t, s.HasPrefix("abc"))
assert.True(t, s.IsEndWith("123"))
assert.True(t, s.HasSuffix("123"))
assert.False(t, s.IsEmpty())
assert.False(t, s.IsBlank())

assert.Eq(t, "abc-123", s.Val())
assert.Eq(t, "abc-123", s.String())
assert.Eq(t, "abc-123", s.OrElse("def"))

s1 := strutil.StrVal("abc-123")
assert.NotEmpty(t, s1.Val())
assert.Len(t, s1.Split("-"), 2)
assert.Len(t, s1.SplitN("-", 2), 2)

assert.Eq(t, "abc", strutil.Value(" abc ").WithTrimSpace().Val())
}

0 comments on commit 2e7aa63

Please sign in to comment.