File: 0001-Expose-Encoder.Canonical-and-MarshalCanonical-Handle.patch

package info (click to toggle)
golang-github-docker-go 1.5.2~git20160303.d30aec9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 444 kB
  • sloc: makefile: 23
file content (509 lines) | stat: -rw-r--r-- 14,854 bytes parent folder | download | duplicates (3)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
From ee929ab88d09e61a93cd041730adfedb1aa2c510 Mon Sep 17 00:00:00 2001
From: Tibor Vass <teabee89@gmail.com>
Date: Fri, 7 Aug 2015 12:49:35 -0700
Subject: [PATCH 1/3] Expose Encoder.Canonical() and MarshalCanonical() Handles
 lexicographic order in struct fields, rejects floating numbers and handles
 strings as defined in http://wiki.laptop.org/go/Canonical_JSON except for
 unicode normalization. IOW, only escaping allowed is " and \.

The notion of Canonical JSON is only for an Encoder.

Signed-off-by: Tibor Vass <teabee89@gmail.com>
---
 canonical/json/decode.go      |   3 +-
 canonical/json/encode.go      | 141 ++++++++++++++++++++++++++++--------------
 canonical/json/encode_test.go |  50 +++++++++++++++
 canonical/json/stream.go      |  27 +++++---
 4 files changed, 162 insertions(+), 59 deletions(-)

diff --git a/canonical/json/decode.go b/canonical/json/decode.go
index 705bc2e..51b4ffe 100644
--- a/canonical/json/decode.go
+++ b/canonical/json/decode.go
@@ -174,6 +174,7 @@ type decodeState struct {
 	nextscan   scanner // for calls to nextValue
 	savedError error
 	useNumber  bool
+	canonical  bool
 }
 
 // errPhase is used for errors that should not happen unless
