-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
83 lines (69 loc) · 2.15 KB
/
values.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
package debefix
import (
"iter"
"maps"
)
// Values represents a list of the row fields and values, similar to a map[string]any.
type Values interface {
Get(fieldName string) (val any, exists bool) // gets the value of a field, returning whether the field exists.
GetOrNil(fieldName string) any // gets the value of a field, or nil if the field don't exist.
GetDefault(fieldName string, def any) any // gets the value of a field, or a default value if the field don't exist.
All(yield func(string, any) bool) // iterator of all the field values.
Len() int // returns the amount of field values.
}
// ValuesMutable is a Values that can be mutated.
type ValuesMutable interface {
Values
Set(fieldName string, val any) // sets a field value.
Insert(seq iter.Seq2[string, any]) // insert a list of field values.
Delete(fieldName ...string) // delete field values.
}
// ValuesGet gets a value from values casting to the T type.
func ValuesGet[T any](values Values, fieldName string) (val T, exists bool, isType bool) {
v, ok := values.Get(fieldName)
if !ok {
var ret T
return ret, ok, false
}
vt, ok := v.(T)
return vt, true, ok
}
// NewValues creates a new ValuesMutable.
func NewValues(val map[string]any) ValuesMutable {
if val == nil {
val = make(map[string]any)
}
return MapValues(val)
}
// MapValues is a ValuesMutable implementation using a map[string]any
type MapValues map[string]any
func (v MapValues) Get(fieldName string) (val any, exists bool) {
val, exists = v[fieldName]
return
}
func (v MapValues) GetDefault(fieldName string, def any) any {
if val, ok := v[fieldName]; ok {
return val
}
return def
}
func (v MapValues) GetOrNil(fieldName string) any {
return v[fieldName]
}
func (v MapValues) Len() int {
return len(v)
}
func (v MapValues) All(yield func(string, any) bool) {
maps.All(v)(yield)
}
func (v MapValues) Insert(seq iter.Seq2[string, any]) {
maps.Insert(v, seq)
}
func (v MapValues) Set(fieldName string, val any) {
v[fieldName] = val
}
func (v MapValues) Delete(fieldName ...string) {
for _, fieldName := range fieldName {
delete(v, fieldName)
}
}