-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.go
117 lines (102 loc) · 2.43 KB
/
attribute.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
package attr
import (
json "github.com/goccy/go-json"
)
// Attr interface describes the behavior that a serializable attribute
// should have.
//
// Besides retrieving its key and value, it also permits creating a copy of
// the original Attr with a different key or a different value
type Attr interface {
// Key returns the string key of the attribute Attr
Key() string
// Value returns the (any) value of the attribute Attr
Value() any
// WithKey returns a copy of this Attr, with key `key`
WithKey(key string) Attr
// WithValue returns a copy of this Attr, with value `value`
//
// It must be the same type of the original Attr, otherwise returns
// nil
WithValue(value any) Attr
}
// New is a generic function to create an Attr
//
// Using a generic approach allows the Attr.WithValue method to be
// scoped with certain constraints for specific applications
func New[T any](key string, value T) Attr {
if key == "" {
return nil
}
return attr[T]{
key: key,
value: value,
}
}
type attr[T any] struct {
key string
value T
}
// Key returns the string key of the attribute Attr
func (a attr[T]) Key() string {
return a.key
}
// Value returns the (any) value of the attribute Attr
func (a attr[T]) Value() any {
return a.value
}
// WithKey returns a copy of this Attr, with key `key`
func (a attr[T]) WithKey(key string) Attr {
if key == "" {
return nil
}
return New(key, a.value)
}
// WithValue returns a copy of this Attr, with value `value`
//
// It must be the same type of the original Attr, otherwise returns
// nil
func (a attr[T]) WithValue(value any) Attr {
if value == nil {
return nil
}
v, ok := (value).(T)
if !ok {
return nil
}
return New(a.key, v)
}
func Map(attrs ...Attr) map[string]any {
return mapAttrs(attrs...)
}
// MarshalJSON encodes the attribute as a JSON object (key-value pair)
func (a attr[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(Map(a))
}
func (a attr[T]) MarshalText() (text []byte, err error) {
return a.MarshalJSON()
}
// String implements fmt.Stringer
func (a attr[T]) String() string {
b, _ := a.MarshalJSON()
return string(b)
}
func mapAttrs(attrs ...Attr) map[string]any {
kv := map[string]any{}
for _, a := range attrs {
if a == nil {
continue
}
switch v := a.Value().(type) {
case []Attr:
kv[a.Key()] = mapAttrs(v...)
case Attrs:
kv[a.Key()] = mapAttrs(v...)
case Attr:
kv[a.Key()] = mapAttrs(v)
default:
kv[a.Key()] = v
}
}
return kv
}