@@ -557,7 +558,7 @@ func (d *decodeState) object(v reflect.Value) {
 			subv = mapElem
 		} else {
 			var f *field
-			fields := cachedTypeFields(v.Type())
+			fields := cachedTypeFields(v.Type(), false)
 			for i := range fields {
 				ff := &fields[i]
 				if bytes.Equal(ff.nameBytes, key) {
diff --git a/canonical/json/encode.go b/canonical/json/encode.go
index fca2a09..aaa79c2 100644
--- a/canonical/json/encode.go
+++ b/canonical/json/encode.go
@@ -131,12 +131,7 @@ import (
 // an infinite recursion.
 //
 func Marshal(v interface{}) ([]byte, error) {
-	e := &encodeState{}
-	err := e.marshal(v)
-	if err != nil {
-		return nil, err
-	}
-	return e.Bytes(), nil
+	return marshal(v, false)
 }
 
 // MarshalIndent is like Marshal but applies Indent to format the output.
@@ -153,6 +148,21 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
 	return buf.Bytes(), nil
 }
 
+// MarshalCanonical is like Marshal but encodes into Canonical JSON.
+// Read more at: http://wiki.laptop.org/go/Canonical_JSON
+func MarshalCanonical(v interface{}) ([]byte, error) {
+	return marshal(v, true)
+}
+
+func marshal(v interface{}, canonical bool) ([]byte, error) {
+	e := &encodeState{canonical: canonical}
+	err := e.marshal(v)
+	if err != nil {
+		return nil, err
+	}
+	return e.Bytes(), nil
+}
+
 // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
 // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
 // so that the JSON will be safe to embed inside HTML <script> tags.
@@ -242,17 +252,19 @@ var hex = "0123456789abcdef"
 type encodeState struct {
 	bytes.Buffer // accumulated output
 	scratch      [64]byte
+	canonical    bool
 }
 
 var encodeStatePool sync.Pool
 
-func newEncodeState() *encodeState {
+func newEncodeState(canonical bool) *encodeState {
 	if v := encodeStatePool.Get(); v != nil {
 		e := v.(*encodeState)
 		e.Reset()
+		e.canonical = canonical
 		return e
 	}
-	return new(encodeState)
+	return &encodeState{canonical: canonical}
 }
 
 func (e *encodeState) marshal(v interface{}) (err error) {
@@ -296,7 +308,7 @@ func isEmptyValue(v reflect.Value) bool {
 }
 
 func (e *encodeState) reflectValue(v reflect.Value) {
-	valueEncoder(v)(e, v, false)
+	e.valueEncoder(v)(e, v, false)
 }
 
 type encoderFunc func(e *encodeState, v reflect.Value, quoted bool)
@@ -306,14 +318,14 @@ var encoderCache struct {
 	m map[reflect.Type]encoderFunc
 }
 
-func valueEncoder(v reflect.Value) encoderFunc {
+func (e *encodeState) valueEncoder(v reflect.Value) encoderFunc {
 	if !v.IsValid() {
 		return invalidValueEncoder
 	}
-	return typeEncoder(v.Type())
+	return e.typeEncoder(v.Type())
 }
 
-func typeEncoder(t reflect.Type) encoderFunc {
+func (e *encodeState) typeEncoder(t reflect.Type) encoderFunc {
 	encoderCache.RLock()
 	f := encoderCache.m[t]
 	encoderCache.RUnlock()
@@ -339,7 +351,7 @@ func typeEncoder(t reflect.Type) encoderFunc {
 
 	// Compute fields without lock.
 	// Might duplicate effort but won't hold other computations back.
-	f = newTypeEncoder(t, true)
+	f = e.newTypeEncoder(t, true)
 	wg.Done()
 	encoderCache.Lock()
 	encoderCache.m[t] = f
@@ -354,13 +366,13 @@ var (
 
 // newTypeEncoder constructs an encoderFunc for a type.
 // The returned encoder only checks CanAddr when allowAddr is true.
-func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
+func (e *encodeState) newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
 	if t.Implements(marshalerType) {
 		return marshalerEncoder
 	}
 	if t.Kind() != reflect.Ptr && allowAddr {
 		if reflect.PtrTo(t).Implements(marshalerType) {
-			return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
+			return newCondAddrEncoder(addrMarshalerEncoder, e.newTypeEncoder(t, false))
 		}
 	}
 
@@ -369,7 +381,7 @@ func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
 	}
 	if t.Kind() != reflect.Ptr && allowAddr {
 		if reflect.PtrTo(t).Implements(textMarshalerType) {
-			return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
+			return newCondAddrEncoder(addrTextMarshalerEncoder, e.newTypeEncoder(t, false))
 		}
 	}
 
@@ -389,15 +401,15 @@ func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
 	case reflect.Interface:
 		return interfaceEncoder
 	case reflect.Struct:
-		return newStructEncoder(t)
+		return e.newStructEncoder(t)
 	case reflect.Map:
-		return newMapEncoder(t)
+		return e.newMapEncoder(t)
 	case reflect.Slice:
-		return newSliceEncoder(t)
+		return e.newSliceEncoder(t)
 	case reflect.Array:
-		return newArrayEncoder(t)
+		return e.newArrayEncoder(t)
 	case reflect.Ptr:
-		return newPtrEncoder(t)
+		return e.newPtrEncoder(t)
 	default:
 		return unsupportedTypeEncoder
 	}
@@ -511,7 +523,7 @@ type floatEncoder int // number of bits
 
 func (bits floatEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
 	f := v.Float()
-	if math.IsInf(f, 0) || math.IsNaN(f) {
+	if math.IsInf(f, 0) || math.IsNaN(f) || (e.canonical && math.Floor(f) != f) {
 		e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
 	}
 	b := strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits))
@@ -586,14 +598,14 @@ func (se *structEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
 	e.WriteByte('}')
 }
 
-func newStructEncoder(t reflect.Type) encoderFunc {
-	fields := cachedTypeFields(t)
+func (e *encodeState) newStructEncoder(t reflect.Type) encoderFunc {
+	fields := cachedTypeFields(t, e.canonical)
 	se := &structEncoder{
 		fields:    fields,
 		fieldEncs: make([]encoderFunc, len(fields)),
 	}
 	for i, f := range fields {
-		se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index))
+		se.fieldEncs[i] = e.typeEncoder(typeByIndex(t, f.index))
 	}
 	return se.encode
 }
@@ -621,11 +633,11 @@ func (me *mapEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
 	e.WriteByte('}')
 }
 
-func newMapEncoder(t reflect.Type) encoderFunc {
+func (e *encodeState) newMapEncoder(t reflect.Type) encoderFunc {
 	if t.Key().Kind() != reflect.String {
 		return unsupportedTypeEncoder
 	}
-	me := &mapEncoder{typeEncoder(t.Elem())}
+	me := &mapEncoder{e.typeEncoder(t.Elem())}
 	return me.encode
 }
 
@@ -664,12 +676,12 @@ func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
 	se.arrayEnc(e, v, false)
 }
 
-func newSliceEncoder(t reflect.Type) encoderFunc {
+func (e *encodeState) newSliceEncoder(t reflect.Type) encoderFunc {
 	// Byte slices get special treatment; arrays don't.
 	if t.Elem().Kind() == reflect.Uint8 {
 		return encodeByteSlice
 	}
-	enc := &sliceEncoder{newArrayEncoder(t)}
+	enc := &sliceEncoder{e.newArrayEncoder(t)}
 	return enc.encode
 }
 
@@ -689,8 +701,8 @@ func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
 	e.WriteByte(']')
 }
 
-func newArrayEncoder(t reflect.Type) encoderFunc {
-	enc := &arrayEncoder{typeEncoder(t.Elem())}
+func (e *encodeState) newArrayEncoder(t reflect.Type) encoderFunc {
+	enc := &arrayEncoder{e.typeEncoder(t.Elem())}
 	return enc.encode
 }
 
@@ -706,8 +718,8 @@ func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
 	pe.elemEnc(e, v.Elem(), quoted)
 }
 
-func newPtrEncoder(t reflect.Type) encoderFunc {
-	enc := &ptrEncoder{typeEncoder(t.Elem())}
+func (e *encodeState) newPtrEncoder(t reflect.Type) encoderFunc {
+	enc := &ptrEncoder{e.typeEncoder(t.Elem())}
 	return enc.encode
 }
 
@@ -788,9 +800,11 @@ func (e *encodeState) string(s string) (int, error) {
 	start := 0
 	for i := 0; i < len(s); {
 		if b := s[i]; b < utf8.RuneSelf {
-			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
-				i++
-				continue
+			if b != '\\' && b != '"' {
+				if e.canonical || (0x20 <= b && b != '<' && b != '>' && b != '&') {
+					i++
+					continue
+				}
 			}
 			if start < i {
 				e.WriteString(s[start:i])
@@ -821,6 +835,10 @@ func (e *encodeState) string(s string) (int, error) {
 			start = i
 			continue
 		}
+		if e.canonical {
+			i++
+			continue
+		}
 		c, size := utf8.DecodeRuneInString(s[i:])
 		if c == utf8.RuneError && size == 1 {
 			if start < i {
@@ -864,9 +882,11 @@ func (e *encodeState) stringBytes(s []byte) (int, error) {
 	start := 0
 	for i := 0; i < len(s); {
 		if b := s[i]; b < utf8.RuneSelf {
-			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
-				i++
-				continue
+			if b != '\\' && b != '"' {
+				if e.canonical || (0x20 <= b && b != '<' && b != '>' && b != '&') {
+					i++
+					continue
+				}
 			}
 			if start < i {
 				e.Write(s[start:i])
@@ -897,6 +917,10 @@ func (e *encodeState) stringBytes(s []byte) (int, error) {
 			start = i
 			continue
 		}
+		if e.canonical {
+			i++
+			continue
+		}
 		c, size := utf8.DecodeRune(s[i:])
 		if c == utf8.RuneError && size == 1 {
 			if start < i {
@@ -1108,10 +1132,7 @@ func typeFields(t reflect.Type) []field {
 		}
 	}
 
-	fields = out
-	sort.Sort(byIndex(fields))
-
-	return fields
+	return out
 }
 
 // dominantField looks through the fields, all of which are known to
@@ -1152,16 +1173,29 @@ func dominantField(fields []field) (field, bool) {
 	return fields[0], true
 }
 
+type fields struct {
+	byName  []field
+	byIndex []field
+}
+
 var fieldCache struct {
 	sync.RWMutex
-	m map[reflect.Type][]field
+	m map[reflect.Type]*fields
 }
 
 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
-func cachedTypeFields(t reflect.Type) []field {
+func cachedTypeFields(t reflect.Type, canonical bool) []field {
 	fieldCache.RLock()
-	f := fieldCache.m[t]
+	x := fieldCache.m[t]
 	fieldCache.RUnlock()
+
+	var f []field
+	if x != nil {
+		if canonical {
+			f = x.byName
+		}
+		f = x.byIndex
+	}
 	if f != nil {
 		return f
 	}
@@ -1172,12 +1206,23 @@ func cachedTypeFields(t reflect.Type) []field {
 	if f == nil {
 		f = []field{}
 	}
+	if !canonical {
+		sort.Sort(byIndex(f))
+	}
 
 	fieldCache.Lock()
 	if fieldCache.m == nil {
-		fieldCache.m = map[reflect.Type][]field{}
+		fieldCache.m = map[reflect.Type]*fields{}
 	}
-	fieldCache.m[t] = f
+	x = fieldCache.m[t]
 	fieldCache.Unlock()
+	if x == nil {
+		x = new(fields)
+	}
+	if canonical {
+		x.byName = f
+	} else {
+		x.byIndex = f
+	}
 	return f
 }
diff --git a/canonical/json/encode_test.go b/canonical/json/encode_test.go
index 7abfa85..87c697e 100644
--- a/canonical/json/encode_test.go
+++ b/canonical/json/encode_test.go
@@ -6,6 +6,7 @@ package json
 
 import (
 	"bytes"
+	"fmt"
 	"math"
 	"reflect"
 	"testing"
@@ -530,3 +531,52 @@ func TestEncodeString(t *testing.T) {
 		}
 	}
 }
+
+type CanonicalTestStruct struct {
+	S string
+	F float64
+	I int
+	E *CanonicalTestStruct
+}
+
+func (s *CanonicalTestStruct) String() string {
+	var e interface{} = s.E
+	if s.E == nil {
+		e = "nil"
+	}
+	return fmt.Sprintf("{S:%q F:%v I:%v E:%v}", s.S, s.F, s.I, e)
+}
+
+var encodeCanonicalTests = []struct {
+	in        interface{}
+	out       string
+	expectErr bool
+}{
+	{nil, `null`, false},
+	{&CanonicalTestStruct{}, `{"E":null,"F":0,"I":0,"S":""}`, false},
+	{&CanonicalTestStruct{F: 1.0}, `{"E":null,"F":1,"I":0,"S":""}`, false},
+	// error out on floating numbers
+	{&CanonicalTestStruct{F: 1.2}, ``, true},
+	{&CanonicalTestStruct{S: "foo", E: &CanonicalTestStruct{I: 42}}, `{"E":{"E":null,"F":0,"I":42,"S":""},"F":0,"I":0,"S":"foo"}`, false},
+	// only escape \ and " and keep any other character as-is
+	{"\u0090 \t \\ \n \"", "\"\u0090 \t \\\\ \n \\\"\"", false},
+}
+
+func TestEncodeCanonicalStruct(t *testing.T) {
+	for _, tt := range encodeCanonicalTests {
+		b, err := MarshalCanonical(tt.in)
+		if err != nil {
+			if !tt.expectErr {
+				t.Errorf("MarshalCanonical(%#v) = error(%v), want %s", tt.in, err, tt.out)
+			}
+			continue
+		} else if tt.expectErr {
+			t.Errorf("MarshalCanonical(%#v) expects an error", tt.in)
+			continue
+		}
+		out := string(b)
+		if out != tt.out {
+			t.Errorf("MarshalCanonical(%#v) = %q, want %q", tt.in, out, tt.out)
+		}
+	}
+}
diff --git a/canonical/json/stream.go b/canonical/json/stream.go
index 9566eca..8905550 100644
--- a/canonical/json/stream.go
+++ b/canonical/json/stream.go
@@ -138,8 +138,9 @@ func nonSpace(b []byte) bool {
 
 // An Encoder writes JSON objects to an output stream.
 type Encoder struct {
-	w   io.Writer
-	err error
+	w         io.Writer
+	err       error
+	canonical bool
 }
 
 // NewEncoder returns a new encoder that writes to w.
@@ -147,6 +148,10 @@ func NewEncoder(w io.Writer) *Encoder {
 	return &Encoder{w: w}
 }
 
+// Canonical causes the encoder to switch to Canonical JSON mode.
+// Read more at: http://wiki.laptop.org/go/Canonical_JSON
+func (enc *Encoder) Canonical() { enc.canonical = true }
+
 // Encode writes the JSON encoding of v to the stream,
 // followed by a newline character.
 //
@@ -156,19 +161,21 @@ func (enc *Encoder) Encode(v interface{}) error {
 	if enc.err != nil {
 		return enc.err
 	}
-	e := newEncodeState()
+	e := newEncodeState(enc.canonical)
 	err := e.marshal(v)
 	if err != nil {
 		return err
 	}
 
-	// Terminate each value with a newline.
-	// This makes the output look a little nicer
-	// when debugging, and some kind of space
-	// is required if the encoded value was a number,
-	// so that the reader knows there aren't more
-	// digits coming.
-	e.WriteByte('\n')
+	if !enc.canonical {
+		// Terminate each value with a newline.
+		// This makes the output look a little nicer
+		// when debugging, and some kind of space
+		// is required if the encoded value was a number,
+		// so that the reader knows there aren't more
+		// digits coming.
+		e.WriteByte('\n')
+	}
 
 	if _, err = enc.w.Write(e.Bytes()); err != nil {
 		enc.err = err
-- 
2.5.0