/
encode.go
440 lines (408 loc) · 9.72 KB
/
encode.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright (c) 2018 Mark K Mueller <github.com/mkmueller>
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package config
import (
"io"
"os"
"fmt"
"sort"
"time"
"bytes"
"errors"
"reflect"
"strconv"
"strings"
)
// The Encoder handles encoding a struct to an io.Writer.
type Encoder struct {
writer io.Writer
previous_key string
options int
v reflect.Value
fileMode os.FileMode
errs []error
}
// NewEncoder accepts a struct or map and returns a new Encoder.
func NewEncoder(x interface{}, options ...int) *Encoder {
rv := reflect.ValueOf(x)
switch rv.Kind() {
case reflect.Ptr:
if rv.Elem().Kind() == reflect.Struct {
rv = rv.Elem()
break
}
panic("Expecting a struct or a map")
case reflect.Map:
break
case reflect.Struct:
break
default:
panic("Expecting a struct or a map")
}
o := &Encoder{v: rv}
if len(options) > 0 {
if !o.allowedOption(options[0]) {
panic("Option not allowed")
}
o.options = options[0]
}
return o
}
func (o *Encoder) allowedOption(option int) bool {
return option == option&(ENCODE_ZERO_VALUES|ENCODE_LOWER_CASE|ENCODE_SNAKE_CASE|OVERWRITE_FILE)
}
// ToFile will encode a struct to the supplied filename. If the file exists,
// it will not be overwritten unless the overwrite options is used.
func (o *Encoder) ToFile(filename string) error {
fi, err := os.Stat(filename)
if err == nil {
// file exists
if fi.IsDir() {
return errors.New("cannot overwrite a directory")
}
if OVERWRITE_FILE != OVERWRITE_FILE&(o.options) {
return errors.New("file already exists")
}
}
fh, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
fi, err := fh.Stat()
fh.Close()
if err == nil {
// Remove file if it's empty
if fi.Size() == 0 {
os.Remove(filename)
}
}
}()
// We don't care if chmod returns an error. Just ignore it.
fh.Chmod(o.fileMode)
return o.ToStream(fh)
}
func Encode(x interface{}, options ...int) ([]byte, error) {
o := NewEncoder(x, options...)
var buf bytes.Buffer
o.writer = &buf
o.encodeTraverseStruct(o.v, 0, "")
return buf.Bytes(), getErrors(o.errs)
}
func EncodeToFile(x interface{}, filename string, options ...int) error {
return NewEncoder(x, options...).ToFile(filename)
}
// ToBytes
func (o *Encoder) ToBytes(bs *[]byte) error {
var buf bytes.Buffer
err := o.ToStream(&buf)
*bs = buf.Bytes()
return err
}
// ToStream
func (o *Encoder) ToStream(w io.Writer) error {
o.writer = w
o.encodeTraverseStruct(o.v, 0, "")
return getErrors(o.errs)
}
func (o *Encoder) appendErr(s string, v interface{}) {
o.errs = append(o.errs, errors.New(fmt.Sprintf(s, v)))
}
func (o *Encoder) encodeTraverseStruct(v1 reflect.Value, depth int, parent_key string) bool {
switch v1.Kind() {
case reflect.Map:
return o.encodeMap(v1, depth, parent_key)
case reflect.Struct:
if isTimeType(v1.Type()) {
return o.encodeTime(v1, depth, parent_key)
}
return o.encodeStruct(v1, depth, parent_key)
default:
if !o.encodeScalar(v1, depth, parent_key) {
o.appendErr("Cannot encode type (%v)", v1.Kind())
return false
}
}
return true
}
func (o *Encoder) encodeTime(v1 reflect.Value, depth int, parent_key string) bool {
if isTimeType(v1.Type()) {
t := v1.Interface().(time.Time)
var dt string
switch {
case isTimeOnly(t):
dt = t.Format(time_fmt)
case isDateOnly(t):
dt = t.Format(date_fmt)
case isDateTime(t):
dt = t.Format(date_time)
case isUTCTime(t):
dt = t.Format(utc_time)
case isUTCDate(t):
dt = t.Format(utc_date)
}
o.write_kv(depth, parent_key, dt)
}
return true
}
func (o *Encoder) encodeScalar(v1 reflect.Value, depth int, parent_key string) bool {
switch v1.Kind() {
case reflect.String:
o.encodeString(v1, depth, parent_key)
case reflect.Bool:
BoolStr := "False"
if v1.Interface().(bool) == true {
BoolStr = "True"
}
if !o.isOption(ENCODE_ZERO_VALUES) && !v1.Interface().(bool) {
break
}
o.write_kv(depth, parent_key, BoolStr)
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
if !o.isOption(ENCODE_ZERO_VALUES) && isZero(v1) {
break
}
o.write_kv(depth, parent_key, v1)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
if !o.isOption(ENCODE_ZERO_VALUES) && isZero(v1) {
break
}
o.write_kv(depth, parent_key, v1)
case reflect.Float32, reflect.Float64:
if !o.isOption(ENCODE_ZERO_VALUES) && isZero(v1) {
break
}
o.write_kv(depth, parent_key, v1)
default:
return false
}
return true
}
func (o *Encoder) encodeString(v1 reflect.Value, depth int, parent_key string) bool {
str := v1.String()
if len(str) > 50 {
if needs_heredoc(str) {
str = output_heredoc(str)
} else {
str = encodeMultiline(parent_key, str)
}
} else {
str = quote(str)
}
if str == "" {
if o.isOption(ENCODE_ZERO_VALUES) {
str = `""`
} else {
return true
}
}
o.write_kv(depth, parent_key, str)
return true
}
func (o *Encoder) encodeMap(v1 reflect.Value, depth int, parent_key string) bool {
last_parent := ""
open__brace := false
keys := v1.MapKeys()
sorted := make([]string, len(keys))
for i, k := range keys {
sorted[i] = k.String()
}
sort.Strings(sorted)
for _, ky := range sorted {
this_key := ky
v := v1.MapIndex(reflect.ValueOf(ky))
if !(o.isOption(ENCODE_ZERO_VALUES) && isZeroStruct(v1)) {
if parent_key != o.previous_key && last_parent != parent_key {
o.previous_key = parent_key
o.write_kv(depth, parent_key, "{")
open__brace = true
last_parent = parent_key
}
o.encodeTraverseStruct(v, depth+1, this_key)
}
}
if open__brace && parent_key != "" {
o.write(depth, "}\n")
open__brace = false
}
return true
}
func (o *Encoder) encodeStruct(v1 reflect.Value, depth int, parent_key string) bool {
last_parent := ""
open__brace := false
for i, n := 0, v1.NumField(); i < n; i++ {
this_key := v1.Type().Field(i).Name
if !isPublic(this_key) {
continue
}
if parent_key != "" {
if !o.isOption(ENCODE_ZERO_VALUES) && isZeroStruct(v1) {
continue
}
if parent_key != o.previous_key && last_parent != parent_key {
o.previous_key = parent_key
o.write_kv(depth, parent_key, "{")
open__brace = true
last_parent = parent_key
}
}
if !o.encodeTraverseStruct(v1.Field(i), depth+1, this_key) {
continue
}
}
if open__brace && parent_key != "" {
o.write(depth, "}\n")
open__brace = false
}
return true
}
func isZero(v reflect.Value) bool {
z := reflect.Zero(v.Type())
return v.Interface() == z.Interface()
}
func isZeroStruct(v reflect.Value) bool {
switch v.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return v.IsNil()
case reflect.Struct:
if isTimeType(v.Type()) {
return isZero(v)
}
r := true
for i := 0; i < v.NumField(); i++ {
if isPublic(v.Type().Field(i).Name) {
r = r && isZeroStruct(v.Field(i))
}
}
return r
}
return isZero(v)
}
// Break long lines at word boundaries
func encodeMultiline(parent_key, str string) string {
var ar []string
var i, n int
width := multi_line_width - (len(parent_key) + 3)
for {
n = i + width
if n >= len(str) {
break
}
x := strings.LastIndex(str[i:n], " ")
if x > -1 {
x += i + 1
n = x
// if we landed on a space, find next non-space
for n < len(str) {
if str[n:n+1] == " " {
break
}
n++
}
}
ar = append(ar, quote(str[i:n]))
i = n
}
ar = append(ar, quote(str[i:]))
indent := strings.Repeat(" ", len(parent_key)+3)
return strings.Join(ar, "\\\n"+indent)
}
func needs_heredoc(str string) bool {
// if string has more than 3 newlines
if strings.Count(str, "\n") > 3 {
return true
}
return false
}
func output_heredoc(str string) string {
indent := ""
code := toUpper(fmt.Sprintf("__%x__", time.Now().Unix()))
var heredoc []string
heredoc = strings.Split(str, "\n")
str = "<<" + code + "\n"
for _, s := range heredoc {
str += indent + s + "\n"
}
str += indent + code
return str
}
func (o *Encoder) write_kv(depth int, key string, v interface{}) {
key = setKeyCase(o.options, key)
o.write(depth, fmt.Sprintf("%s = %v\n", key, v))
}
func (o *Encoder) write(depth int, s string) {
indent := ""
for i := depth; i > 1; i-- {
indent += " "
}
_, err := o.writer.Write([]byte(indent + s))
if err != nil {
o.appendErr("%s", err)
}
}
func (o *Encoder) isOption(opt int) bool {
return opt == opt&o.options
}
func quote(s string) string {
if len(s) == 0 {
return ""
}
q := strconv.QuoteToASCII(s)
l := len(q)
if q[1:l-1] != s {
// return quoted string
return q
} else {
// if we have leading or trailing space, return quoted string
l := len(s)
if s[0:1] == " " || s[l-1:] == " " {
return q
}
}
// just return original string
return s
}
// Horked from unicode package
func toUpper(s string) string {
z := []byte(s)
for i := 0; i < len(z); i++ {
z[i] = upper(z[i])
}
return string(z)
}
// Horked from unicode package
func upper(r byte) byte {
if 'a' <= r && r <= 'z' {
r -= 'a' - 'A'
}
return r
}
func isStructPtr(x interface{}) bool {
v1 := reflect.ValueOf(x)
if v1.Kind().String() == "ptr" {
return v1.Elem().Kind().String() == "struct"
}
return false
}
func isTimeType(v interface{}) bool {
return v == reflect.TypeOf(time.Time{})
}
func isDateOnly(t time.Time) bool {
return !isTimeOffset(t) && t.Format(time_fmt) == "00:00:00"
}
func isTimeOnly(t time.Time) bool {
return !isTimeOffset(t) && t.Format(date_fmt) == "0000-01-01"
}
func isUTCTime(t time.Time) bool {
return isTimeOffset(t) && t.Format(date_fmt) == "0000-01-01"
}
func isUTCDate(t time.Time) bool {
return isTimeOffset(t) && t.Format(date_fmt) != "0000-01-01"
}
func isDateTime(t time.Time) bool {
return !isTimeOffset(t) && !isDateOnly(t) && !isTimeOnly(t)
}
func isTimeOffset(t time.Time) bool {
return t.Format("-0700") != "+0000"
}