-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
189 lines (159 loc) · 3.73 KB
/
container.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package goexer
import (
"encoding/json"
"fmt"
"reflect"
"github.com/samber/lo"
"github.com/spf13/cast"
)
// One item inside container.
type Item struct {
Value interface{}
Type string // Just for better string representation.
Name string // Just for better string representation.
}
// Marshaled representation.
type Marshaled map[string]interface{}
// Return string representation for Item.
func (i *Item) String() string {
return fmt.Sprintf("Item{ Name: '%s' Type: '%s' Value: %v}", i.Name, i.Type, i.Get())
}
// Return raw, unconverted Value as interface{}.
func (i *Item) GetRaw() interface{} {
return i.Value
}
// Return converted to original type, if possible, value of item.
//nolint:gocyclo
func (i *Item) Get() any {
switch i.Type {
case "bool":
return cast.ToBool(i.Value)
case "time.Time":
return cast.ToTime(i.Value)
case "time.Duration":
return cast.ToDuration(i.Value)
case "float64":
return cast.ToFloat64(i.Value)
case "float32":
return cast.ToFloat32(i.Value)
case "int64":
return cast.ToInt64(i.Value)
case "int32":
return cast.ToInt32(i.Value)
case "int16":
return cast.ToInt16(i.Value)
case "int8":
return cast.ToInt8(i.Value)
case "int":
return cast.ToInt(i.Value)
case "uint64":
return cast.ToUint64(i.Value)
case "uint32":
return cast.ToUint32(i.Value)
case "uint16":
return cast.ToUint16(i.Value)
case "uint8":
return cast.ToUint8(i.Value)
case "uint":
return cast.ToUint(i.Value)
case "string":
return cast.ToString(i.Value)
case "map[string]string":
return cast.ToStringMapString(i.Value)
case "map[string][]string":
return cast.ToStringMapStringSlice(i.Value)
case "map[string]bool":
return cast.ToStringMapBool(i.Value)
case "map[string]int":
return cast.ToStringMapInt(i.Value)
case "map[string]int64":
return cast.ToStringMapInt64(i.Value)
case "[]bool":
return cast.ToBoolSlice(i.Value)
case "[]string":
return cast.ToStringSlice(i.Value)
case "[]int":
return cast.ToIntSlice(i.Value)
case "[]time.Duration":
return cast.ToDurationSlice(i.Value)
default:
return i.Value
}
}
// Container for extended data.
type Container struct {
items map[string]Item
}
// Return value of Item.
func (c *Container) Get(key string) any {
item, ok := c.items[key]
if !ok {
return nil
}
return item.Get()
}
// Return raw value of Item.
func (c *Container) GetRaw(key string) any {
item, ok := c.items[key]
if !ok {
return nil
}
return item.GetRaw()
}
// Return value of Item with ok status.
func (c *Container) GetE(key string) (any, bool) {
item, ok := c.items[key]
if !ok {
return nil, false
}
return item.Get(), true
}
// Return raw value of Item with ok status.
func (c *Container) GetRawE(key string) (any, bool) {
item, ok := c.items[key]
if !ok {
return nil, false
}
return item.GetRaw(), true
}
// Set value for item.
func (c *Container) Set(key string, value interface{}) *Container {
item := Item{
Name: key,
Value: value,
}
item.Type = reflect.TypeOf(value).String()
c.items[key] = item
return c
}
// Create new variable of type Container.
func NewContainer() *Container {
cc := Container{}
cc.items = make(map[string]Item)
return &cc
}
// Return count of fields (items) inside Container.
func (c *Container) Size() int {
return len(c.items)
}
// Return all keys of fields inside Container.
func (c *Container) Keys() []string {
return lo.Keys(c.items)
}
// Return marshaled representation.
func (c *Container) Marshaled() Marshaled {
m := make(map[string]interface{}, len(c.items)+1)
for k, v := range c.items {
m[k] = v.Get()
}
return m
}
// return json representation.
func (c *Container) JSON() ([]byte, error) {
j, err := json.Marshal(c.Marshaled())
if err != nil {
//nolint:wrapcheck
return nil, err
}
return j, nil
}