Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 2efa1fd

Browse files
author
David Chung
authored
0.3.0 patch (#385)
Signed-off-by: David Chung <[email protected]>
1 parent 4b179b1 commit 2efa1fd

File tree

4 files changed

+71
-16
lines changed

4 files changed

+71
-16
lines changed

pkg/template/funcs.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SplitLines(o interface{}) ([]string, error) {
3333

3434
// FromJSON decode the input JSON encoded as string or byte slice into a map.
3535
func FromJSON(o interface{}) (interface{}, error) {
36-
ret := map[string]interface{}{}
36+
var ret interface{}
3737
switch o := o.(type) {
3838
case string:
3939
err := json.Unmarshal([]byte(o), &ret)
@@ -136,11 +136,37 @@ func (t *Template) DefaultFuncs() map[string]interface{} {
136136
return make([]struct{}, c)
137137
},
138138

139-
"var": func(name, doc string, v ...interface{}) interface{} {
139+
"def": func(name string, args ...interface{}) (string, error) {
140+
if _, has := t.defaults[name]; has {
141+
// not sure if this is good, but should complain loudly
142+
return "", fmt.Errorf("already defined: %v", name)
143+
}
144+
var doc string
145+
var value interface{}
146+
switch len(args) {
147+
case 1:
148+
// just value, no docs
149+
value = args[0]
150+
case 2:
151+
// docs and value
152+
doc = fmt.Sprintf("%v", args[0])
153+
value = args[1]
154+
}
155+
t.defaults[name] = defaultValue{
156+
Name: name,
157+
Value: value,
158+
Doc: doc,
159+
}
160+
return "", nil
161+
},
162+
163+
"ref": func(name string) interface{} {
140164
if found, has := t.binds[name]; has {
141165
return found
166+
} else if v, has := t.defaults[name]; has {
167+
return v.Value
142168
}
143-
return v // default
169+
return nil
144170
},
145171

146172
"global": func(name string, v interface{}) interface{} {

pkg/template/funcs_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func TestQueryObjectEncodeDecode(t *testing.T) {
5757
require.NoError(t, err)
5858

5959
require.Equal(t, decoded, decoded2)
60+
61+
decoded, err = FromJSON("[]")
62+
require.NoError(t, err)
63+
require.Equal(t, []interface{}{}, decoded)
64+
65+
decoded, err = FromJSON(`{"foo":"bar"}`)
66+
require.NoError(t, err)
67+
require.Equal(t, map[string]interface{}{"foo": "bar"}, decoded)
6068
}
6169

6270
func TestQueryObject(t *testing.T) {

pkg/template/template.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,23 @@ type Options struct {
4545
SocketDir string
4646
}
4747

48+
type defaultValue struct {
49+
Name string
50+
Value interface{}
51+
Doc string
52+
}
53+
4854
// Template is the templating engine
4955
type Template struct {
5056
options Options
5157

52-
url string
53-
body []byte
54-
parsed *template.Template
55-
funcs map[string]interface{}
56-
binds map[string]interface{}
57-
lock sync.Mutex
58+
url string
59+
body []byte
60+
parsed *template.Template
61+
funcs map[string]interface{}
62+
binds map[string]interface{}
63+
defaults map[string]defaultValue
64+
lock sync.Mutex
5865
}
5966

6067
// NewTemplate fetches the content at the url and returns a template. If the string begins
@@ -84,11 +91,12 @@ func NewTemplateFromBytes(buff []byte, contextURL string, opt Options) (*Templat
8491
}
8592

8693
return &Template{
87-
options: opt,
88-
url: contextURL,
89-
body: buff,
90-
funcs: map[string]interface{}{},
91-
binds: map[string]interface{}{},
94+
options: opt,
95+
url: contextURL,
96+
body: buff,
97+
funcs: map[string]interface{}{},
98+
binds: map[string]interface{}{},
99+
defaults: map[string]defaultValue{},
92100
}, nil
93101
}
94102

pkg/template/template_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ func TestVarAndGlobal(t *testing.T) {
3434
str := `{{ q "locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}" . | global "washington-cities"}}
3535
3636
{{/* The query above is exported and referenced somewhere else */}}
37+
{{ from_json "[\"SF\",\"LA\"]" | def "california-cities" "Default value for California cities" }}
38+
{{ from_json "{\"SF\":\"94109\",\"LA\":\"90210\"}" | def "zip-codes" "Default value for zip codes" }}
39+
3740
{
3841
"test" : "hello",
3942
"val" : true,
40-
"result" : {{var "washington-cities" "A json with washington cities" | to_json}}
43+
"result" : {{ref "washington-cities" | to_json}},
44+
"california" : {{ ref "california-cities" | to_json}},
45+
"sf_zip" : {{ ref "zip-codes" | q "SF" | to_json }}
4146
}
4247
`
4348

@@ -59,12 +64,20 @@ func TestVarAndGlobal(t *testing.T) {
5964
expected := `
6065
6166
67+
68+
69+
6270
{
6371
"test" : "hello",
6472
"val" : true,
6573
"result" : {
6674
"WashingtonCities": "Bellevue, Olympia, Seattle"
67-
}
75+
},
76+
"california" : [
77+
"SF",
78+
"LA"
79+
],
80+
"sf_zip" : "94109"
6881
}
6982
`
7083
require.Equal(t, expected, view)

0 commit comments

Comments
 (0)