forked from fioepq9/helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_hook.go
executable file
·153 lines (142 loc) · 3.69 KB
/
decode_hook.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
package helper
import (
"reflect"
"strconv"
"strings"
"time"
"github.com/cockroachdb/errors"
"github.com/mitchellh/mapstructure"
)
func StringToSliceHookFunc(sep string) mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to != reflect.SliceOf(from) {
return data, nil
}
s := data.(string)
return strings.Split(s, sep), nil
}
}
func UnmarshalToStructHookFunc(unmarshal func(in []byte, out any) error) mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to.Kind() != reflect.Struct {
return data, nil
}
s := data.(string)
out := reflect.New(to).Interface()
err := unmarshal([]byte(s), out)
return out, err
}
}
func UnmarshalToMapHookFunc(unmarshal func(in []byte, out any) error) mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to.Kind() != reflect.Map {
return data, nil
}
s := data.(string)
out := reflect.MakeMap(to).Interface()
err := unmarshal([]byte(s), &out)
return out, err
}
}
func UnmarshalToSliceHookFunc(unmarshal func(in []byte, out any) error) mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to.Kind() != reflect.Slice {
return data, nil
}
s := data.(string)
out := reflect.MakeSlice(to, 0, 0).Interface()
err := unmarshal([]byte(s), &out)
return out, err
}
}
func StringToBoolHookFunc() mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to != reflect.TypeOf(true) {
return data, nil
}
s := data.(string)
return s == "true", nil
}
}
func StringToIntHookFunc() mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to != reflect.TypeOf(1) {
return data, nil
}
s := data.(string)
return strconv.Atoi(s)
}
}
func StringToFloat64HookFunc() mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to != reflect.TypeOf(1.0) {
return data, nil
}
s := data.(string)
return strconv.ParseFloat(s, 64)
}
}
func StringToBytesHookFunc() mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to != reflect.TypeOf([]byte{}) {
return data, nil
}
s := data.(string)
return []byte(s), nil
}
}
func StringToTimeHookFunc() mapstructure.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() != reflect.String {
return data, nil
}
if to != reflect.TypeOf(time.Time{}) {
return data, nil
}
s := data.(string)
if !strings.HasPrefix(s, "now") {
return data, errors.New("cannot find keyword now")
}
s = strings.TrimPrefix(s, "now")
now := time.Now()
if len(s) == 0 {
return now, nil
}
verb, s := s[0], s[1:]
d, err := time.ParseDuration(s)
if err != nil {
return data, errors.Wrapf(err, "cannot parse %s to time.Duration", s)
}
if verb == '+' {
return now.Add(d), nil
} else if verb == '-' {
return now.Add(-d), nil
} else {
return data, errors.Newf("unsupported verb %c", verb)
}
}
}