-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
accessor.go
199 lines (175 loc) · 4.78 KB
/
accessor.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package googp
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
)
// accessor is an interface for writing the value of ogp to variables.
type accessor interface {
Set(key string, val string) error
}
// valueAccessor is an accessor for writing the value of ogp to single variable.
type valueAccessor struct {
value reflect.Value
didSet bool
}
// arrayAccessor is an accessor for writing the values of ogp to an array or a slice.
type arrayAccessor struct {
tag *tag
value reflect.Value
idx int
current accessor
}
// structAccessor is an accessor for writing the values of ogp to a struct.
type structAccessor struct {
value *reflect.Value
fields map[string]*field
}
type field struct {
structField *reflect.StructField
tag *tag
value *reflect.Value
accessor accessor
}
func newAccessor(tag *tag, v reflect.Value) accessor {
iv := reflect.Indirect(v)
switch iv.Kind() {
case reflect.Array, reflect.Slice:
return &arrayAccessor{tag: tag, value: iv}
case reflect.Struct:
if iv.CanAddr() {
if iv.Addr().Type().Implements(reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()) {
return newValueAccessor(v)
}
}
t := iv.Type()
fields := make(map[string]*field)
for i := 0; i < t.NumField(); i++ {
structField := t.Field(i)
fieldValue := iv.Field(i)
if !fieldValue.CanSet() {
continue
}
tag := newTag(structField)
field := &field{
structField: &structField,
tag: newTag(structField),
value: &fieldValue,
}
for _, name := range tag.names {
if _, ok := fields[name]; !ok {
fields[name] = field
}
}
}
if len(fields) == 0 {
return newValueAccessor(v)
}
return &structAccessor{value: &v, fields: fields}
default:
return newValueAccessor(v)
}
}
func newValueAccessor(v reflect.Value) *valueAccessor {
return &valueAccessor{value: v}
}
func (f *valueAccessor) Set(key string, val string) error {
// NOTE: The first tag (from top to bottom) is given preference during conflicts.
if f.didSet {
return nil
}
if !f.value.IsValid() {
return fmt.Errorf("invalid reflect.Value")
}
ty := f.value.Type()
v := f.value
if f.value.Kind() == reflect.Ptr {
if f.value.IsNil() && f.value.CanSet() {
v = reflect.New(f.value.Type().Elem())
f.value.Set(v)
v = v.Elem()
} else {
v = reflect.Indirect(v)
}
}
if !v.CanSet() {
return fmt.Errorf("Cannot set to value")
}
switch v.Kind() {
case reflect.String:
v.Set(reflect.ValueOf(val).Convert(v.Type()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(val, 10, 64)
if err != nil || reflect.Zero(v.Type()).OverflowInt(i) {
return convertErr(key, val, ty)
}
v.Set(reflect.ValueOf(i).Convert(v.Type()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
u, err := strconv.ParseUint(val, 10, 64)
if err != nil || reflect.Zero(v.Type()).OverflowUint(u) {
return convertErr(key, val, ty)
}
v.Set(reflect.ValueOf(u).Convert(v.Type()))
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(val, 64)
if err != nil || reflect.Zero(v.Type()).OverflowFloat(n) {
return convertErr(key, val, ty)
}
v.Set(reflect.ValueOf(n).Convert(v.Type()))
default:
unmarshaler, ok := v.Addr().Interface().(encoding.TextUnmarshaler)
if !ok {
return unsupportedErr(key, ty)
}
if err := unmarshaler.UnmarshalText([]byte(val)); err != nil {
return convertErr(key, val, ty)
}
}
f.didSet = true
return nil
}
func (f *arrayAccessor) Set(key string, val string) error {
if f.current != nil && f.tag != nil && !f.tag.isContainsName(key) {
return f.current.Set(key, val)
}
if f.current != nil {
f.idx += 1
}
if f.idx >= f.value.Len() {
if f.value.Kind() == reflect.Slice {
if !f.value.CanSet() {
return fmt.Errorf("Cannot set to value")
}
v := reflect.New(f.value.Type().Elem()).Elem()
f.value.Set(reflect.Append(f.value, v))
} else {
return nil
}
}
f.current = newAccessor(nil, f.value.Index(f.idx))
return f.current.Set(key, val)
}
func (ac *structAccessor) Set(key string, val string) error {
parts := strings.Split(key, ":")
for i := len(parts); i >= 0; i-- {
k := strings.Join(parts[0:i], ":")
if f := ac.fields[k]; f != nil {
if f.accessor == nil {
if f.value.Kind() == reflect.Ptr && f.value.IsNil() {
f.value.Set(reflect.New(f.value.Type().Elem()))
}
f.accessor = newAccessor(f.tag, *f.value)
}
return f.accessor.Set(key, val)
}
}
return nil
}
func convertErr(key string, val string, ty reflect.Type) error {
return fmt.Errorf("%s field is invalid. (type = %s, value = %s)", key, ty.Name(), val)
}
func unsupportedErr(key string, ty reflect.Type) error {
return fmt.Errorf("%s is unsupported type (field = %s)", ty.Name(), key)
}