-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode.go
192 lines (174 loc) · 4.14 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
package ini
import (
"encoding"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
type encoder struct {
emitter ini_emitter_t
event ini_event_t
out []byte
flow bool
}
func newEncoder() (e *encoder) {
e = &encoder{}
e.must(ini_emitter_initialize(&e.emitter))
ini_emitter_set_output_string(&e.emitter, &e.out)
ini_emitter_set_unicode(&e.emitter, true)
e.must(ini_document_start_event_initialize(&e.event))
e.emit()
return e
}
func (e *encoder) finish() {
e.must(ini_document_start_event_initialize(&e.event))
e.emit()
e.emitter.open_ended = false
}
func (e *encoder) destroy() {
ini_emitter_delete(&e.emitter)
}
func (e *encoder) emit() {
// This will internally delete the e.event value.
if !ini_emitter_emit(&e.emitter, &e.event) && e.event.typ != ini_DOCUMENT_END_EVENT {
e.must(false)
}
}
func (e *encoder) must(ok bool) {
if !ok {
msg := e.emitter.problem
if msg == "" {
msg = "unknown problem generating INI content"
}
failf("%s", msg)
}
}
func (e *encoder) marshal(in reflect.Value) {
if !in.IsValid() {
e.nilv()
return
}
iface := in.Interface()
if m, ok := iface.(Marshaler); ok {
v, err := m.MarshalINI()
if err != nil {
fail(err)
}
if v == nil {
e.nilv()
return
}
in = reflect.ValueOf(v)
} else if m, ok := iface.(encoding.TextMarshaler); ok {
text, err := m.MarshalText()
if err != nil {
fail(err)
}
in = reflect.ValueOf(string(text))
}
switch in.Kind() {
case reflect.Interface:
if in.IsNil() {
e.nilv()
} else {
e.marshal(in.Elem())
}
case reflect.Map:
e.mapv(in)
case reflect.Ptr:
if in.IsNil() {
e.nilv()
} else {
e.marshal(in.Elem())
}
case reflect.String:
e.stringv(in)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if in.Type() == durationType {
e.stringv(reflect.ValueOf(iface.(time.Duration).String()))
} else {
e.intv(in)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
e.uintv(in)
case reflect.Float32, reflect.Float64:
e.floatv(in)
case reflect.Bool:
e.boolv(in)
default:
panic("cannot marshal type: " + in.Type().String())
}
}
func (e *encoder) mapv(in reflect.Value) {
}
// isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
//
// The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
// in YAML 1.2 and by this package, but these should be marshalled quoted for
// the time being for compatibility with other parsers.
func isBase60Float(s string) (result bool) {
// Fast path.
if s == "" {
return false
}
c := s[0]
if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
return false
}
// Do the full match.
return base60float.MatchString(s)
}
// From http://ini.org/type/float.html, except the regular expression there
// is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
func (e *encoder) stringv(in reflect.Value) {
var style ini_scalar_style_t
s := in.String()
if isBase60Float(s) {
style = ini_DOUBLE_QUOTED_SCALAR_STYLE
} else if strings.Contains(s, "\n") {
style = ini_PLAIN_SCALAR_STYLE
} else {
style = ini_PLAIN_SCALAR_STYLE
}
e.emitNode(s, style)
}
func (e *encoder) boolv(in reflect.Value) {
var s string
if in.Bool() {
s = "true"
} else {
s = "false"
}
e.emitNode(s, ini_PLAIN_SCALAR_STYLE)
}
func (e *encoder) intv(in reflect.Value) {
s := strconv.FormatInt(in.Int(), 10)
e.emitNode(s, ini_PLAIN_SCALAR_STYLE)
}
func (e *encoder) uintv(in reflect.Value) {
s := strconv.FormatUint(in.Uint(), 10)
e.emitNode(s, ini_PLAIN_SCALAR_STYLE)
}
func (e *encoder) floatv(in reflect.Value) {
// FIXME: Handle 64 bits here.
s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
switch s {
case "+Inf":
s = ".inf"
case "-Inf":
s = "-.inf"
case "NaN":
s = ".nan"
}
e.emitNode(s, ini_PLAIN_SCALAR_STYLE)
}
func (e *encoder) nilv() {
e.emitNode("null", ini_PLAIN_SCALAR_STYLE)
}
func (e *encoder) emitNode(value string, style ini_scalar_style_t) {
e.must(ini_scalar_event_initialize(&e.event, []byte(value), style))
e.emit()
}