-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamapping.go
37 lines (28 loc) · 856 Bytes
/
datamapping.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
package goBungieNet
import (
"reflect"
"time"
"strconv"
"github.com/mitchellh/mapstructure"
)
func decodeHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if t == reflect.TypeOf(time.Time{}) && f.Kind() == reflect.String {
return time.Parse(time.RFC3339, data.(string))
}
if t.Kind() == reflect.Int64 && f.Kind() == reflect.String {
return strconv.ParseInt(data.(string), 10, 64)
}
if t.Kind() == reflect.Uint32 && f.Kind() == reflect.String {
return strconv.ParseUint(data.(string), 10, 32)
}
return data, nil
}
func decode(source interface{}, target interface{}) error {
config := mapstructure.DecoderConfig{
DecodeHook: decodeHook,
Result: target,
}
decoder, err := mapstructure.NewDecoder(&config)
if err != nil { return err }
return decoder.Decode(source)
}