Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit. Add new function to convert string json to map #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ func (tpl *Template) FromJson(i interface{}) (interface{}, error) {
return r, nil
}

func (tpl *Template)FromJsonStringToMap(jsonStr string) (map[string]interface{}, error) {
var r map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &r)
if err != nil {
return nil, err
}

return r, nil
}

// split is a version of strings.Split that can be piped
func (tpl *Template) Split(sep, s string) ([]string, error) {
s = strings.TrimSpace(s)
Expand Down Expand Up @@ -1934,6 +1944,7 @@ func (tpl *Template) setTemplateFuncs(funcs map[string]any) {
funcs["toJSON"] = tpl.ToJson // deprecated
funcs["toJson"] = tpl.ToJson
funcs["fromJson"] = tpl.FromJson
funcs["fromJsonStringToMap"] = tpl.FromJsonStringToMap
funcs["split"] = tpl.Split
funcs["join"] = tpl.Join
funcs["isEmpty"] = tpl.IsEmpty
Expand Down