-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
73 lines (64 loc) · 1.82 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package structmapper
import (
"reflect"
"strconv"
"strings"
)
// GetJSONTag takes a field name and json struct tag value and returns the effective name, omitempty setting
// and an indicator if the field value should be omitted (if the tag value is "-").
//
// name, omitEmpty, omit := GetJSONTag(fieldName, fieldTag)
//
// Examples:
//
// GetJSONTag("Cake", "tuna,omitempty") -> "tuna",true
// GetJSONTag("Cake", "") -> "Cake",false
func GetJSONTag(fieldName, tag string) (string, bool, bool) {
if tag == "-" {
return "", false, true
}
if tag == "-," { // per the docs, this is how you get a json field name of "-"
return "-", false, false
}
f := strings.Split(tag, ",")
omitEmpty := len(f) > 1 && f[1] == "omitempty"
if len(f) < 1 || f[0] == "" {
return fieldName, omitEmpty, false
}
return f[0], omitEmpty, false
}
// getJSONTagFromField gathers tag and name from a field and passes it to getJSONTag.
// If an "sm" tag is present, it is used in favor of the "json" field tag.
func getJSONTagFromField(f reflect.StructField) (string, bool, bool) {
t := f.Tag.Get("sm")
if t == "" {
t = f.Tag.Get("json")
}
return GetJSONTag(f.Name, t)
}
// StringToBool returns true if the value is true looking
// if numeric and greater than zero
// if string and starts with t or y
func StringToBool(s string) bool {
// optimise for normal json case
if s == "true" {
return true
}
if s == "false" {
return false
}
// now try to guess from the input
if s == "" {
return false
}
if i, err := strconv.Atoi(s); err == nil {
return i > 0
}
s = strings.ToLower(s)
return len(s) > 0 && s[0] == 't' || s[0] == 'y'
}
// stringToStringSlice hopes the input is a comma separated string and returns it as a slice.
func stringToStringSlice(s string) []string {
s = strings.Replace(s, ", ", ",", -1)
return strings.Split(s, ",")
}