Skip to content

Commit

Permalink
💥 break: strutil - remove deprecated func: PrettyJSON, RenderText, Re…
Browse files Browse the repository at this point in the history
…nderTemplate
  • Loading branch information
inhere committed Dec 7, 2023
1 parent a1e842e commit e06088b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 76 deletions.
54 changes: 3 additions & 51 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package strutil

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -92,66 +90,20 @@ func NewReplacer(pairs map[string]string) *strings.Replacer {
return strings.NewReplacer(ss...)
}

// PrettyJSON get pretty Json string
// Deprecated: please use fmtutil.PrettyJSON() or jsonutil.Pretty() instead it
func PrettyJSON(v any) (string, error) {
out, err := json.MarshalIndent(v, "", " ")
return string(out), err
}

var builtInFuncs = template.FuncMap{

Check failure on line 93 in strutil/strutil.go

View workflow job for this annotation

GitHub Actions / staticcheck

[staticcheck] strutil/strutil.go#L93 <U1000>(https://staticcheck.io/docs/checks#U1000)

var builtInFuncs is unused
Raw output
{"source":{"name":"staticcheck","url":"https://staticcheck.io"},"message":"var builtInFuncs is unused","code":{"value":"U1000","url":"https://staticcheck.io/docs/checks#U1000"},"location":{"path":"/home/runner/work/goutil/goutil/strutil/strutil.go","range":{"start":{"line":93,"column":5}}},"severity":"ERROR"}
// don't escape content
"raw": func(s string) string {
return s
},
"trim": func(s string) string {
return strings.TrimSpace(s)
},
"trim": strings.TrimSpace,
// join strings
"join": func(ss []string, sep string) string {
return strings.Join(ss, sep)
},
// lower first char
"lcFirst": func(s string) string {
return LowerFirst(s)
},
"lcFirst": LowerFirst,
// upper first char
"upFirst": func(s string) string {
return UpperFirst(s)
},
}

// RenderTemplate quickly render text template.
//
// Deprecated: please use textutil.RenderTpl() instead it
func RenderTemplate(input string, data any, fns template.FuncMap, isFile ...bool) string {
return RenderText(input, data, fns, isFile...)
}

// RenderText quickly render text template
//
// Deprecated: please use textutil.RenderTpl() instead it
func RenderText(input string, data any, fns template.FuncMap, isFile ...bool) string {
t := template.New("simple-text")
t.Funcs(builtInFuncs)

// add custom template functions
if len(fns) > 0 {
t.Funcs(fns)
}

if len(isFile) > 0 && isFile[0] {
template.Must(t.ParseFiles(input))
} else {
template.Must(t.Parse(input))
}

// use buffer receive rendered content
buf := new(bytes.Buffer)
if err := t.Execute(buf, data); err != nil {
panic(err)
}
return buf.String()
"upFirst": UpperFirst,
}

// WrapTag for given string.
Expand Down
25 changes: 0 additions & 25 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ func TestValid(t *testing.T) {
is.Empty(strutil.OrHandle("", strings.TrimSpace))
}

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{
"name": "inhere",
"age": 2000,
}, nil))
}

func TestReplaces(t *testing.T) {
assert.Eq(t, "tom age is 20", strutil.Replaces(
"{name} age is {age}",
Expand Down Expand Up @@ -90,20 +82,3 @@ func TestSubstrCount(t *testing.T) {
assert.Err(t, err)
assert.Eq(t, 0, res)
}

func TestPrettyJSON(t *testing.T) {
tests := []any{
map[string]int{"a": 1},
struct {
A int `json:"a"`
}{1},
}
want := `{
"a": 1
}`
for _, sample := range tests {
got, err := strutil.PrettyJSON(sample)
assert.NoErr(t, err)
assert.Eq(t, want, got)
}
}

0 comments on commit e06088b

Please sign in to comment.