2020-10-23 03:54:50 +13:00
|
|
|
package utils
|
|
|
|
|
2021-03-18 03:47:49 +13:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2022-07-28 22:20:20 +12:00
|
|
|
func Unmarshal(in any, raw []byte, callback func() error) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := json.Unmarshal(raw, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
2021-03-18 03:47:49 +13:00
|
|
|
|
2022-07-28 22:20:20 +12:00
|
|
|
func JsonStringAutoDecode(m any) func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
|
|
|
|
return func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
|
2021-03-18 03:47:49 +13:00
|
|
|
if rf != reflect.String || rt == reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := data.(string)
|
|
|
|
if raw != "" && (raw[0:1] == "{" || raw[0:1] == "[") {
|
|
|
|
err := json.Unmarshal([]byte(raw), &m)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
}
|