Skip to content

Commit 2fb6a6a

Browse files
committed
Add parseJsonArray and parseJsonObject
1 parent ad7451f commit 2fb6a6a

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

template.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ func marshalJson(input interface{}) (string, error) {
216216
return strings.TrimSuffix(buf.String(), "\n"), nil
217217
}
218218

219+
func unmarshalJsonArray(input string) ([]interface{}, error) {
220+
var v []interface{}
221+
if err := json.Unmarshal([]byte(input), &v); err != nil {
222+
return nil, err
223+
}
224+
return v, nil
225+
}
226+
227+
func unmarshalJsonObject(input string) (interface{}, error) {
228+
var v map[string]interface{}
229+
if err := json.Unmarshal([]byte(input), &v); err != nil {
230+
return nil, err
231+
}
232+
return v, nil
233+
}
234+
219235
// arrayFirst returns first item in the array or nil if the
220236
// input is nil or empty
221237
func arrayFirst(input interface{}) interface{} {
@@ -285,32 +301,34 @@ func trimSuffix(suffix, s string) string {
285301

286302
func newTemplate(name string) *template.Template {
287303
tmpl := template.New(name).Funcs(template.FuncMap{
288-
"closest": arrayClosest,
289-
"coalesce": coalesce,
290-
"contains": contains,
291-
"dict": dict,
292-
"dir": dirList,
293-
"exists": exists,
294-
"first": arrayFirst,
295-
"groupBy": groupBy,
296-
"groupByKeys": groupByKeys,
297-
"groupByMulti": groupByMulti,
298-
"hasPrefix": hasPrefix,
299-
"hasSuffix": hasSuffix,
300-
"json": marshalJson,
301-
"intersect": intersect,
302-
"keys": keys,
303-
"last": arrayLast,
304-
"replace": strings.Replace,
305-
"sha1": hashSha1,
306-
"split": strings.Split,
307-
"trimPrefix": trimPrefix,
308-
"trimSuffix": trimSuffix,
309-
"where": where,
310-
"whereExist": whereExist,
311-
"whereNotExist": whereNotExist,
312-
"whereAny": whereAny,
313-
"whereAll": whereAll,
304+
"closest": arrayClosest,
305+
"coalesce": coalesce,
306+
"contains": contains,
307+
"dict": dict,
308+
"dir": dirList,
309+
"exists": exists,
310+
"first": arrayFirst,
311+
"groupBy": groupBy,
312+
"groupByKeys": groupByKeys,
313+
"groupByMulti": groupByMulti,
314+
"hasPrefix": hasPrefix,
315+
"hasSuffix": hasSuffix,
316+
"json": marshalJson,
317+
"intersect": intersect,
318+
"keys": keys,
319+
"last": arrayLast,
320+
"replace": strings.Replace,
321+
"parseJsonArray": unmarshalJsonArray,
322+
"parseJsonObject": unmarshalJsonObject,
323+
"sha1": hashSha1,
324+
"split": strings.Split,
325+
"trimPrefix": trimPrefix,
326+
"trimSuffix": trimSuffix,
327+
"where": where,
328+
"whereExist": whereExist,
329+
"whereNotExist": whereNotExist,
330+
"whereAny": whereAny,
331+
"whereAll": whereAll,
314332
})
315333
return tmpl
316334
}

template_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,44 @@ func TestJson(t *testing.T) {
518518
}
519519
}
520520

521+
func TestParseJsonArray(t *testing.T) {
522+
const expected = "true"
523+
const testJson = `[{"enabled":true}]`
524+
525+
const text = `{{index (parseJsonArray . | first) "enabled"}}`
526+
tmpl := template.Must(newTemplate("parseJsonArray-test").Parse(text))
527+
528+
var b bytes.Buffer
529+
err := tmpl.ExecuteTemplate(&b, "parseJsonArray-test", testJson)
530+
if err != nil {
531+
t.Fatalf("Error executing template: %v", err)
532+
}
533+
534+
got := b.String()
535+
if expected != got {
536+
t.Fatalf("Incorrect output found; expected %s, got %s", expected, got)
537+
}
538+
}
539+
540+
func TestParseJsonObject(t *testing.T) {
541+
const expected = "true"
542+
const testJson = `{"enabled":true}`
543+
544+
const text = `{{index (parseJsonObject .) "enabled"}}`
545+
tmpl := template.Must(newTemplate("parseJsonObject-test").Parse(text))
546+
547+
var b bytes.Buffer
548+
err := tmpl.ExecuteTemplate(&b, "parseJsonObject-test", testJson)
549+
if err != nil {
550+
t.Fatalf("Error executing template: %v", err)
551+
}
552+
553+
got := b.String()
554+
if expected != got {
555+
t.Fatalf("Incorrect output found; expected %s, got %s", expected, got)
556+
}
557+
}
558+
521559
func TestArrayClosestExact(t *testing.T) {
522560
if arrayClosest([]string{"foo.bar.com", "bar.com"}, "foo.bar.com") != "foo.bar.com" {
523561
t.Fatal("Expected foo.bar.com")

0 commit comments

Comments
 (0)