-
Notifications
You must be signed in to change notification settings - Fork 2
/
kv.go
158 lines (135 loc) · 3.31 KB
/
kv.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
package log
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
type encodeState struct {
buffer *bytes.Buffer
writeBegan bool
}
// Marshal will encode any structure or map in a k=v format
func Marshal(data interface{}) ([]byte, error) {
e := &encodeState{}
err := e.marshal("", data)
return e.buffer.Bytes(), err
}
// MarshalString is a proxy method for Marshal that drops any error and always return a string
func MarshalString(data interface{}) string {
e := &encodeState{}
e.marshal("", data)
return e.buffer.String()
}
func (e *encodeState) marshal(prefix string, data interface{}) error {
if e.writeBegan {
e.writeBegan = false
}
if e.buffer == nil {
e.buffer = bytes.NewBuffer(nil)
}
value := reflect.ValueOf(data)
if value.Kind() == reflect.Invalid {
return nil
}
if value.Kind() == reflect.Ptr {
return e.marshalPtr(prefix, value.Interface())
}
if value.Kind() == reflect.Struct {
return e.marshalStruct(prefix, value.Interface())
}
if value.Kind() == reflect.Map {
return e.marshalMap(prefix, value.Interface())
}
_, err := e.buffer.WriteString(fmt.Sprint(data))
return err
}
func (e *encodeState) writeTab() error {
if !e.writeBegan {
return nil
}
_, err := e.buffer.WriteRune('\t')
return err
}
func (e *encodeState) marshalPtr(prefix string, data interface{}) error {
value := reflect.ValueOf(data)
value = value.Elem()
if value.Kind() == reflect.Struct {
return e.marshalStruct(prefix, value.Interface())
}
return fmt.Errorf("don't know how to marshal this pointer")
}
func (e *encodeState) marshalStruct(prefix string, data interface{}) error {
ref := reflect.TypeOf(data)
value := reflect.ValueOf(data)
for i := 0; i < value.NumField(); i++ {
fieldType := ref.Field(i)
// Ignore unexported fields
if fieldType.PkgPath != "" {
continue
}
// Get the key for this field
var key string
if key = fieldType.Tag.Get("kv"); key == "-" {
continue
} else if key == "" {
key = fieldType.Name
}
e.writeTab()
field := value.Field(i)
if field.Kind() == reflect.Ptr || field.Kind() == reflect.Struct || field.Kind() == reflect.Map {
if len(key) > 0 {
key = key + "."
}
err := e.marshal(key, field.Interface())
if err != nil {
return err
}
continue
}
value := stringify(field.Interface())
if _, err := e.buffer.WriteString(fmt.Sprintf("%s%s=%s", prefix, key, value)); err != nil {
return err
}
e.writeBegan = true
}
return nil
}
func (e *encodeState) marshalMap(prefix string, data interface{}) error {
value := reflect.ValueOf(data)
keys := value.MapKeys()
for _, v := range keys {
e.writeTab()
mapKey, _ := strconv.Unquote(stringify(v.Interface()))
mapValue := stringify(value.MapIndex(v).Interface())
if _, err := e.buffer.WriteString(fmt.Sprintf("%s%s=%s", prefix, mapKey, mapValue)); err != nil {
return err
}
e.writeBegan = true
}
return nil
}
func stringify(data interface{}) string {
value := ""
switch t := data.(type) {
case bool:
value = strconv.FormatBool(t)
case rune:
value = strconv.QuoteRune(t)
case time.Time:
value = t.UTC().Format(time.RFC3339)
case int:
value = strconv.Itoa(int(t))
case fmt.Stringer:
value = t.String()
case string:
value = strconv.Quote(t)
case []string:
value = strconv.Quote(strings.Join(t, ";"))
default:
value = "n/a"
}
return value
}