Skip to content

Commit

Permalink
Add set, unset, and hasKey.
Browse files Browse the repository at this point in the history
  • Loading branch information
technosophos committed Dec 14, 2016
1 parent 9d1414e commit 69011c0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ parse, it returns the time unaltered. See `time.ParseDuration` for info on durat
follows: []byte are converted, fmt.Stringers will have String() called.
errors will have Error() called. All others will be passed through
fmt.Sprtinf("%v").
- set: Takes a dict, a key, and a value, and sets that key/value pair in
the dict. `set $dict $key $value`. For convenience, it returns the dict,
even though the dict was modified in place.
- unset: Takes a dict and a key, and deletes that key/value pair from the
dict. `unset $dict $key`. This returns the dict for convenience.
- hasKey: Takes a dict and a key, and returns boolean true if the key is in
the dict.

```
{{$t := tuple 1 "a" "foo"}}
Expand Down
29 changes: 27 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ Data Structures:
follows: []byte are converted, fmt.Stringers will have String() called.
errors will have Error() called. All others will be passed through
fmt.Sprtinf("%v").
- set: Takes a dict, a key, and a value, and sets that key/value pair in
the dict. `set $dict $key $value`. For convenience, it returns the dict,
even though the dict was modified in place.
- unset: Takes a dict and a key, and deletes that key/value pair from the
dict. `unset $dict $key`. This returns the dict for convenience.
- hasKey: Takes a dict and a key, and returns boolean true if the key is in
the dict.
Math Functions:
Expand Down Expand Up @@ -386,8 +393,11 @@ var genericMap = map[string]interface{}{
"b32dec": base32decode,

// Data Structures:
"tuple": tuple,
"dict": dict,
"tuple": tuple,
"dict": dict,
"set": set,
"unset": unset,
"hasKey": hasKey,

// Crypto:
"genPrivateKey": generatePrivateKey,
Expand Down Expand Up @@ -650,6 +660,21 @@ func tuple(v ...interface{}) []interface{} {
return v
}

func set(d map[string]interface{}, key string, value interface{}) map[string]interface{} {
d[key] = value
return d
}

func unset(d map[string]interface{}, key string) map[string]interface{} {
delete(d, key)
return d
}

func hasKey(d map[string]interface{}, key string) bool {
_, ok := d[key]
return ok
}

func dict(v ...interface{}) map[string]interface{} {
dict := map[string]interface{}{}
lenv := len(v)
Expand Down
47 changes: 47 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ func TestEmpty(t *testing.T) {
if err := runt(tpl, "1"); err != nil {
t.Error(err)
}

dict := map[string]interface{}{"top": map[string]interface{}{}}
tpl = `{{if empty .top.NoSuchThing}}1{{else}}0{{end}}`
if err := runtv(tpl, "1", dict); err != nil {
t.Error(err)
}
tpl = `{{if empty .bottom.NoSuchThing}}1{{else}}0{{end}}`
if err := runtv(tpl, "1", dict); err != nil {
t.Error(err)
}
}

func TestSplit(t *testing.T) {
Expand Down Expand Up @@ -523,6 +533,43 @@ func TestDict(t *testing.T) {
}
}

func TestUnset(t *testing.T) {
tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
{{- $_ := unset $d "two" -}}
{{- range $k, $v := $d}}{{$k}}{{$v}}{{- end -}}
`

expect := "one1"
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}
func TestHasKey(t *testing.T) {
tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
{{- if hasKey $d "one" -}}1{{- end -}}
`

expect := "1"
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}

func TestSet(t *testing.T) {
tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
{{- $_ := set $d "two" 2 -}}
{{- $_ := set $d "three" 3 -}}
{{- if hasKey $d "one" -}}{{$d.one}}{{- end -}}
{{- if hasKey $d "two" -}}{{$d.two}}{{- end -}}
{{- if hasKey $d "three" -}}{{$d.three}}{{- end -}}
`

expect := "123"
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}

func TestUntil(t *testing.T) {
tests := map[string]string{
`{{range $i, $e := until 5}}{{$i}}{{$e}}{{end}}`: "0011223344",
Expand Down

0 comments on commit 69011c0

Please sign in to comment.