forked from ryankurte/go-structparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
129 lines (117 loc) · 2.97 KB
/
parse.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package structparse
import (
"reflect"
)
// StringParser interface is called on any string elements
type StringParser interface {
ParseString(in string) interface{}
}
// IntParser called on integer elements
type IntParser interface {
ParseInt(in int64) interface{}
}
// FloatParser called on floating point elements
type FloatParser interface {
ParseFloat(in float64) interface{}
}
// Parsers is a container for all possible parser interfaces
type Parsers struct {
StringParser StringParser
IntParser IntParser
FloatParser FloatParser
}
// Strings reflects over a structure and calls Parse when strings are located
func Strings(parser StringParser, obj interface{}) {
parsers := Parsers{
StringParser: parser,
}
parseRecursive(parsers, reflect.ValueOf(obj))
}
// Parse an object using the provided parser
// The parser must implement one of more ObjectParser interfaces
func Parse(parser, obj interface{}) {
parsers := Parsers{}
if p, ok := parser.(StringParser); ok {
parsers.StringParser = p
}
if p, ok := parser.(IntParser); ok {
parsers.IntParser = p
}
if p, ok := parser.(FloatParser); ok {
parsers.FloatParser = p
}
parseRecursive(parsers, reflect.ValueOf(obj))
}
// Internal recursive parsing function
func parseRecursive(parsers Parsers, val reflect.Value) reflect.Value {
switch val.Kind() {
case reflect.Ptr:
o := val.Elem()
if o.IsValid() {
res := parseRecursive(parsers, o)
if res != reflect.ValueOf(nil) {
return res.Addr()
}
}
case reflect.Interface:
res := parseRecursive(parsers, val.Elem())
if res != reflect.ValueOf(nil) {
return res
}
case reflect.Struct:
dst := val
if !val.CanSet() {
// this case is typically for a struct in map
dst = reflect.New(val.Type()).Elem()
}
for i := 0; i < val.NumField(); i++ {
if !dst.Field(i).CanSet() {
// this case is typically for unexported field, ignore it
continue
}
res := parseRecursive(parsers, val.Field(i))
if res != reflect.ValueOf(nil) {
dst.Field(i).Set(res)
}
}
return dst
case reflect.Slice:
for i := 0; i < val.Len(); i++ {
res := parseRecursive(parsers, val.Index(i))
if res != reflect.ValueOf(nil) {
val.Index(i).Set(res)
}
}
return val
case reflect.Map:
for _, k := range val.MapKeys() {
mapVal := val.MapIndex(k)
res := parseRecursive(parsers, mapVal)
if res != reflect.ValueOf(nil) {
val.SetMapIndex(k, res)
}
}
return val
case reflect.String:
if parsers.StringParser != nil && val.Type().AssignableTo(reflect.TypeOf("")) {
value := parsers.StringParser.ParseString(val.String())
return reflect.ValueOf(value)
}
return val
case reflect.Int:
if parsers.IntParser != nil {
value := parsers.IntParser.ParseInt(val.Int())
return reflect.ValueOf(value)
}
return val
case reflect.Float64:
if parsers.FloatParser != nil {
value := parsers.FloatParser.ParseFloat(val.Float())
return reflect.ValueOf(value)
}
return val
default:
return val
}
return reflect.ValueOf(nil)
}