From 877a86b07ee4702b2069c31ecd19a4d08a0ace47 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 10 Jan 2021 14:56:49 +0900 Subject: [PATCH] Add indent test cases for float32/float64 types --- coverage_test.go | 768 +++++++++++++++++++++++++++++++++++++++++++++-- encode_vm.go | 716 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 1452 insertions(+), 32 deletions(-) diff --git a/coverage_test.go b/coverage_test.go index 32ea189..e47fd83 100644 --- a/coverage_test.go +++ b/coverage_test.go @@ -9839,13 +9839,19 @@ func TestCoverStructHeadFloat32(t *testing.T) { } tests := []struct { - name string - expected string - data interface{} + name string + expected string + indentExpected string + data interface{} }{ { name: "HeadFloat32Zero", expected: `{"a":0}`, + indentExpected: ` +{ + "a": 0 +} +`, data: struct { A float32 `json:"a"` }{}, @@ -9853,6 +9859,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { A float32 `json:"a"` }{A: 1}, @@ -9860,6 +9871,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32Ptr", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { A *float32 `json:"a"` }{A: float32ptr(1)}, @@ -9867,6 +9883,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrNil", expected: `{"a":null}`, + indentExpected: ` +{ + "a": null +} +`, data: struct { A *float32 `json:"a"` }{A: nil}, @@ -9874,6 +9895,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32Zero", expected: `{"a":0}`, + indentExpected: ` +{ + "a": 0 +} +`, data: &struct { A float32 `json:"a"` }{}, @@ -9881,6 +9907,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: &struct { A float32 `json:"a"` }{A: 1}, @@ -9888,6 +9919,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32Ptr", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: &struct { A *float32 `json:"a"` }{A: float32ptr(1)}, @@ -9895,6 +9931,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNil", expected: `{"a":null}`, + indentExpected: ` +{ + "a": null +} +`, data: &struct { A *float32 `json:"a"` }{A: nil}, @@ -9902,6 +9943,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32Nil", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *float32 `json:"a"` })(nil), @@ -9909,6 +9953,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32ZeroMultiFields", expected: `{"a":0,"b":0}`, + indentExpected: ` +{ + "a": 0, + "b": 0 +} +`, data: struct { A float32 `json:"a"` B float32 `json:"b"` @@ -9917,6 +9967,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32MultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { A float32 `json:"a"` B float32 `json:"b"` @@ -9925,6 +9981,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrMultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { A *float32 `json:"a"` B *float32 `json:"b"` @@ -9933,6 +9995,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrNilMultiFields", expected: `{"a":null,"b":null}`, + indentExpected: ` +{ + "a": null, + "b": null +} +`, data: struct { A *float32 `json:"a"` B *float32 `json:"b"` @@ -9941,6 +10009,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32ZeroMultiFields", expected: `{"a":0,"b":0}`, + indentExpected: ` +{ + "a": 0, + "b": 0 +} +`, data: &struct { A float32 `json:"a"` B float32 `json:"b"` @@ -9949,6 +10023,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32MultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: &struct { A float32 `json:"a"` B float32 `json:"b"` @@ -9957,6 +10037,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrMultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: &struct { A *float32 `json:"a"` B *float32 `json:"b"` @@ -9965,6 +10051,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNilMultiFields", expected: `{"a":null,"b":null}`, + indentExpected: ` +{ + "a": null, + "b": null +} +`, data: &struct { A *float32 `json:"a"` B *float32 `json:"b"` @@ -9973,6 +10065,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32NilMultiFields", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *float32 `json:"a"` B *float32 `json:"b"` @@ -9981,6 +10076,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32ZeroNotRoot", expected: `{"A":{"a":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + } +} +`, data: struct { A struct { A float32 `json:"a"` @@ -9990,6 +10092,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32NotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A struct { A float32 `json:"a"` @@ -10001,6 +10110,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrNotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A struct { A *float32 `json:"a"` @@ -10012,6 +10128,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrNilNotRoot", expected: `{"A":{"a":null}}`, + indentExpected: ` +{ + "A": { + "a": null + } +} +`, data: struct { A struct { A *float32 `json:"a"` @@ -10021,6 +10144,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32ZeroNotRoot", expected: `{"A":{"a":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + } +} +`, data: struct { A *struct { A float32 `json:"a"` @@ -10032,6 +10162,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32NotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A *struct { A float32 `json:"a"` @@ -10043,6 +10180,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A *struct { A *float32 `json:"a"` @@ -10054,6 +10198,13 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNilNotRoot", expected: `{"A":{"a":null}}`, + indentExpected: ` +{ + "A": { + "a": null + } +} +`, data: struct { A *struct { A *float32 `json:"a"` @@ -10065,6 +10216,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32NilNotRoot", expected: `{"A":null}`, + indentExpected: ` +{ + "A": null +} +`, data: struct { A *struct { A *float32 `json:"a"` @@ -10074,6 +10230,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32ZeroMultiFieldsNotRoot", expected: `{"A":{"a":0},"B":{"b":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + }, + "B": { + "b": 0 + } +} +`, data: struct { A struct { A float32 `json:"a"` @@ -10086,6 +10252,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32MultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: struct { A struct { A float32 `json:"a"` @@ -10102,6 +10278,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrMultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: struct { A struct { A *float32 `json:"a"` @@ -10118,6 +10304,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "HeadFloat32PtrNilMultiFieldsNotRoot", expected: `{"A":{"a":null},"B":{"b":null}}`, + indentExpected: ` +{ + "A": { + "a": null + }, + "B": { + "b": null + } +} +`, data: struct { A struct { A *float32 `json:"a"` @@ -10134,6 +10330,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32ZeroMultiFieldsNotRoot", expected: `{"A":{"a":0},"B":{"b":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + }, + "B": { + "b": 0 + } +} +`, data: &struct { A struct { A float32 `json:"a"` @@ -10146,6 +10352,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32MultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: &struct { A struct { A float32 `json:"a"` @@ -10162,6 +10378,16 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrMultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: &struct { A *struct { A *float32 `json:"a"` @@ -10178,6 +10404,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNilMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, + indentExpected: ` +{ + "A": null, + "B": null +} +`, data: &struct { A *struct { A *float32 `json:"a"` @@ -10190,6 +10422,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32NilMultiFieldsNotRoot", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *struct { A *float32 `json:"a"` @@ -10202,6 +10437,18 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32DoubleMultiFieldsNotRoot", expected: `{"A":{"a":1,"b":2},"B":{"a":3,"b":4}}`, + indentExpected: ` +{ + "A": { + "a": 1, + "b": 2 + }, + "B": { + "a": 3, + "b": 4 + } +} +`, data: &struct { A *struct { A float32 `json:"a"` @@ -10222,6 +10469,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32NilDoubleMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, + indentExpected: ` +{ + "A": null, + "B": null +} +`, data: &struct { A *struct { A float32 `json:"a"` @@ -10236,6 +10489,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32NilDoubleMultiFieldsNotRoot", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *struct { A float32 `json:"a"` @@ -10250,6 +10506,18 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrDoubleMultiFieldsNotRoot", expected: `{"A":{"a":1,"b":2},"B":{"a":3,"b":4}}`, + indentExpected: ` +{ + "A": { + "a": 1, + "b": 2 + }, + "B": { + "a": 3, + "b": 4 + } +} +`, data: &struct { A *struct { A *float32 `json:"a"` @@ -10270,6 +10538,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNilDoubleMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, + indentExpected: ` +{ + "A": null, + "B": null +} +`, data: &struct { A *struct { A *float32 `json:"a"` @@ -10284,6 +10558,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrHeadFloat32PtrNilDoubleMultiFieldsNotRoot", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *struct { A *float32 `json:"a"` @@ -10298,6 +10575,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "AnonymousHeadFloat32", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { structFloat32 B float32 `json:"b"` @@ -10309,6 +10592,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrAnonymousHeadFloat32", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { *structFloat32 B float32 `json:"b"` @@ -10320,6 +10609,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "NilPtrAnonymousHeadFloat32", expected: `{"b":2}`, + indentExpected: ` +{ + "b": 2 +} +`, data: struct { *structFloat32 B float32 `json:"b"` @@ -10331,6 +10625,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "AnonymousHeadFloat32Ptr", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { structFloat32Ptr B *float32 `json:"b"` @@ -10342,6 +10642,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "AnonymousHeadFloat32PtrNil", expected: `{"a":null,"b":2}`, + indentExpected: ` +{ + "a": null, + "b": 2 +} +`, data: struct { structFloat32Ptr B *float32 `json:"b"` @@ -10353,6 +10659,12 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrAnonymousHeadFloat32Ptr", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { *structFloat32Ptr B *float32 `json:"b"` @@ -10364,6 +10676,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "NilPtrAnonymousHeadFloat32Ptr", expected: `{"b":2}`, + indentExpected: ` +{ + "b": 2 +} +`, data: struct { *structFloat32Ptr B *float32 `json:"b"` @@ -10375,6 +10692,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "AnonymousHeadFloat32Only", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { structFloat32 }{ @@ -10384,6 +10706,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrAnonymousHeadFloat32Only", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { *structFloat32 }{ @@ -10393,6 +10720,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "NilPtrAnonymousHeadFloat32Only", expected: `{}`, + indentExpected: ` +{} +`, data: struct { *structFloat32 }{ @@ -10402,6 +10732,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "AnonymousHeadFloat32PtrOnly", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { structFloat32Ptr }{ @@ -10411,6 +10746,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "AnonymousHeadFloat32PtrNilOnly", expected: `{"a":null}`, + indentExpected: ` +{ + "a": null +} +`, data: struct { structFloat32Ptr }{ @@ -10420,6 +10760,11 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "PtrAnonymousHeadFloat32PtrOnly", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { *structFloat32Ptr }{ @@ -10429,6 +10774,9 @@ func TestCoverStructHeadFloat32(t *testing.T) { { name: "NilPtrAnonymousHeadFloat32PtrOnly", expected: `{}`, + indentExpected: ` +{} +`, data: struct { *structFloat32Ptr }{ @@ -10437,15 +10785,27 @@ func TestCoverStructHeadFloat32(t *testing.T) { }, } for _, test := range tests { - for _, htmlEscape := range []bool{true, false} { - var buf bytes.Buffer - enc := NewEncoder(&buf) - enc.SetEscapeHTML(htmlEscape) - if err := enc.Encode(test.data); err != nil { - t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err) - } - if strings.TrimRight(buf.String(), "\n") != test.expected { - t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.expected, buf.String()) + for _, indent := range []bool{true, false} { + for _, htmlEscape := range []bool{true, false} { + var buf bytes.Buffer + enc := NewEncoder(&buf) + enc.SetEscapeHTML(htmlEscape) + if indent { + enc.SetIndent("", " ") + } + if err := enc.Encode(test.data); err != nil { + t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err) + } + if indent { + got := "\n" + buf.String() + if got != test.indentExpected { + t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.indentExpected, got) + } + } else { + if strings.TrimRight(buf.String(), "\n") != test.expected { + t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.expected, buf.String()) + } + } } } } @@ -10460,13 +10820,19 @@ func TestCoverStructHeadFloat64(t *testing.T) { } tests := []struct { - name string - expected string - data interface{} + name string + expected string + indentExpected string + data interface{} }{ { name: "HeadFloat64Zero", expected: `{"a":0}`, + indentExpected: ` +{ + "a": 0 +} +`, data: struct { A float64 `json:"a"` }{}, @@ -10474,6 +10840,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { A float64 `json:"a"` }{A: 1}, @@ -10481,6 +10852,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64Ptr", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { A *float64 `json:"a"` }{A: float64ptr(1)}, @@ -10488,6 +10864,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrNil", expected: `{"a":null}`, + indentExpected: ` +{ + "a": null +} +`, data: struct { A *float64 `json:"a"` }{A: nil}, @@ -10495,6 +10876,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64Zero", expected: `{"a":0}`, + indentExpected: ` +{ + "a": 0 +} +`, data: &struct { A float64 `json:"a"` }{}, @@ -10502,6 +10888,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: &struct { A float64 `json:"a"` }{A: 1}, @@ -10509,6 +10900,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64Ptr", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: &struct { A *float64 `json:"a"` }{A: float64ptr(1)}, @@ -10516,6 +10912,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNil", expected: `{"a":null}`, + indentExpected: ` +{ + "a": null +} +`, data: &struct { A *float64 `json:"a"` }{A: nil}, @@ -10523,6 +10924,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64Nil", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *float64 `json:"a"` })(nil), @@ -10530,6 +10934,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64ZeroMultiFields", expected: `{"a":0,"b":0}`, + indentExpected: ` +{ + "a": 0, + "b": 0 +} +`, data: struct { A float64 `json:"a"` B float64 `json:"b"` @@ -10538,6 +10948,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64MultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { A float64 `json:"a"` B float64 `json:"b"` @@ -10546,6 +10962,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrMultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { A *float64 `json:"a"` B *float64 `json:"b"` @@ -10554,6 +10976,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrNilMultiFields", expected: `{"a":null,"b":null}`, + indentExpected: ` +{ + "a": null, + "b": null +} +`, data: struct { A *float64 `json:"a"` B *float64 `json:"b"` @@ -10562,6 +10990,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64ZeroMultiFields", expected: `{"a":0,"b":0}`, + indentExpected: ` +{ + "a": 0, + "b": 0 +} +`, data: &struct { A float64 `json:"a"` B float64 `json:"b"` @@ -10570,6 +11004,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64MultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: &struct { A float64 `json:"a"` B float64 `json:"b"` @@ -10578,6 +11018,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrMultiFields", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: &struct { A *float64 `json:"a"` B *float64 `json:"b"` @@ -10586,6 +11032,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNilMultiFields", expected: `{"a":null,"b":null}`, + indentExpected: ` +{ + "a": null, + "b": null +} +`, data: &struct { A *float64 `json:"a"` B *float64 `json:"b"` @@ -10594,6 +11046,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64NilMultiFields", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *float64 `json:"a"` B *float64 `json:"b"` @@ -10602,6 +11057,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64ZeroNotRoot", expected: `{"A":{"a":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + } +} +`, data: struct { A struct { A float64 `json:"a"` @@ -10611,6 +11073,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64NotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A struct { A float64 `json:"a"` @@ -10622,6 +11091,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrNotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A struct { A *float64 `json:"a"` @@ -10633,6 +11109,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrNilNotRoot", expected: `{"A":{"a":null}}`, + indentExpected: ` +{ + "A": { + "a": null + } +} +`, data: struct { A struct { A *float64 `json:"a"` @@ -10642,6 +11125,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64ZeroNotRoot", expected: `{"A":{"a":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + } +} +`, data: struct { A *struct { A float64 `json:"a"` @@ -10653,6 +11143,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64NotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A *struct { A float64 `json:"a"` @@ -10664,6 +11161,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNotRoot", expected: `{"A":{"a":1}}`, + indentExpected: ` +{ + "A": { + "a": 1 + } +} +`, data: struct { A *struct { A *float64 `json:"a"` @@ -10675,6 +11179,13 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNilNotRoot", expected: `{"A":{"a":null}}`, + indentExpected: ` +{ + "A": { + "a": null + } +} +`, data: struct { A *struct { A *float64 `json:"a"` @@ -10686,6 +11197,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64NilNotRoot", expected: `{"A":null}`, + indentExpected: ` +{ + "A": null +} +`, data: struct { A *struct { A *float64 `json:"a"` @@ -10695,6 +11211,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64ZeroMultiFieldsNotRoot", expected: `{"A":{"a":0},"B":{"b":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + }, + "B": { + "b": 0 + } +} +`, data: struct { A struct { A float64 `json:"a"` @@ -10707,6 +11233,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64MultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: struct { A struct { A float64 `json:"a"` @@ -10723,6 +11259,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrMultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: struct { A struct { A *float64 `json:"a"` @@ -10739,6 +11285,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "HeadFloat64PtrNilMultiFieldsNotRoot", expected: `{"A":{"a":null},"B":{"b":null}}`, + indentExpected: ` +{ + "A": { + "a": null + }, + "B": { + "b": null + } +} +`, data: struct { A struct { A *float64 `json:"a"` @@ -10755,6 +11311,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64ZeroMultiFieldsNotRoot", expected: `{"A":{"a":0},"B":{"b":0}}`, + indentExpected: ` +{ + "A": { + "a": 0 + }, + "B": { + "b": 0 + } +} +`, data: &struct { A struct { A float64 `json:"a"` @@ -10767,6 +11333,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64MultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: &struct { A struct { A float64 `json:"a"` @@ -10783,6 +11359,16 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrMultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, + indentExpected: ` +{ + "A": { + "a": 1 + }, + "B": { + "b": 2 + } +} +`, data: &struct { A *struct { A *float64 `json:"a"` @@ -10799,6 +11385,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNilMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, + indentExpected: ` +{ + "A": null, + "B": null +} +`, data: &struct { A *struct { A *float64 `json:"a"` @@ -10811,6 +11403,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64NilMultiFieldsNotRoot", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *struct { A *float64 `json:"a"` @@ -10823,6 +11418,18 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64DoubleMultiFieldsNotRoot", expected: `{"A":{"a":1,"b":2},"B":{"a":3,"b":4}}`, + indentExpected: ` +{ + "A": { + "a": 1, + "b": 2 + }, + "B": { + "a": 3, + "b": 4 + } +} +`, data: &struct { A *struct { A float64 `json:"a"` @@ -10843,6 +11450,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64NilDoubleMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, + indentExpected: ` +{ + "A": null, + "B": null +} +`, data: &struct { A *struct { A float64 `json:"a"` @@ -10857,6 +11470,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64NilDoubleMultiFieldsNotRoot", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *struct { A float64 `json:"a"` @@ -10871,6 +11487,18 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrDoubleMultiFieldsNotRoot", expected: `{"A":{"a":1,"b":2},"B":{"a":3,"b":4}}`, + indentExpected: ` +{ + "A": { + "a": 1, + "b": 2 + }, + "B": { + "a": 3, + "b": 4 + } +} +`, data: &struct { A *struct { A *float64 `json:"a"` @@ -10891,6 +11519,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNilDoubleMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, + indentExpected: ` +{ + "A": null, + "B": null +} +`, data: &struct { A *struct { A *float64 `json:"a"` @@ -10905,6 +11539,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrHeadFloat64PtrNilDoubleMultiFieldsNotRoot", expected: `null`, + indentExpected: ` +null +`, data: (*struct { A *struct { A *float64 `json:"a"` @@ -10919,6 +11556,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "AnonymousHeadFloat64", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { structFloat64 B float64 `json:"b"` @@ -10930,6 +11573,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrAnonymousHeadFloat64", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { *structFloat64 B float64 `json:"b"` @@ -10941,6 +11590,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "NilPtrAnonymousHeadFloat64", expected: `{"b":2}`, + indentExpected: ` +{ + "b": 2 +} +`, data: struct { *structFloat64 B float64 `json:"b"` @@ -10952,6 +11606,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "AnonymousHeadFloat64Ptr", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { structFloat64Ptr B *float64 `json:"b"` @@ -10963,6 +11623,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "AnonymousHeadFloat64PtrNil", expected: `{"a":null,"b":2}`, + indentExpected: ` +{ + "a": null, + "b": 2 +} +`, data: struct { structFloat64Ptr B *float64 `json:"b"` @@ -10974,6 +11640,12 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrAnonymousHeadFloat64Ptr", expected: `{"a":1,"b":2}`, + indentExpected: ` +{ + "a": 1, + "b": 2 +} +`, data: struct { *structFloat64Ptr B *float64 `json:"b"` @@ -10985,6 +11657,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "NilPtrAnonymousHeadFloat64Ptr", expected: `{"b":2}`, + indentExpected: ` +{ + "b": 2 +} +`, data: struct { *structFloat64Ptr B *float64 `json:"b"` @@ -10996,6 +11673,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "AnonymousHeadFloat64Only", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { structFloat64 }{ @@ -11005,6 +11687,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrAnonymousHeadFloat64Only", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { *structFloat64 }{ @@ -11014,6 +11701,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "NilPtrAnonymousHeadFloat64Only", expected: `{}`, + indentExpected: ` +{} +`, data: struct { *structFloat64 }{ @@ -11023,6 +11713,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "AnonymousHeadFloat64PtrOnly", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { structFloat64Ptr }{ @@ -11032,6 +11727,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "AnonymousHeadFloat64PtrNilOnly", expected: `{"a":null}`, + indentExpected: ` +{ + "a": null +} +`, data: struct { structFloat64Ptr }{ @@ -11041,6 +11741,11 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "PtrAnonymousHeadFloat64PtrOnly", expected: `{"a":1}`, + indentExpected: ` +{ + "a": 1 +} +`, data: struct { *structFloat64Ptr }{ @@ -11050,6 +11755,9 @@ func TestCoverStructHeadFloat64(t *testing.T) { { name: "NilPtrAnonymousHeadFloat64PtrOnly", expected: `{}`, + indentExpected: ` +{} +`, data: struct { *structFloat64Ptr }{ @@ -11058,15 +11766,27 @@ func TestCoverStructHeadFloat64(t *testing.T) { }, } for _, test := range tests { - for _, htmlEscape := range []bool{true, false} { - var buf bytes.Buffer - enc := NewEncoder(&buf) - enc.SetEscapeHTML(htmlEscape) - if err := enc.Encode(test.data); err != nil { - t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err) - } - if strings.TrimRight(buf.String(), "\n") != test.expected { - t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.expected, buf.String()) + for _, indent := range []bool{true, false} { + for _, htmlEscape := range []bool{true, false} { + var buf bytes.Buffer + enc := NewEncoder(&buf) + enc.SetEscapeHTML(htmlEscape) + if indent { + enc.SetIndent("", " ") + } + if err := enc.Encode(test.data); err != nil { + t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err) + } + if indent { + got := "\n" + buf.String() + if got != test.indentExpected { + t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.indentExpected, got) + } + } else { + if strings.TrimRight(buf.String(), "\n") != test.expected { + t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.expected, buf.String()) + } + } } } } diff --git a/encode_vm.go b/encode_vm.go index e2780cb..05009f8 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -8666,12 +8666,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte case opStructFieldHeadFloat32Indent: ptr := load(ctxptr, code.idx) if ptr == 0 { - b = e.encodeIndent(b, code.indent) b = encodeNull(b) b = encodeIndentComma(b) code = code.end.next } else { - b = e.encodeIndent(b, code.indent) b = append(b, '{', '\n') b = e.encodeIndent(b, code.indent+1) b = append(b, code.key...) @@ -8680,18 +8678,25 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte b = encodeIndentComma(b) code = code.next } + case opStructFieldPtrHeadFloat32OnlyIndent, opStructFieldHeadFloat32OnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + b = encodeFloat32(b, e.ptrToFloat32(p)) + b = encodeIndentComma(b) + code = code.next case opStructEscapedFieldPtrHeadFloat32Indent: store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) fallthrough case opStructEscapedFieldHeadFloat32Indent: ptr := load(ctxptr, code.idx) if ptr == 0 { - b = e.encodeIndent(b, code.indent) b = encodeNull(b) b = encodeIndentComma(b) code = code.end.next } else { - b = e.encodeIndent(b, code.indent) b = append(b, '{', '\n') b = e.encodeIndent(b, code.indent+1) b = append(b, code.escapedKey...) @@ -8700,13 +8705,295 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte b = encodeIndentComma(b) code = code.next } + case opStructEscapedFieldPtrHeadFloat32OnlyIndent, opStructEscapedFieldHeadFloat32OnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + b = encodeFloat32(b, e.ptrToFloat32(p)) + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrHeadFloat32PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructFieldHeadFloat32PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructFieldHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrHeadFloat32PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructEscapedFieldHeadFloat32PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructEscapedFieldHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldHeadFloat32NPtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + for i := 0; i < code.ptrNum; i++ { + if p == 0 { + break + } + p = e.ptrToPtr(p) + } + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldHeadFloat32NPtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + for i := 0; i < code.ptrNum; i++ { + if p == 0 { + break + } + p = e.ptrToPtr(p) + } + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrAnonymousHeadFloat32Indent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructFieldAnonymousHeadFloat32Indent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + b = encodeFloat32(b, e.ptrToFloat32(ptr)) + b = encodeIndentComma(b) + code = code.next + } + case opStructEscapedFieldPtrAnonymousHeadFloat32Indent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructEscapedFieldAnonymousHeadFloat32Indent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + b = encodeFloat32(b, e.ptrToFloat32(ptr)) + b = encodeIndentComma(b) + code = code.next + } + case opStructFieldPtrAnonymousHeadFloat32OnlyIndent, opStructFieldAnonymousHeadFloat32OnlyIndent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + b = encodeFloat32(b, e.ptrToFloat32(ptr)) + b = encodeIndentComma(b) + code = code.next + } + case opStructEscapedFieldPtrAnonymousHeadFloat32OnlyIndent, opStructEscapedFieldAnonymousHeadFloat32OnlyIndent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + b = encodeFloat32(b, e.ptrToFloat32(ptr)) + b = encodeIndentComma(b) + code = code.next + } + case opStructFieldPtrAnonymousHeadFloat32PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructFieldAnonymousHeadFloat32PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrAnonymousHeadFloat32PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructEscapedFieldAnonymousHeadFloat32PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrAnonymousHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructFieldAnonymousHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrAnonymousHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructEscapedFieldAnonymousHeadFloat32PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = encodeIndentComma(b) + code = code.next case opStructFieldPtrHeadFloat64Indent: store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) fallthrough case opStructFieldHeadFloat64Indent: ptr := load(ctxptr, code.idx) if ptr == 0 { - b = e.encodeIndent(b, code.indent) b = encodeNull(b) b = encodeIndentComma(b) code = code.end.next @@ -8715,7 +9002,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } - b = e.encodeIndent(b, code.indent) b = append(b, '{', '\n') b = e.encodeIndent(b, code.indent+1) b = append(b, code.key...) @@ -8724,13 +9010,25 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte b = encodeIndentComma(b) code = code.next } + case opStructFieldPtrHeadFloat64OnlyIndent, opStructFieldHeadFloat64OnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + b = encodeIndentComma(b) + code = code.next case opStructEscapedFieldPtrHeadFloat64Indent: store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) fallthrough case opStructEscapedFieldHeadFloat64Indent: ptr := load(ctxptr, code.idx) if ptr == 0 { - b = e.encodeIndent(b, code.indent) b = encodeNull(b) b = encodeIndentComma(b) code = code.end.next @@ -8739,7 +9037,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } - b = e.encodeIndent(b, code.indent) b = append(b, '{', '\n') b = e.encodeIndent(b, code.indent+1) b = append(b, code.escapedKey...) @@ -8748,6 +9045,349 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte b = encodeIndentComma(b) code = code.next } + case opStructEscapedFieldPtrHeadFloat64OnlyIndent, opStructEscapedFieldHeadFloat64OnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrHeadFloat64PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructFieldHeadFloat64PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructFieldHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrHeadFloat64PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructEscapedFieldHeadFloat64PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructEscapedFieldHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldHeadFloat64NPtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.key...) + b = append(b, ' ') + for i := 0; i < code.ptrNum; i++ { + if p == 0 { + break + } + p = e.ptrToPtr(p) + } + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldHeadFloat64NPtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + b = encodeNull(b) + } else { + b = append(b, '{', '\n') + b = e.encodeIndent(b, code.indent+1) + b = append(b, code.escapedKey...) + b = append(b, ' ') + for i := 0; i < code.ptrNum; i++ { + if p == 0 { + break + } + p = e.ptrToPtr(p) + } + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrAnonymousHeadFloat64Indent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructFieldAnonymousHeadFloat64Indent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + v := e.ptrToFloat64(ptr) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + b = encodeIndentComma(b) + code = code.next + } + case opStructEscapedFieldPtrAnonymousHeadFloat64Indent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructEscapedFieldAnonymousHeadFloat64Indent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + v := e.ptrToFloat64(ptr) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + b = encodeIndentComma(b) + code = code.next + } + case opStructFieldPtrAnonymousHeadFloat64OnlyIndent, opStructFieldAnonymousHeadFloat64OnlyIndent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + v := e.ptrToFloat64(ptr) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + b = encodeIndentComma(b) + code = code.next + } + case opStructEscapedFieldPtrAnonymousHeadFloat64OnlyIndent, opStructEscapedFieldAnonymousHeadFloat64OnlyIndent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + code = code.end.next + } else { + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + v := e.ptrToFloat64(ptr) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + b = encodeIndentComma(b) + code = code.next + } + case opStructFieldPtrAnonymousHeadFloat64PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructFieldAnonymousHeadFloat64PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrAnonymousHeadFloat64PtrIndent: + store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) + fallthrough + case opStructEscapedFieldAnonymousHeadFloat64PtrIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + p = e.ptrToPtr(p) + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = encodeIndentComma(b) + code = code.next + case opStructFieldPtrAnonymousHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructFieldAnonymousHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = encodeIndentComma(b) + code = code.next + case opStructEscapedFieldPtrAnonymousHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + if p == 0 { + code = code.end.next + break + } + store(ctxptr, code.idx, e.ptrToPtr(p)) + fallthrough + case opStructEscapedFieldAnonymousHeadFloat64PtrOnlyIndent: + p := load(ctxptr, code.idx) + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = encodeIndentComma(b) + code = code.next case opStructFieldPtrHeadStringIndent: store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) fallthrough @@ -17669,6 +18309,32 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte b = encodeFloat32(b, e.ptrToFloat32(ptr+code.offset)) b = e.appendStructEndIndent(b, code.indent-1) code = code.next + case opStructEndFloat32PtrIndent: + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + ptr := load(ctxptr, code.headIdx) + p := e.ptrToPtr(ptr + code.offset) + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = e.appendStructEndIndent(b, code.indent-1) + code = code.next + case opStructEscapedEndFloat32PtrIndent: + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + ptr := load(ctxptr, code.headIdx) + p := e.ptrToPtr(ptr + code.offset) + if p == 0 { + b = encodeNull(b) + } else { + b = encodeFloat32(b, e.ptrToFloat32(p)) + } + b = e.appendStructEndIndent(b, code.indent-1) + code = code.next case opStructEndFloat64Indent: b = e.encodeIndent(b, code.indent) b = append(b, code.key...) @@ -17693,6 +18359,40 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte b = encodeFloat64(b, v) b = e.appendStructEndIndent(b, code.indent-1) code = code.next + case opStructEndFloat64PtrIndent: + b = e.encodeIndent(b, code.indent) + b = append(b, code.key...) + b = append(b, ' ') + ptr := load(ctxptr, code.headIdx) + p := e.ptrToPtr(ptr + code.offset) + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = e.appendStructEndIndent(b, code.indent-1) + code = code.next + case opStructEscapedEndFloat64PtrIndent: + b = e.encodeIndent(b, code.indent) + b = append(b, code.escapedKey...) + b = append(b, ' ') + ptr := load(ctxptr, code.headIdx) + p := e.ptrToPtr(ptr + code.offset) + if p == 0 { + b = encodeNull(b) + } else { + v := e.ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = encodeFloat64(b, v) + } + b = e.appendStructEndIndent(b, code.indent-1) + code = code.next case opStructEndStringIndent: b = e.encodeIndent(b, code.indent) b = append(b, code.key...)