This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
textwriter.go
359 lines (303 loc) · 7.55 KB
/
textwriter.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
package ion
import (
"encoding/base64"
"fmt"
"io"
"math/big"
"time"
)
// TextWriterOpts defines a set of bit flag options for text writers.
type TextWriterOpts uint8
const (
// TextWriterQuietFinish disables emiting a newline in Finish(). Convenient if you
// know you're only emiting one datagram; dangerous if there's a chance you're going
// to emit another datagram using the same Writer.
TextWriterQuietFinish TextWriterOpts = 1
)
// textWriter is a writer that writes human-readable text
type textWriter struct {
writer
needsSeparator bool
opts TextWriterOpts
}
// NewTextWriter returns a new text writer.
func NewTextWriter(out io.Writer) Writer {
return NewTextWriterOpts(out, 0)
}
// NewTextWriterOpts returns a new text writer with the given options.
func NewTextWriterOpts(out io.Writer, opts TextWriterOpts) Writer {
return &textWriter{
writer: writer{
out: out,
},
opts: opts,
}
}
// WriteNull writes an untyped null.
func (w *textWriter) WriteNull() error {
return w.writeValue("Writer.WriteNull", textNulls[NoType])
}
// WriteNullType writes a typed null.
func (w *textWriter) WriteNullType(t Type) error {
return w.writeValue("Writer.WriteNullType", textNulls[t])
}
// WriteBool writes a boolean value.
func (w *textWriter) WriteBool(val bool) error {
str := "false"
if val {
str = "true"
}
return w.writeValue("Writer.WriteBool", str)
}
// WriteInt writes an integer value.
func (w *textWriter) WriteInt(val int64) error {
return w.writeValue("Writer.WriteInt", fmt.Sprintf("%d", val))
}
// WriteUint writes an unsigned integer value.
func (w *textWriter) WriteUint(val uint64) error {
return w.writeValue("Writer.WriteUint", fmt.Sprintf("%d", val))
}
// WriteBigInt writes a (big) integer value.
func (w *textWriter) WriteBigInt(val *big.Int) error {
return w.writeValue("Writer.WriteBigInt", val.String())
}
// WriteFloat writes a floating-point value.
func (w *textWriter) WriteFloat(val float64) error {
return w.writeValue("Writer.WriteFloat", formatFloat(val))
}
// WriteDecimal writes an arbitrary-precision decimal value.
func (w *textWriter) WriteDecimal(val *Decimal) error {
return w.writeValue("Writer.WriteDecimal", val.String())
}
// WriteTimestamp writes a timestamp.
func (w *textWriter) WriteTimestamp(val time.Time) error {
return w.writeValue("Writer.WriteTimestamp", val.Format(time.RFC3339Nano))
}
// WriteSymbol writes a symbol.
func (w *textWriter) WriteSymbol(val string) error {
if w.err != nil {
return w.err
}
if w.err = w.beginValue("Writer.WriteSymbol"); w.err != nil {
return w.err
}
if w.err = writeSymbol(val, w.out); w.err != nil {
return w.err
}
w.endValue()
return nil
}
// WriteString writes a string.
func (w *textWriter) WriteString(val string) error {
if w.err != nil {
return w.err
}
if w.err = w.beginValue("Writer.WriteString"); w.err != nil {
return w.err
}
if w.err = writeRawChar('"', w.out); w.err != nil {
return w.err
}
if w.err = writeEscapedString(val, w.out); w.err != nil {
return w.err
}
if w.err = writeRawChar('"', w.out); w.err != nil {
return w.err
}
w.endValue()
return nil
}
// WriteClob writes a clob.
func (w *textWriter) WriteClob(val []byte) error {
if w.err != nil {
return w.err
}
if w.err = w.beginValue("Writer.WriteBlob"); w.err != nil {
return w.err
}
if w.err = writeRawString("{{\"", w.out); w.err != nil {
return w.err
}
for _, c := range val {
if c < 32 || c == '\\' || c == '"' || c > 0x7F {
if err := writeEscapedChar(c, w.out); err != nil {
return err
}
} else {
if err := writeRawChar(c, w.out); err != nil {
return err
}
}
}
if w.err = writeRawString("\"}}", w.out); w.err != nil {
return w.err
}
w.endValue()
return nil
}
// WriteBlob writes a blob.
func (w *textWriter) WriteBlob(val []byte) error {
if w.err != nil {
return w.err
}
if w.err = w.beginValue("Writer.WriteBlob"); w.err != nil {
return w.err
}
if w.err = writeRawString("{{", w.out); w.err != nil {
return w.err
}
enc := base64.NewEncoder(base64.StdEncoding, w.out)
enc.Write(val)
if w.err = enc.Close(); w.err != nil {
return w.err
}
if w.err = writeRawString("}}", w.out); w.err != nil {
return w.err
}
w.endValue()
return nil
}
// BeginList begins writing a list.
func (w *textWriter) BeginList() error {
if w.err == nil {
w.err = w.begin("Writer.BeginList", ctxInList, '[')
}
return w.err
}
// EndList finishes writing a list.
func (w *textWriter) EndList() error {
if w.err == nil {
w.err = w.end("Writer.EndList", ctxInList, ']')
}
return w.err
}
// BeginSexp begins writing an s-expression.
func (w *textWriter) BeginSexp() error {
if w.err == nil {
w.err = w.begin("Writer.BeginSexp", ctxInSexp, '(')
}
return w.err
}
// EndSexp finishes writing an s-expression.
func (w *textWriter) EndSexp() error {
if w.err == nil {
w.err = w.end("Writer.EndSexp", ctxInSexp, ')')
}
return w.err
}
// BeginStruct begins writing a struct.
func (w *textWriter) BeginStruct() error {
if w.err == nil {
w.err = w.begin("Writer.BeginStruct", ctxInStruct, '{')
}
return w.err
}
// EndStruct finishes writing a struct.
func (w *textWriter) EndStruct() error {
if w.err == nil {
w.err = w.end("Writer.EndStruct", ctxInStruct, '}')
}
return w.err
}
// Finish finishes writing the current datagram.
func (w *textWriter) Finish() error {
if w.err != nil {
return w.err
}
if w.ctx.peek() != ctxAtTopLevel {
return &UsageError{"Writer.Finish", "not at top level"}
}
if w.opts&TextWriterQuietFinish == 0 {
if w.err = writeRawChar('\n', w.out); w.err != nil {
return w.err
}
w.needsSeparator = false
}
w.clear()
return nil
}
// writeValue writes a stringified value to the output stream.
func (w *textWriter) writeValue(api string, val string) error {
if w.err != nil {
return w.err
}
if w.err = w.beginValue(api); w.err != nil {
return w.err
}
if w.err = writeRawString(val, w.out); w.err != nil {
return w.err
}
w.endValue()
return nil
}
// beginValue begins the process of writing a value, by writing out
// a separator (if needed), field name (if in a struct), and type
// annotations (if any).
func (w *textWriter) beginValue(api string) error {
if w.needsSeparator {
var sep byte
switch w.ctx.peek() {
case ctxInStruct, ctxInList:
sep = ','
case ctxInSexp:
sep = ' '
default:
sep = '\n'
}
if err := writeRawChar(sep, w.out); err != nil {
return err
}
}
if w.inStruct() {
if w.fieldName == "" {
return &UsageError{api, "field name not set"}
}
name := w.fieldName
w.fieldName = ""
if err := writeSymbol(name, w.out); err != nil {
return err
}
if err := writeRawChar(':', w.out); err != nil {
return err
}
}
if len(w.annotations) > 0 {
as := w.annotations
w.annotations = nil
for _, a := range as {
if err := writeSymbol(a, w.out); err != nil {
return err
}
if err := writeRawString("::", w.out); err != nil {
return err
}
}
}
return nil
}
// endValue finishes the process of writing a value.
func (w *textWriter) endValue() {
w.needsSeparator = true
}
// begin starts writing a container of the given type.
func (w *textWriter) begin(api string, t ctx, c byte) error {
if err := w.beginValue(api); err != nil {
return err
}
w.ctx.push(t)
w.needsSeparator = false
return writeRawChar(c, w.out)
}
// end finishes writing a container of the given type
func (w *textWriter) end(api string, t ctx, c byte) error {
if w.ctx.peek() != t {
return &UsageError{api, "not in that kind of container"}
}
if err := writeRawChar(c, w.out); err != nil {
return err
}
w.clear()
w.ctx.pop()
w.endValue()
return nil
}