This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmapstructure.go
61 lines (50 loc) · 1.86 KB
/
mapstructure.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
package orient
import (
"github.com/mitchellh/mapstructure"
"reflect"
"time"
)
// TagName is a name for a struct tag used for types conversion using reflect
var TagName = "mapstructure"
var mapDecoderHooks = []mapstructure.DecodeHookFunc{
stringToTimeHookFunc,
stringToByteSliceHookFunc,
documentToMapHookFunc,
}
// RegisterMapDecoderHook allows to register additional hook for map decoder
func RegisterMapDecoderHook(hook mapstructure.DecodeHookFunc) {
mapDecoderHooks = append(mapDecoderHooks, hook)
}
// NewMapDecoder returns decoder configured for decoding data into result with all registered hooks.
func newMapDecoder(result interface{}) (*mapstructure.Decoder, error) {
return mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(mapDecoderHooks...),
Metadata: nil,
Result: result,
TagName: TagName,
})
}
var reflTimeType = reflect.TypeOf((*time.Time)(nil)).Elem()
// StringToTimeHookFunc returns a DecodeHookFunc that converts
// strings to time.Time using RFC3339Nano format.
func stringToTimeHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String || t != reflTimeType {
return data, nil
}
return time.Parse(time.RFC3339Nano, data.(string))
}
var reflByteSliceType = reflect.TypeOf(([]byte)(nil))
// StringToByteSliceHookFunc returns a DecodeHookFunc that converts strings to []byte.
func stringToByteSliceHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String || t != reflByteSliceType {
return data, nil
}
return []byte(data.(string)), nil
}
var reflDocumentType = reflect.TypeOf((*Document)(nil))
func documentToMapHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f != reflDocumentType {
return data, nil
}
return data.(*Document).ToMap()
}