Split common utility functions into util package (#64)

This commit is contained in:
andig 2020-04-27 18:49:04 +02:00
parent 56ad3dad95
commit 1729399ef9
44 changed files with 177 additions and 193 deletions

24
util/decoder.go Normal file
View file

@ -0,0 +1,24 @@
package util
import (
"github.com/mitchellh/mapstructure"
)
// DecodeOther uses mapstructure to decode into target structure. Unused keys cause errors.
func DecodeOther(log *Logger, other interface{}, cc interface{}) {
decoderConfig := &mapstructure.DecoderConfig{
Result: cc,
ErrorUnused: true,
WeaklyTypedInput: true,
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
}
decoder, err := mapstructure.NewDecoder(decoderConfig)
if err != nil {
log.FATAL.Fatal(err)
}
if err := decoder.Decode(other); err != nil {
log.FATAL.Fatal(err)
}
}