Skip to content

Commit e471fb5

Browse files
committed
Move coax to string to the GetStringAtKey function
1 parent d560991 commit e471fb5

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

restapi/common.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ func GetStringAtKey(data map[string]interface{}, path string, debug bool) (strin
3030

3131
/* JSON supports strings, numbers, objects and arrays. Allow a string OR number here */
3232
t := fmt.Sprintf("%T", res)
33-
if t != "string" && t != "float64" {
33+
if t == "string" {
34+
return res.(string), nil
35+
} else if t == "float64" {
36+
return strconv.FormatFloat(res.(float64), 'f', -1, 64), nil
37+
} else {
3438
return "", fmt.Errorf("Object at path '%s' is not a JSON string or number (float64). The go fmt package says it is '%T'", path, res)
3539
}
36-
37-
/* Since it might be a number, coax it to a string with fmt */
38-
return fmt.Sprintf("%v", res), nil
3940
}
4041

4142
/* Handy helper that will dig through a map and find something
@@ -123,11 +124,7 @@ func GetObjectAtKey(data map[string]interface{}, path string, debug bool) (inter
123124
log.Printf("common.go:GetObjectAtKey: %s - exists (%v)", part, hash[part])
124125
}
125126

126-
if _, ok := hash[part].(float64); ok {
127-
return strconv.FormatFloat(hash[part].(float64), 'f', -1, 64), nil
128-
} else {
129-
return hash[part], nil
130-
}
127+
return hash[part], nil
131128
}
132129

133130
/* Handy helper to just dump the keys of a map into a slice */

0 commit comments

Comments
 (0)