-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
278 lines (232 loc) · 5.62 KB
/
types.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package kdlgo
import (
"math/big"
"strconv"
"strings"
)
type KDLType string
const (
KDLBoolType = "kdl_bool"
KDLNumberType = "kdl_number"
KDLStringType = "kdl_string"
KDLRawStringType = "kdl_raw_string"
KDLDocumentType = "kdl_document"
KDLNullType = "kdl_null"
KDLDefaultType = "kdl_default"
KDLObjectsType = "kdl_objects"
)
type KDLNode struct {
node string
declaredType string
}
type KDLValue struct {
Bool bool
Number big.Float
String string
RawString string
Document []KDLValue
Objects []KDLObject
Type KDLType
declaredType string
}
func (kdlValue KDLValue) RecreateKDL() (string, error) {
switch kdlValue.Type {
case KDLBoolType:
return strconv.FormatBool(kdlValue.Bool), nil
case KDLNumberType:
num := kdlValue.Number
f64, _ := num.Float64()
return strconv.FormatFloat(f64, 'f', -1, 64), nil
case KDLStringType:
return RecreateString(kdlValue.String), nil
case KDLRawStringType:
return RecreateString(kdlValue.RawString), nil
case KDLDocumentType:
var s strings.Builder
for i, v := range kdlValue.Document {
str, err := v.RecreateKDL()
if err != nil {
return "", err
}
s.WriteString(str)
if i+1 != len(kdlValue.Document) {
s.WriteRune(' ')
}
}
return s.String(), nil
case KDLNullType:
return "null", nil
case KDLDefaultType:
return "", nil
case KDLObjectsType:
if len(kdlValue.Objects) < 1 {
return "", nil
}
var s strings.Builder
for _, obj := range kdlValue.Objects {
objStr, err := RecreateKDLObj(obj)
if err != nil {
return "", err
}
s.WriteString(objStr + "; ")
}
return "{ " + s.String() + "}", nil
default:
return "", invalidTypeErr()
}
}
func RecreateString(s string) string {
return strings.ReplaceAll(strconv.Quote(s), "/", "\\/")
}
func (kdlValue KDLValue) ToString() (string, error) {
switch kdlValue.Type {
case KDLStringType:
return kdlValue.String, nil
case KDLRawStringType:
return kdlValue.RawString, nil
case KDLBoolType, KDLNumberType, KDLDocumentType, KDLNullType, KDLDefaultType, KDLObjectsType:
fallthrough
default:
return kdlValue.RecreateKDL()
}
}
type KDLObject interface {
GetKey() string
GetValue() KDLValue
}
func RecreateKDLObj(kdlObj KDLObject) (string, error) {
s, err := kdlObj.GetValue().RecreateKDL()
if err != nil {
return "", nil
}
if len(s) > 0 {
s = " " + s
}
key := kdlObj.GetKey()
if strings.Contains(key, " ") || strconv.Quote(key) != "\""+key+"\"" {
key = strconv.Quote(key)
}
return key + s, nil
}
type KDLBool struct {
key string
value KDLValue
}
func NewKDLBool(key string, value bool) KDLBool {
return KDLBool{key: key, value: KDLValue{Bool: value, Type: KDLBoolType}}
}
func (kdlNode KDLBool) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLBool) GetValue() KDLValue {
return kdlNode.value
}
type KDLNumber struct {
key string
value KDLValue
}
func NewKDLNumber(key string, value float64) KDLNumber {
return KDLNumber{key: key, value: KDLValue{Number: *big.NewFloat(value), Type: KDLNumberType}}
}
func (kdlNode KDLNumber) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLNumber) GetValue() KDLValue {
return kdlNode.value
}
type KDLString struct {
key string
value KDLValue
}
func NewKDLString(key string, value string) KDLString {
value = strings.ReplaceAll(value, "\n", "\\n")
s, _ := strconv.Unquote(`"` + value + `"`)
return KDLString{key: key, value: KDLValue{String: s, Type: KDLStringType}}
}
func (kdlNode KDLString) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLString) GetValue() KDLValue {
return kdlNode.value
}
type KDLRawString struct {
key string
value KDLValue
}
func NewKDLRawString(key string, value string) KDLRawString {
return KDLRawString{key: key, value: KDLValue{RawString: value, Type: KDLRawStringType}}
}
func (kdlNode KDLRawString) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLRawString) GetValue() KDLValue {
return kdlNode.value
}
type KDLDocument struct {
key string
value KDLValue
}
func NewKDLDocument(key string, value []KDLValue) KDLDocument {
return KDLDocument{key: key, value: KDLValue{Document: value, Type: KDLDocumentType}}
}
func (kdlNode KDLDocument) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLDocument) GetValue() KDLValue {
return kdlNode.value
}
type KDLNull struct {
key string
value KDLValue
}
func NewKDLNull(key string) KDLNull {
return KDLNull{key: key, value: KDLValue{Type: KDLNullType}}
}
func (kdlNode KDLNull) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLNull) GetValue() KDLValue {
return kdlNode.value
}
type KDLDefault struct {
key string
value KDLValue
}
func NewKDLDefault(key string) KDLDefault {
return KDLDefault{key: key, value: KDLValue{Type: KDLDefaultType}}
}
func (kdlNode KDLDefault) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLDefault) GetValue() KDLValue {
return kdlNode.value
}
type KDLObjects struct {
key string
value KDLValue
}
func NewKDLObjects(key string, objects []KDLObject) KDLObjects {
return KDLObjects{key: key, value: KDLValue{Objects: objects, Type: KDLObjectsType}}
}
func (kdlNode KDLObjects) GetKey() string {
return kdlNode.key
}
func (kdlNode KDLObjects) GetValue() KDLValue {
return kdlNode.value
}
func (kdlObjs KDLObjects) ToObjMap() KDLObjectsMap {
ret := make(KDLObjectsMap)
for _, obj := range kdlObjs.GetValue().Objects {
ret[obj.GetKey()] = obj
}
return ret
}
func (kdlObjs KDLObjects) ToValueMap() KDLValuesMap {
ret := make(KDLValuesMap)
for _, obj := range kdlObjs.GetValue().Objects {
ret[obj.GetKey()] = obj.GetValue()
}
return ret
}
type KDLObjectsMap map[string]KDLObject
type KDLValuesMap map[string]KDLValue