-
Notifications
You must be signed in to change notification settings - Fork 46
/
properties.go
230 lines (170 loc) · 4.31 KB
/
properties.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"strconv"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type Property struct {
Properties *Properties
Name string
data interface{}
InUse bool
OnChange func()
OnlySerializeInSaves bool
}
func NewProperty(name string, properties *Properties) *Property {
return &Property{
Properties: properties,
Name: name,
}
}
func (prop *Property) IsString() bool {
_, isOK := prop.data.(string)
return isOK
}
func (prop *Property) AsString() string {
if prop.data == nil {
prop.data = ""
}
return prop.data.(string)
}
func (prop *Property) AsJSON() gjson.Result {
if prop.data == nil {
prop.data = "{}"
}
return gjson.Parse(prop.data.(string))
}
func (prop *Property) IsBool() bool {
_, isOK := prop.data.(bool)
return isOK
}
func (prop *Property) AsBool() bool {
if prop.data == nil {
prop.data = false
}
return prop.data.(bool)
}
func (prop *Property) IsNumber() bool {
_, isOK := prop.data.(float64)
return isOK
}
func (prop *Property) AsFloat() float64 {
if prop.data == nil {
prop.data = 0.0
}
return prop.data.(float64)
}
func (prop *Property) AsMap() map[string]interface{} {
if prop.data == nil {
prop.data = map[string]interface{}{}
}
return prop.data.(map[string]interface{})
}
func (prop *Property) Set(value interface{}) {
if prop.data != value {
if intData, isInt := value.(int); isInt {
prop.data = float64(intData)
} else {
prop.data = value
}
if prop.OnChange != nil {
prop.OnChange()
}
if prop.Properties != nil && prop.Properties.OnChange != nil {
prop.Properties.OnChange(prop)
}
}
}
func (prop *Property) AsArrayOfInts() []int64 {
if !prop.IsString() {
prop.data = "{}"
}
out := []int64{}
for _, value := range prop.AsJSON().Array() {
out = append(out, value.Int())
}
return out
}
func (prop *Property) SetInts(values ...int64) {
jsonStr := "["
for _, v := range values {
jsonStr += strconv.Itoa(int(v))
}
jsonStr += "]"
prop.Set(jsonStr)
}
// func (prop *Property) SetAfterParse(value interface{}) {
// current := prop.AsJSON()
// parsed, err := sjson.Set(current.String(), "#0", value)
// prop.Set(parsed)
// fmt.Println(parsed, err)
// }
// SetRaw sets the property, but without triggering OnChange
func (prop *Property) SetRaw(value interface{}) {
prop.data = value
}
// Contains serializable properties for a Card.
type Properties struct {
Props map[string]*Property
DefinitionOrder []string
OnChange func(property *Property)
}
func NewProperties() *Properties {
return &Properties{
Props: map[string]*Property{},
DefinitionOrder: []string{},
}
}
func (properties *Properties) Has(name string) bool {
if _, exists := properties.Props[name]; exists {
return true
}
return false
}
func (properties *Properties) Get(name string) *Property {
if _, exists := properties.Props[name]; !exists {
properties.Props[name] = NewProperty(name, properties)
properties.DefinitionOrder = append(properties.DefinitionOrder, name)
}
prop := properties.Props[name]
prop.InUse = true
return prop
}
func (properties *Properties) SetDefault(propertyName string, value any) {
if !properties.Has(propertyName) {
properties.Get(propertyName).Set(value)
}
}
func (properties *Properties) GetIfExists(propertyName string) *Property {
if properties.Has(propertyName) {
return properties.Get(propertyName)
}
return nil
}
func (properties *Properties) Remove(propertyName string) {
delete(properties.Props, propertyName)
for i, p := range properties.DefinitionOrder {
if p == propertyName {
properties.DefinitionOrder = append(properties.DefinitionOrder[:i], properties.DefinitionOrder[i+1:]...)
break
}
}
}
func (properties *Properties) Serialize(saving bool) string {
data := "{}"
for _, name := range properties.DefinitionOrder {
prop := properties.Props[name]
if prop.InUse && (!prop.OnlySerializeInSaves || saving) {
data, _ = sjson.Set(data, name, prop.data)
}
}
return data
}
func (properties *Properties) Deserialize(data string) {
// All Properties contained within this object should probably be cleared before parsing...?
parsed := gjson.Parse(data)
parsed.ForEach(func(key, value gjson.Result) bool {
properties.Get(key.String()).SetRaw(value.Value())
return true
})
}