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
| package util
import ( "strings"
"github.com/pkg/errors" )
func (m ValueMap) ParseMap(path string, allowMissing bool, allowEmpty bool) (ValueMap, error) { result, err := m.GetPath(path, allowMissing) if err != nil { return nil, errors.Wrap(err, "invalid int") } switch t := result.(type) { case ValueMap: if (!allowEmpty) && len(t) == 0 { return nil, errors.New("empty map") } return t, nil case map[string]any: if (!allowEmpty) && len(t) == 0 { return nil, errors.New("empty map") } return t, nil case string: if strings.TrimSpace(t) == "" { return nil, nil } ret := ValueMap{} err := FromJSON([]byte(t), &ret) if err != nil { return nil, decorateError(m, path, "time", errors.Wrap(err, "invalid JSON")) } return ret, err case []byte: if len(t) == 0 { return nil, nil } ret := ValueMap{} err := FromJSON(t, &ret) if err != nil { return nil, decorateError(m, path, "time", errors.Wrap(err, "invalid JSON")) } return ret, err case nil: if !allowEmpty { return nil, errors.Errorf("could not find map for path [%s]", path) } return nil, nil default: return nil, invalidTypeError(path, "map", t) } }
|