-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
kv.go
74 lines (57 loc) · 1.43 KB
/
kv.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
package oops
import (
"reflect"
"github.com/samber/lo"
)
func dereferencePointers(data map[string]any) map[string]any {
if !DereferencePointers {
return data
}
for key, value := range data {
val := reflect.ValueOf(value)
if val.Kind() == reflect.Ptr {
// @TODO: might be a pointer to a pointer
data[key] = val.Elem().Interface()
}
}
return data
}
func lazyMapEvaluation(data map[string]any) map[string]any {
for key, value := range data {
switch v := value.(type) {
case map[string]any:
data[key] = lazyMapEvaluation(v)
default:
data[key] = lazyValueEvaluation(value)
}
}
return data
}
func lazyValueEvaluation(value any) any {
v := reflect.ValueOf(value)
if !v.IsValid() || v.Kind() != reflect.Func {
return value
}
if v.Type().NumIn() != 0 || v.Type().NumOut() != 1 {
return value
}
return v.Call([]reflect.Value{})[0].Interface()
}
func getDeepestErrorAttribute[T comparable](err OopsError, getter func(OopsError) T) T {
if err.err == nil {
return getter(err)
}
if child, ok := AsOops(err.err); ok {
return coalesceOrEmpty(getDeepestErrorAttribute(child, getter), getter(err))
}
return getter(err)
}
func mergeNestedErrorMap(err OopsError, getter func(OopsError) map[string]any) map[string]any {
if err.err == nil {
return getter(err)
}
if child, ok := AsOops(err.err); ok {
return lo.Assign(map[string]any{}, getter(err), mergeNestedErrorMap(child, getter))
}
return getter(err)
}