-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
henge.go
272 lines (247 loc) · 6.81 KB
/
henge.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Package henge is a type conversion library for Golang.
//
// 変化 (Henge) means "Appearing with a different figure." in Japanese.
// Henge as the name implies can easily convert to different types.
//
// # Usage
//
// Package henge has three conversion methods.
//
// 1. Methods having prefix with To. (e.g. ToInt). These are syntactic sugar. It often used when it can be converted reliably.
//
// henge.ToInt("1")
//
// 2. Starting from New and continue to other methods using method chain.
//
// value, err := henge.New("1").Int().Result()
//
// 3. Using Convert method, it can convert to any type.
//
// in := map[interface{}]interface{}{"a":1,"b":2}
//
// var out map[string]interface{}
// henge.New(in).Convert(&out)
//
// # Links
//
// Source code: https://github.com/soranoba/henge
package henge
import (
"reflect"
)
type (
// InstanceStore is an interface for Converter holds some key-value pairs.
InstanceStore interface {
// InstanceGet returns the value saved using Set.
InstanceGet(key string) interface{}
// InstanceSet saves the value on the key.
InstanceSet(key string, value interface{})
// InstanceSetValues saves multiple key-value pairs.
InstanceSetValues(m map[string]interface{})
}
// Converter is an interface that has common functions of all Converters.
Converter interface {
InstanceStore
Interface() interface{}
Error() error
}
// baseConverter is a struct inherited by each Converter and has common functions.
baseConverter struct {
isNil bool
field string
opts *converterOpts
storage map[string]interface{}
}
)
func (c *baseConverter) new(i interface{}, fieldName string) *ValueConverter {
newConverter := New(i)
newConverter.baseConverter.field = fieldName
newConverter.baseConverter.opts = c.opts
newConverter.baseConverter.storage = c.storage
return newConverter
}
// InstanceGet returns the value saved using Set.
func (c *baseConverter) InstanceGet(key string) interface{} {
return c.storage[key]
}
// InstanceSet saves the value on the key.
func (c *baseConverter) InstanceSet(key string, value interface{}) {
c.storage[key] = value
}
// InstanceSetValues saves multiple key-value pairs.
func (c *baseConverter) InstanceSetValues(m map[string]interface{}) {
for k, v := range m {
c.storage[k] = v
}
}
func (c *baseConverter) wrapConvertError(srcValue interface{}, dstType reflect.Type, err error) error {
if convertErr, ok := err.(*ConvertError); ok {
err := *convertErr
err.DstType = dstType
return &err
}
var srcType reflect.Type
if reflect.ValueOf(srcValue).IsValid() {
srcType = reflect.ValueOf(srcValue).Type()
}
return &ConvertError{
Field: c.field,
SrcType: srcType,
DstType: dstType,
Value: srcValue,
Err: err,
}
}
// ValueConverter is a converter that converts an interface type to another type.
type ValueConverter struct {
*baseConverter
reflectValue reflect.Value
value interface{}
err error
}
// New returns a new ValueConverter
func New(i interface{}, fs ...ConverterOption) *ValueConverter {
opts := defaultConverterOpts()
for _, f := range fs {
f(opts)
}
reflectValue := reflect.ValueOf(i)
isNil := false
switch reflectValue.Kind() {
case reflect.Ptr:
if isNil = reflectValue.IsNil(); isNil {
reflectValue = reflect.New(reflectValue.Type().Elem())
}
case reflect.Slice, reflect.Map:
if isNil = reflectValue.IsNil(); isNil {
reflectValue = reflect.New(reflectValue.Type())
}
case reflect.Interface:
isNil = reflectValue.IsNil()
case reflect.Invalid:
isNil = true
}
return &ValueConverter{
baseConverter: &baseConverter{
isNil: isNil,
opts: opts,
storage: map[string]interface{}{},
},
reflectValue: reflectValue,
value: i,
err: nil,
}
}
// Model converts the input to the specified type.
func (c *ValueConverter) Model(t interface{}) *ValueConverter {
var (
value interface{}
err error
)
v := reflect.ValueOf(t)
if v.Kind() == reflect.Ptr && v.Elem().CanSet() {
value = t
err = c.Convert(t)
} else {
v = reflect.New(v.Type())
err = c.Convert(v.Interface())
value = v.Elem().Interface()
}
return &ValueConverter{baseConverter: c.baseConverter, value: value, err: err}
}
// As set the input value to the output using simple type cast.
func (c *ValueConverter) As(out interface{}) error {
outV := reflect.ValueOf(out)
if outV.Type().Kind() != reflect.Ptr {
panic("out must be ptr")
}
outV = outV.Elem()
outType := outV.Type()
for outType.Kind() == reflect.Ptr {
outType = outType.Elem()
}
inV := c.reflectValue
for inV.Kind() == reflect.Ptr {
inV = inV.Elem()
}
if inV.Type().ConvertibleTo(outType) {
for outV.Kind() == reflect.Ptr {
outV.Set(reflect.New(outV.Type().Elem()))
outV = outV.Elem()
}
outV.Set(inV.Convert(outType))
return nil
}
return c.wrapConvertError(c.value, outType, ErrNotConvertible)
}
// Convert converts the input to the out type and assigns it.
// If the conversion fails, the method returns an error.
func (c *ValueConverter) Convert(out interface{}) error {
outV := reflect.ValueOf(out)
if outV.Type().Kind() != reflect.Ptr {
panic("out must be ptr")
}
return c.convert(outV.Elem())
}
func (c *ValueConverter) convert(outV reflect.Value) error {
outT := outV.Type()
for outT.Kind() == reflect.Ptr {
outT = outT.Elem()
}
switch outT.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return c.Int().convert(outV)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return c.Uint().convert(outV)
case reflect.Float32, reflect.Float64:
return c.Float().convert(outV)
case reflect.Bool:
return c.Bool().convert(outV)
case reflect.String:
return c.String().convert(outV)
case reflect.Array, reflect.Slice:
return c.Slice().convert(outV)
case reflect.Map, reflect.Struct:
t := reflect.ValueOf(c.value).Type()
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Map {
return c.Map().convert(outV)
}
return c.Struct().convert(outV)
case reflect.Interface:
for outV.Kind() == reflect.Ptr {
outV = outV.Elem()
}
outV.Set(reflect.ValueOf(c.value))
return nil
default:
return c.wrapConvertError(c.value, outV.Type(), ErrUnsupportedType)
}
}
// Result returns the conversion result and error.
func (c *ValueConverter) Result() (interface{}, error) {
return c.value, c.err
}
// Value returns the conversion result.
func (c *ValueConverter) Value() interface{} {
return c.value
}
// Interface returns the conversion result of interface type.
func (c *ValueConverter) Interface() interface{} {
return c.value
}
// Error returns an error if the conversion fails.
func (c *ValueConverter) Error() error {
return c.err
}
func toInitializedNonPtrValue(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}