• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ory / x / 13912274386

18 Mar 2025 12:01AM UTC coverage: 61.082% (-1.2%) from 62.237%
13912274386

push

github

web-flow
chore: bump and reduce dependencies (#845)

- bump a bunch of dependencies
- move dev dependencies to `go tools`
- add go.mod for non-consumable module

35 of 42 new or added lines in 6 files covered. (83.33%)

179 existing lines in 21 files now uncovered.

7088 of 11604 relevant lines covered (61.08%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.51
/mapx/type_assert.go
1
// Copyright © 2023 Ory Corp
2
// SPDX-License-Identifier: Apache-2.0
3

4
package mapx
5

6
import (
7
        "encoding/json"
8
        "errors"
9
        "fmt"
10
        "math"
11
        "time"
12
)
13

14
// ErrKeyDoesNotExist is returned when the key does not exist in the map.
15
var ErrKeyDoesNotExist = errors.New("key is not present in map")
16

17
// ErrKeyCanNotBeTypeAsserted is returned when the key can not be type asserted.
18
var ErrKeyCanNotBeTypeAsserted = errors.New("key could not be type asserted")
19

20
// GetString returns a string for a given key in values.
21
func GetString[K comparable](values map[K]any, key K) (string, error) {
1✔
22
        if v, ok := values[key]; !ok {
2✔
23
                return "", ErrKeyDoesNotExist
1✔
24
        } else if sv, ok := v.(string); !ok {
3✔
25
                return "", ErrKeyCanNotBeTypeAsserted
1✔
26
        } else {
2✔
27
                return sv, nil
1✔
28
        }
1✔
29
}
30

31
// GetStringSlice returns a string slice for a given key in values.
32
func GetStringSlice[K comparable](values map[K]any, key K) ([]string, error) {
1✔
33
        if v, ok := values[key]; !ok {
2✔
34
                return []string{}, ErrKeyDoesNotExist
1✔
35
        } else if sv, ok := v.([]string); ok {
3✔
36
                return sv, nil
1✔
37
        } else if sv, ok := v.([]any); ok {
2✔
UNCOV
38
                vs := make([]string, len(sv))
×
UNCOV
39
                for k, v := range sv {
×
UNCOV
40
                        vv, ok := v.(string)
×
UNCOV
41
                        if !ok {
×
42
                                return []string{}, ErrKeyCanNotBeTypeAsserted
×
43
                        }
×
UNCOV
44
                        vs[k] = vv
×
45
                }
UNCOV
46
                return vs, nil
×
47
        }
48
        return []string{}, ErrKeyCanNotBeTypeAsserted
1✔
49
}
50

51
// GetTime returns a string slice for a given key in values.
UNCOV
52
func GetTime[K comparable](values map[K]any, key K) (time.Time, error) {
×
UNCOV
53
        v, ok := values[key]
×
UNCOV
54
        if !ok {
×
55
                return time.Time{}, ErrKeyDoesNotExist
×
56
        }
×
57

UNCOV
58
        if sv, ok := v.(time.Time); ok {
×
59
                return sv, nil
×
UNCOV
60
        } else if sv, ok := v.(int64); ok {
×
61
                return time.Unix(sv, 0), nil
×
UNCOV
62
        } else if sv, ok := v.(int32); ok {
×
63
                return time.Unix(int64(sv), 0), nil
×
UNCOV
64
        } else if sv, ok := v.(int); ok {
×
UNCOV
65
                return time.Unix(int64(sv), 0), nil
×
UNCOV
66
        } else if sv, ok := v.(float64); ok {
×
UNCOV
67
                return time.Unix(int64(sv), 0), nil
×
UNCOV
68
        } else if sv, ok := v.(float32); ok {
×
69
                return time.Unix(int64(sv), 0), nil
×
70
        }
×
71

72
        return time.Time{}, ErrKeyCanNotBeTypeAsserted
×
73
}
74

75
// GetInt64Default returns a int64 or the default value for a given key in values.
76
func GetInt64Default[K comparable](values map[K]any, key K, defaultValue int64) int64 {
1✔
77
        f, err := GetInt64(values, key)
1✔
78
        if err != nil {
2✔
79
                return defaultValue
1✔
80
        }
1✔
81
        return f
1✔
82
}
83

84
// GetInt64 returns an int64 for a given key in values.
85
func GetInt64[K comparable](values map[K]any, key K) (int64, error) {
1✔
86
        v, ok := values[key]
1✔
87
        if !ok {
2✔
88
                return 0, ErrKeyDoesNotExist
1✔
89
        }
1✔
90
        switch v := v.(type) {
1✔
91
        case json.Number:
×
92
                return v.Int64()
×
93
        case int64:
1✔
94
                return v, nil
1✔
95
        case int:
1✔
96
                return int64(v), nil
1✔
97
        case int32:
1✔
98
                return int64(v), nil
1✔
99
        case uint:
×
100
                vv := uint64(v)
×
101
                if vv > math.MaxInt64 {
×
102
                        return 0, errors.New("value is out of range")
×
103
                }
×
104
                return int64(vv), nil
×
105
        case uint32:
×
106
                return int64(v), nil
×
107
        case uint64:
×
108
                if v > math.MaxInt64 {
×
109
                        return 0, errors.New("value is out of range")
×
110
                }
×
111
                return int64(v), nil
×
112
        }
113
        return 0, ErrKeyCanNotBeTypeAsserted
1✔
114
}
115

116
// GetInt32Default returns a int32 or the default value for a given key in values.
117
func GetInt32Default[K comparable](values map[K]any, key K, defaultValue int32) int32 {
1✔
118
        f, err := GetInt32(values, key)
1✔
119
        if err != nil {
2✔
120
                return defaultValue
1✔
121
        }
1✔
122
        return f
1✔
123
}
124

125
// GetInt32 returns an int32 for a given key in values.
126
func GetInt32[K comparable](values map[K]any, key K) (int32, error) {
1✔
127
        v, err := GetInt64(values, key)
1✔
128
        if err != nil {
2✔
129
                return 0, err
1✔
130
        }
1✔
131
        if v > math.MaxInt32 || v < math.MinInt32 {
1✔
132
                return 0, errors.New("value is out of range")
×
133
        }
×
134
        return int32(v), nil
1✔
135
}
136

137
// GetIntDefault returns a int or the default value for a given key in values.
138
func GetIntDefault[K comparable](values map[K]any, key K, defaultValue int) int {
1✔
139
        f, err := GetInt(values, key)
1✔
140
        if err != nil {
2✔
141
                return defaultValue
1✔
142
        }
1✔
143
        return f
1✔
144
}
145

146
// GetInt returns an int for a given key in values.
147
func GetInt[K comparable](values map[K]any, key K) (int, error) {
1✔
148
        v, err := GetInt64(values, key)
1✔
149
        if err != nil {
2✔
150
                return 0, err
1✔
151
        }
1✔
152
        if v > math.MaxInt || v < math.MinInt {
1✔
153
                return 0, errors.New("value is out of range")
×
154
        }
×
155
        return int(v), nil
1✔
156
}
157

158
// GetFloat32Default returns a float32 or the default value for a given key in values.
159
func GetFloat32Default[K comparable](values map[K]any, key K, defaultValue float32) float32 {
1✔
160
        f, err := GetFloat32(values, key)
1✔
161
        if err != nil {
2✔
162
                return defaultValue
1✔
163
        }
1✔
164
        return f
1✔
165
}
166

167
// GetFloat32 returns a float32 for a given key in values.
168
func GetFloat32[K comparable](values map[K]any, key K) (float32, error) {
1✔
169
        if v, ok := values[key]; !ok {
2✔
170
                return 0, ErrKeyDoesNotExist
1✔
171
        } else if j, ok := v.(json.Number); ok {
2✔
172
                v, err := j.Float64()
×
173
                return float32(v), err
×
174
        } else if sv, ok := v.(float32); ok {
2✔
175
                return sv, nil
1✔
176
        }
1✔
177
        return 0, ErrKeyCanNotBeTypeAsserted
1✔
178
}
179

180
// GetFloat64Default returns a float64 or the default value for a given key in values.
181
func GetFloat64Default[K comparable](values map[K]any, key K, defaultValue float64) float64 {
1✔
182
        f, err := GetFloat64(values, key)
1✔
183
        if err != nil {
2✔
184
                return defaultValue
1✔
185
        }
1✔
186
        return f
1✔
187
}
188

189
// GetFloat64 returns a float64 for a given key in values.
190
func GetFloat64[K comparable](values map[K]any, key K) (float64, error) {
1✔
191
        if v, ok := values[key]; !ok {
2✔
192
                return 0, ErrKeyDoesNotExist
1✔
193
        } else if j, ok := v.(json.Number); ok {
2✔
194
                return j.Float64()
×
195
        } else if sv, ok := v.(float64); ok {
2✔
196
                return sv, nil
1✔
197
        }
1✔
198
        return 0, ErrKeyCanNotBeTypeAsserted
1✔
199
}
200

201
// GetStringDefault returns a string or the default value for a given key in values.
202
func GetStringDefault[K comparable](values map[K]any, key K, defaultValue string) string {
1✔
203
        if s, err := GetString(values, key); err == nil {
2✔
204
                return s
1✔
205
        }
1✔
206
        return defaultValue
1✔
207
}
208

209
// GetStringSliceDefault returns a string slice or the default value for a given key in values.
210
func GetStringSliceDefault[K comparable](values map[K]any, key K, defaultValue []string) []string {
1✔
211
        if s, err := GetStringSlice(values, key); err == nil {
2✔
212
                return s
1✔
213
        }
1✔
214
        return defaultValue
1✔
215
}
216

217
// KeyStringToInterface converts map[string]any to map[any]any
218
// Deprecated: with generics, this should not be necessary anymore.
219
func KeyStringToInterface(i map[string]any) map[any]any {
1✔
220
        o := make(map[any]any)
1✔
221
        for k, v := range i {
2✔
222
                o[k] = v
1✔
223
        }
1✔
224
        return o
1✔
225
}
226

227
// ToJSONMap converts all map[any]any occurrences (nested as well) to map[string]any.
228
// Deprecated: with generics, this should not be necessary anymore.
229
func ToJSONMap(i any) any {
1✔
230
        switch t := i.(type) {
1✔
231
        case []any:
1✔
232
                for k, v := range t {
2✔
233
                        t[k] = ToJSONMap(v)
1✔
234
                }
1✔
235
                return t
1✔
236
        case map[string]any:
1✔
237
                for k, v := range t {
2✔
238
                        t[k] = ToJSONMap(v)
1✔
239
                }
1✔
240
                return t
1✔
241
        case map[any]any:
1✔
242
                res := make(map[string]any)
1✔
243
                for k, v := range t {
2✔
244
                        res[fmt.Sprintf("%s", k)] = ToJSONMap(v)
1✔
245
                }
1✔
246
                return res
1✔
247
        }
248

249
        return i
1✔
250
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc