-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshal.go
286 lines (239 loc) · 5.64 KB
/
marshal.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
279
280
281
282
283
284
285
286
package sfv
import (
"encoding/base64"
"fmt"
"math"
"reflect"
"strconv"
"strings"
)
func Marshal(v interface{}) (string, error) {
var w strings.Builder
switch v := v.(type) {
case Item:
if err := marshalItem(&w, v); err != nil {
return "", err
}
case List:
if err := marshalList(&w, v); err != nil {
return "", err
}
case Dictionary:
if err := marshalDictionary(&w, v); err != nil {
return "", err
}
case bool, int, int8, int16, int32, int64, uint, uint8, uint16,
uint32, uint64, float32, float64, string, []byte:
item, err := unbindItem(reflect.ValueOf(v))
if err != nil {
return "", err
}
if err := marshalItem(&w, item); err != nil {
return "", err
}
default:
val := reflect.ValueOf(v)
switch val.Kind() {
case reflect.Struct:
item, err := unbindItem(val)
if err != nil {
return "", err
}
if err := marshalItem(&w, item); err != nil {
return "", err
}
case reflect.Slice:
list, err := unbindList(val)
if err != nil {
return "", err
}
if err := marshalList(&w, list); err != nil {
return "", err
}
case reflect.Map:
dict, err := unbindDictionary(val)
if err != nil {
return "", err
}
if err := marshalDictionary(&w, dict); err != nil {
return "", err
}
default:
return "", fmt.Errorf("unsupported type: %T", v)
}
}
return w.String(), nil
}
func marshalItem(w *strings.Builder, v Item) error {
if err := marshalBareItem(w, v.BareItem); err != nil {
return err
}
if err := marshalParams(w, v.Params); err != nil {
return err
}
return nil
}
func marshalList(w *strings.Builder, v []Member) error {
for i, m := range v {
if m.IsItem {
if err := marshalItem(w, m.Item); err != nil {
return err
}
} else {
if err := marshalInnerList(w, m.InnerList); err != nil {
return err
}
}
if i != len(v)-1 {
fmt.Fprint(w, ", ")
}
}
return nil
}
func marshalDictionary(w *strings.Builder, v Dictionary) error {
for i, k := range v.Keys {
if err := marshalKey(w, k); err != nil {
return err
}
if v.Map[k].IsItem && v.Map[k].Item.BareItem.Type == BareItemTypeBoolean && v.Map[k].Item.BareItem.Boolean == true {
if err := marshalParams(w, v.Map[k].Item.Params); err != nil {
return err
}
} else {
fmt.Fprint(w, "=")
if v.Map[k].IsItem {
if err := marshalItem(w, v.Map[k].Item); err != nil {
return err
}
} else {
if err := marshalInnerList(w, v.Map[k].InnerList); err != nil {
return err
}
}
}
if i != len(v.Keys)-1 {
fmt.Fprint(w, ", ")
}
}
return nil
}
func marshalInnerList(w *strings.Builder, v InnerList) error {
fmt.Fprint(w, "(")
for i, m := range v.Items {
if err := marshalItem(w, m); err != nil {
return err
}
if i != len(v.Items)-1 {
fmt.Fprintf(w, " ")
}
}
fmt.Fprint(w, ")")
if err := marshalParams(w, v.Params); err != nil {
return err
}
return nil
}
func marshalBareItem(w *strings.Builder, v BareItem) error {
switch v.Type {
case BareItemTypeDecimal:
return marshalDecimal(w, v.Decimal)
case BareItemTypeInteger:
return marshalInteger(w, v.Integer)
case BareItemTypeString:
return marshalString(w, v.String)
case BareItemTypeToken:
return marshalToken(w, v.Token)
case BareItemTypeBinary:
return marshalByteSequence(w, v.Binary)
case BareItemTypeBoolean:
return marshalBoolean(w, v.Boolean)
default:
return fmt.Errorf("unsupported bare item type: %v", v)
}
}
func marshalDecimal(w *strings.Builder, v float64) error {
if int64(v) < -999_999_999_999 || int64(v) > 999_999_999_999 {
return fmt.Errorf("decimal out of range: %v", v)
}
// limit to three digits of precision past the decimal
v = math.RoundToEven(v*1000) / 1000
s := strconv.FormatFloat(v, 'f', -1, 64)
if !strings.ContainsRune(s, '.') {
s += ".0"
}
fmt.Fprint(w, s)
return nil
}
func marshalInteger(w *strings.Builder, v int64) error {
if v < -999_999_999_999_999 || v > 999_999_999_999_999 {
return fmt.Errorf("integer out of range: %v", v)
}
fmt.Fprintf(w, "%d", v)
return nil
}
func marshalString(w *strings.Builder, v string) error {
fmt.Fprint(w, "\"")
for _, c := range v {
if !isVisible(byte(c)) && c != ' ' {
return fmt.Errorf("invalid char in string: %c", c)
}
if c == '\\' || c == '"' {
fmt.Fprintf(w, "\\%s", string(c))
} else {
fmt.Fprintf(w, "%s", string(c))
}
}
fmt.Fprint(w, "\"")
return nil
}
func marshalToken(w *strings.Builder, v string) error {
for i, c := range v {
if i == 0 && !isAlpha(byte(c)) && c != '*' {
return fmt.Errorf("invalid first char in token: %v", c)
}
if i != 0 && !isTChar(byte(c)) && c != ':' && c != '/' {
return fmt.Errorf("invalid char in token: %v", c)
}
}
fmt.Fprintf(w, "%s", string(v))
return nil
}
func marshalByteSequence(w *strings.Builder, v []byte) error {
fmt.Fprintf(w, ":%s:", base64.StdEncoding.EncodeToString(v))
return nil
}
func marshalBoolean(w *strings.Builder, v bool) error {
n := 0
if v {
n = 1
}
fmt.Fprintf(w, "?%d", n)
return nil
}
func marshalParams(w *strings.Builder, v Params) error {
for _, k := range v.Keys {
fmt.Fprintf(w, ";")
if err := marshalKey(w, k); err != nil {
return err
}
if !v.Map[k].isBoolTrue() {
fmt.Fprint(w, "=")
if err := marshalBareItem(w, v.Map[k]); err != nil {
return err
}
}
}
return nil
}
func marshalKey(w *strings.Builder, v string) error {
for i, c := range v {
if i == 0 && !isLCAlpha(byte(c)) && c != '*' {
return fmt.Errorf("invalid first char in key: %c", c)
}
if !isLCAlpha(byte(c)) && !isDigit(byte(c)) && c != '_' && c != '-' && c != '.' && c != '*' {
return fmt.Errorf("invalid char in key: %c", c)
}
}
fmt.Fprintf(w, "%s", v)
return nil
}