diff options
author | 2018-02-04 15:45:07 -0800 | |
---|---|---|
committer | 2018-02-04 15:45:07 -0800 | |
commit | 3884a33b3623ee5166f8254a0919e65be9bfb49b (patch) | |
tree | 9155305707f4d834df03391fd4fc0e74ee998eb3 /template/dict.go | |
parent | b5b1930599190e2d7a6613db8779d9bbb970a1f7 (diff) |
Move template functions outside engine (refactoring)
Diffstat (limited to 'template/dict.go')
-rw-r--r-- | template/dict.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/template/dict.go b/template/dict.go new file mode 100644 index 0000000..3056bcd --- /dev/null +++ b/template/dict.go @@ -0,0 +1,22 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package template + +import "fmt" + +func dict(values ...interface{}) (map[string]interface{}, error) { + if len(values)%2 != 0 { + return nil, fmt.Errorf("Dict expects an even number of arguments") + } + dict := make(map[string]interface{}, len(values)/2) + for i := 0; i < len(values); i += 2 { + key, ok := values[i].(string) + if !ok { + return nil, fmt.Errorf("Dict keys must be strings") + } + dict[key] = values[i+1] + } + return dict, nil +} |