-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapget.go
154 lines (126 loc) · 3.63 KB
/
mapget.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
154
package util
import (
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
func (m ValueMap) GetRequired(k string) (any, error) {
v, ok := m[k]
if !ok {
const msg = "no value [%s] among candidates [%s]"
return nil, errors.Errorf(msg, k, StringArrayOxfordComma(m.Keys(), "and"))
}
return v, nil
}
func (m ValueMap) GetArray(key string, allowEmpty bool) ([]any, error) {
return m.ParseArray(key, false, allowEmpty, false)
}
func (m ValueMap) GetArrayOpt(key string) []any {
ret, _ := m.ParseArray(key, false, true, false)
return ret
}
func (m ValueMap) GetStringArray(key string, allowEmpty bool) ([]string, error) {
return m.ParseArrayString(key, false, allowEmpty)
}
func (m ValueMap) GetStringArrayOpt(key string) []string {
ret, _ := m.ParseArrayString(key, false, true)
return ret
}
func (m ValueMap) GetIntArray(key string, allowEmpty bool) ([]int, error) {
return m.ParseArrayInt(key, false, allowEmpty)
}
func (m ValueMap) GetFloatArray(key string, allowEmpty bool) ([]float64, error) {
return m.ParseArrayFloat(key, false, allowEmpty)
}
func (m ValueMap) GetMapArray(key string, allowEmpty bool) ([]ValueMap, error) {
return m.ParseArrayMap(key, false, allowEmpty)
}
func (m ValueMap) GetBool(key string, allowEmpty bool) (bool, error) {
return m.ParseBool(key, false, allowEmpty)
}
func (m ValueMap) GetBoolOpt(key string) bool {
ret, _ := m.ParseBool(key, true, true)
return ret
}
func (m ValueMap) GetInt(key string, allowEmpty bool) (int, error) {
return m.ParseInt(key, false, allowEmpty)
}
func (m ValueMap) GetIntOpt(key string) int {
ret, _ := m.ParseInt(key, false, true)
return ret
}
func (m ValueMap) GetInt64(key string, allowEmpty bool) (int64, error) {
ret, err := m.ParseInt(key, false, allowEmpty)
return int64(ret), err
}
func (m ValueMap) GetFloat(key string, allowEmpty bool) (float64, error) {
return m.ParseFloat(key, false, allowEmpty)
}
func (m ValueMap) GetFloatOpt(key string) float64 {
ret, _ := m.ParseFloat(key, false, true)
return ret
}
func (m ValueMap) GetMap(key string, allowEmpty bool) (ValueMap, error) {
return m.ParseMap(key, false, allowEmpty)
}
func (m ValueMap) GetMapOpt(key string) ValueMap {
ret, _ := m.ParseMap(key, false, true)
return ret
}
func (m ValueMap) GetString(key string, allowEmpty bool) (string, error) {
return m.ParseString(key, false, allowEmpty)
}
func (m ValueMap) GetStringOpt(key string) string {
ret, _ := m.ParseString(key, true, true)
return ret
}
func (m ValueMap) GetStringPtr(key string) *string {
ret, err := m.ParseString(key, true, true)
if err != nil {
return nil
}
return &ret
}
func (m ValueMap) GetTime(key string, allowEmpty bool) (*time.Time, error) {
return m.ParseTime(key, false, allowEmpty)
}
func (m ValueMap) GetTimeOpt(key string) time.Time {
ret, _ := m.ParseTime(key, true, true)
if ret == nil {
return time.Time{}
}
return *ret
}
func (m ValueMap) GetUUID(key string, allowEmpty bool) (*uuid.UUID, error) {
return m.ParseUUID(key, false, allowEmpty)
}
func (m ValueMap) GetUUIDOpt(key string) uuid.UUID {
ret, _ := m.ParseUUID(key, true, true)
if ret == nil {
return uuid.UUID{}
}
return *ret
}
func (m ValueMap) GetType(key string, ret any) error {
result, err := m.GetPath(key, false)
if err != nil {
return errors.Wrap(err, "invalid type")
}
switch t := result.(type) {
case []byte:
err = FromJSON(t, ret)
if err != nil {
return errors.Wrap(err, "failed to unmarshal to expected type")
}
return nil
default:
return errors.Errorf("expected binary json data, encountered %T", t)
}
}
func MapGetOrElse[K comparable, V any](mp map[K]V, k K, dflt V) V {
ret, ok := mp[k]
if ok {
return ret
}
return dflt
}