-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtos.go
36 lines (31 loc) · 872 Bytes
/
mtos.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
package mtos
import (
"github.com/mitchellh/mapstructure"
)
var decodeHook = mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.TextUnmarshallerHookFunc(),
mapstructure.StringToSliceHookFunc(","),
)
type DecoderConfigOption func(*mapstructure.DecoderConfig)
func defaultDecoderConfig(output any, opts ...DecoderConfigOption) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
Squash: true,
DecodeHook: decodeHook,
}
for _, opt := range opts {
opt(c)
}
return c
}
func Unmarshal(dst any, mapData map[string]any, opts ...DecoderConfigOption) error {
config := defaultDecoderConfig(dst)
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(mapData)
}