Skip to content

Commit c902800

Browse files
committed
Use a single parseJson function
1 parent 2fb6a6a commit c902800

File tree

2 files changed

+54
-73
lines changed

2 files changed

+54
-73
lines changed

template.go

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,8 @@ 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{}
219+
func unmarshalJson(input string) (interface{}, error) {
220+
var v interface{}
229221
if err := json.Unmarshal([]byte(input), &v); err != nil {
230222
return nil, err
231223
}
@@ -301,34 +293,33 @@ func trimSuffix(suffix, s string) string {
301293

302294
func newTemplate(name string) *template.Template {
303295
tmpl := template.New(name).Funcs(template.FuncMap{
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,
296+
"closest": arrayClosest,
297+
"coalesce": coalesce,
298+
"contains": contains,
299+
"dict": dict,
300+
"dir": dirList,
301+
"exists": exists,
302+
"first": arrayFirst,
303+
"groupBy": groupBy,
304+
"groupByKeys": groupByKeys,
305+
"groupByMulti": groupByMulti,
306+
"hasPrefix": hasPrefix,
307+
"hasSuffix": hasSuffix,
308+
"json": marshalJson,
309+
"intersect": intersect,
310+
"keys": keys,
311+
"last": arrayLast,
312+
"replace": strings.Replace,
313+
"parseJson": unmarshalJson,
314+
"sha1": hashSha1,
315+
"split": strings.Split,
316+
"trimPrefix": trimPrefix,
317+
"trimSuffix": trimSuffix,
318+
"where": where,
319+
"whereExist": whereExist,
320+
"whereNotExist": whereNotExist,
321+
"whereAny": whereAny,
322+
"whereAll": whereAll,
332323
})
333324
return tmpl
334325
}

template_test.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"reflect"
78
"testing"
89
"text/template"
@@ -518,41 +519,30 @@ func TestJson(t *testing.T) {
518519
}
519520
}
520521

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)
522+
func TestParseJson(t *testing.T) {
523+
tests := []struct {
524+
tmpl string
525+
input string
526+
expected string
527+
}{
528+
{`{{index (parseJson .) "enabled"}}`, `{"enabled":true}`, "true"},
529+
{`{{index (parseJson . | first) "enabled"}}`, `[{"enabled":true}]`, "true"},
530+
}
531+
532+
for n, test := range tests {
533+
tmplName := fmt.Sprintf("parseJson-test-%d", n)
534+
tmpl := template.Must(newTemplate(tmplName).Parse(test.tmpl))
535+
536+
var b bytes.Buffer
537+
err := tmpl.ExecuteTemplate(&b, tmplName, test.input)
538+
if err != nil {
539+
t.Fatalf("Error executing template: %v", err)
540+
}
541+
542+
got := b.String()
543+
if test.expected != got {
544+
t.Fatalf("Incorrect output found; expected %s, got %s", test.expected, got)
545+
}
556546
}
557547
}
558548

0 commit comments

Comments
 (0)