Skip to content

Commit

Permalink
Add fmt template func and clean up some template funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
albertrdixon committed Mar 13, 2015
1 parent ddb695e commit 044bc45
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions generator/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func newFuncMap(f *file) map[string]interface{} {
"to_json": marshalJSON,
"from_json": UnmarshalJSON,
"from_json_array": UnmarshalJSONArray,
"fmt": fmt.Sprintf,
"first": arrayFirst,
"last": arrayLast,
"file_exists": fileExists,
Expand All @@ -41,16 +42,14 @@ func newFuncMap(f *file) map[string]interface{} {
}
}

func UnmarshalJSON(data string) (map[string]interface{}, error) {
var ret map[string]interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
func UnmarshalJSON(data string) (j map[string]interface{}, e error) {
e = json.Unmarshal([]byte(data), &j)
return
}

func UnmarshalJSONArray(data string) ([]interface{}, error) {
var ret []interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
func UnmarshalJSONArray(data string) (ja []interface{}, e error) {
e = json.Unmarshal([]byte(data), &ja)
return
}

func fileExists(path string) (bool, error) {
Expand All @@ -64,11 +63,9 @@ func fileExists(path string) (bool, error) {
return false, err
}

func hasKey(item map[string]string, key string) bool {
if _, ok := item[key]; ok {
return true
}
return false
func hasKey(item map[string]string, key string) (ok bool) {
_, ok = item[key]
return
}

func defaultValue(arg interface{}, def interface{}) interface{} {
Expand Down

0 comments on commit 044bc45

Please sign in to comment.