-
Notifications
You must be signed in to change notification settings - Fork 34
/
encode.go
304 lines (266 loc) · 8.01 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
package fixedwidth
import (
"bufio"
"bytes"
"encoding"
"io"
"reflect"
"strconv"
"strings"
)
// Marshal returns the fixed-width encoding of v.
//
// v must be an encodable type or a slice of an encodable
// type. If v is a slice, each item will be treated as a
// line. If v is a single encodable type, a single line
// will be encoded.
//
// In order for a type to be encodable, it must implement
// the encoding.TextMarshaler interface or be based on one
// of the following builtin types: string, int, int64,
// int32, int16, int8, uint, uint64, uint32, uint16,
// uint8, float64, float32, bool, or struct. Pointers to
// encodable types and interfaces containing encodable
// types are also encodable.
//
// nil pointers and interfaces will be omitted. zero vales
// will be encoded normally.
//
// A struct is encoded to a single slice of bytes. Each
// field in a struct will be encoded and placed at the
// position defined by its struct tags. The tags should be
// formatted as `fixed:"{startPos},{endPos}"`. Positions
// start at 1. The interval is inclusive. Fields without
// tags and Fields of an un-encodable type are ignored.
//
// If the encoded value of a field is longer than the
// length of the position interval, the overflow is
// truncated.
func Marshal(v interface{}) ([]byte, error) {
buff := bytes.NewBuffer(nil)
err := NewEncoder(buff).Encode(v)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
// MarshalInvalidTypeError describes an invalid type being marshaled.
type MarshalInvalidTypeError struct {
typeName string
}
func (e *MarshalInvalidTypeError) Error() string {
return "fixedwidth: cannot marshal unknown Type " + e.typeName
}
// An Encoder writes fixed-width formatted data to an output
// stream.
type Encoder struct {
w *bufio.Writer
lineTerminator []byte
useCodepointIndices bool
lastType reflect.Type
lastValueEncoder valueEncoder
}
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{
w: bufio.NewWriter(w),
lineTerminator: []byte("\n"),
}
}
// SetLineTerminator sets the character(s) that will be used to terminate lines.
//
// The default value is "\n".
func (e *Encoder) SetLineTerminator(lineTerminator []byte) {
e.lineTerminator = lineTerminator
}
// SetUseCodepointIndices configures `Encoder` on whether the indices in the
// `fixedwidth` struct tags are expressed in terms of bytes (the default
// behavior) or in terms of UTF-8 decoded codepoints.
func (e *Encoder) SetUseCodepointIndices(use bool) {
e.useCodepointIndices = use
}
// Encode writes the fixed-width encoding of v to the
// stream.
// See the documentation for Marshal for details about
// encoding behavior.
func (e *Encoder) Encode(i interface{}) (err error) {
if i == nil {
return nil
}
// check to see if i should be encoded into multiple lines
v := reflect.ValueOf(i)
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() == reflect.Slice {
// encode each slice element to a line
err = e.writeLines(v)
} else {
// this is a single object so encode the original vale to a line
err = e.writeLine(reflect.ValueOf(i))
}
if err != nil {
return err
}
return e.w.Flush()
}
func (e *Encoder) writeLines(v reflect.Value) error {
for i := 0; i < v.Len(); i++ {
err := e.writeLine(v.Index(i))
if err != nil {
return err
}
if i != v.Len()-1 {
_, err := e.w.Write(e.lineTerminator)
if err != nil {
return err
}
}
}
return nil
}
func (e *Encoder) writeLine(v reflect.Value) (err error) {
t := v.Type()
encoder := e.lastValueEncoder
if e.lastType != t {
e.lastType = t
e.lastValueEncoder = newValueEncoder(t, e.useCodepointIndices)
encoder = e.lastValueEncoder
}
b, err := encoder(v)
if err != nil {
return err
}
_, err = e.w.WriteString(b.data)
return err
}
type valueEncoder func(v reflect.Value) (rawValue, error)
func newValueEncoder(t reflect.Type, useCodepointIndices bool) valueEncoder {
if t == nil {
return nilEncoder
}
if t.Implements(reflect.TypeOf(new(encoding.TextMarshaler)).Elem()) {
return textMarshalerEncoder(useCodepointIndices)
}
switch t.Kind() {
case reflect.Ptr, reflect.Interface:
return ptrInterfaceEncoder(useCodepointIndices)
case reflect.Struct:
return structEncoder(useCodepointIndices)
case reflect.String:
return stringEncoder(useCodepointIndices)
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return intEncoder
case reflect.Float64:
return floatEncoder(2, 64)
case reflect.Float32:
return floatEncoder(2, 32)
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
return uintEncoder
case reflect.Bool:
return boolEncoder
}
return unknownTypeEncoder(t)
}
func (ve valueEncoder) Write(b *lineBuilder, v reflect.Value, spec fieldSpec) error {
format := spec.format
startIndex := spec.startPos - 1
value, err := ve(v)
if err != nil {
return err
}
if value.len() < spec.len() {
switch {
case spec.format.alignment == right:
padding := strings.Repeat(string(format.padChar), spec.len()-value.len())
b.WriteASCII(startIndex, padding)
b.WriteValue(startIndex+len(padding), value)
return nil
// The second case in this block is a special case to maintain backward
// compatibility. In previous versions of the library, only len(value) bytes were
// written to dst. This means overlapping intervals can, in effect, be used to
// coalesce a value.
case format.alignment == left, format.alignment == defaultAlignment && format.padChar != ' ':
padding := strings.Repeat(string(format.padChar), spec.len()-value.len())
b.WriteValue(startIndex, value)
b.WriteASCII(startIndex+value.len(), padding)
return nil
}
}
if value.len() > spec.len() {
// If the value is too long it needs to be trimmed.
// TODO: Add strict mode that returns in this case.
value, err = value.slice(0, spec.len()-1)
if err != nil {
return err
}
}
b.WriteValue(startIndex, value)
return nil
}
func structEncoder(useCodepointIndices bool) valueEncoder {
return func(v reflect.Value) (rawValue, error) {
ss := cachedStructSpec(v.Type())
// Add a 10% headroom to the builder when codepoint indices are being used.
c := ss.ll
if useCodepointIndices {
c = int(1.1*float64(ss.ll)) + 1
}
b := newLineBuilder(ss.ll, c, ' ')
for i, spec := range ss.fieldSpecs {
if !spec.ok {
continue
}
enc := spec.getEncoder(useCodepointIndices)
err := enc.Write(b, v.Field(i), spec)
if err != nil {
return rawValue{}, err
}
}
return b.AsRawValue(), nil
}
}
func textMarshalerEncoder(useCodepointIndices bool) valueEncoder {
return func(v reflect.Value) (rawValue, error) {
txt, err := v.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil {
return rawValue{}, err
}
return newRawValue(string(txt), useCodepointIndices)
}
}
func ptrInterfaceEncoder(useCodepointIndices bool) valueEncoder {
return func(v reflect.Value) (rawValue, error) {
if v.IsNil() {
return nilEncoder(v)
}
return newValueEncoder(v.Elem().Type(), useCodepointIndices)(v.Elem())
}
}
func stringEncoder(useCodepointIndices bool) valueEncoder {
return func(v reflect.Value) (rawValue, error) {
return newRawValue(v.String(), useCodepointIndices)
}
}
func intEncoder(v reflect.Value) (rawValue, error) {
return newRawValue(strconv.Itoa(int(v.Int())), false)
}
func floatEncoder(perc, bitSize int) valueEncoder {
return func(v reflect.Value) (rawValue, error) {
return newRawValue(strconv.FormatFloat(v.Float(), 'f', perc, bitSize), false)
}
}
func boolEncoder(v reflect.Value) (rawValue, error) {
return newRawValue(strconv.FormatBool(v.Bool()), false)
}
func nilEncoder(_ reflect.Value) (rawValue, error) {
return rawValue{}, nil
}
func unknownTypeEncoder(t reflect.Type) valueEncoder {
return func(value reflect.Value) (rawValue, error) {
return rawValue{}, &MarshalInvalidTypeError{typeName: t.Name()}
}
}
func uintEncoder(v reflect.Value) (rawValue, error) {
return newRawValue(strconv.FormatUint(v.Uint(), 10), false)
}