Debian Codegraph @

encode_long_key_test.go

raw
1 package json_test
2
3 import (
4 stdjson "encoding/json"
5 "fmt"
6 "strings"
7 "testing"
8
9 "github.com/goccy/go-json"
10 )
11
12 // A key up to a chunk ( encoder.KeyChunkSize ) is written by the opcode of the field, a longer one by the
13 // generic field opcode with the value by its own opcode: both must encode as encoding/json does, for every kind
14 // of a value, with and without omitempty and the string option.
15
16 type longKeyMarshaler struct{ V int }
17
18 func (m longKeyMarshaler) MarshalJSON() ([]byte, error) {
19 return []byte(fmt.Sprintf(`{"v":%d}`, m.V)), nil
20 }
21
22 type longKeyPtrMarshaler struct{ V int }
23
24 func (m *longKeyPtrMarshaler) MarshalJSON() ([]byte, error) {
25 return []byte(fmt.Sprintf(`{"p":%d}`, m.V)), nil
26 }
27
28 type longKeyTextMarshaler struct{ V int }
29
30 func (m longKeyTextMarshaler) MarshalText() ([]byte, error) {
31 return []byte(fmt.Sprintf("t%d", m.V)), nil
32 }
33
34 type longKeyNode struct {
35 Next *longKeyNode `json:"a_key_of_a_recursive_field_which_is_longer_than_a_chunk,omitempty"`
36 V int `json:"v"`
37 }
38
39 type longKeyEmbedded struct {
40 EmbeddedFieldWithAKeyLongerThanAChunkOfTheVM int `json:"embedded_field_with_a_key_longer_than_a_chunk,omitempty"`
41 }
42
43 type longKeys struct {
44 Int int `json:"an_int_field_with_a_key_longer_than_a_chunk"`
45 Int8 int8 `json:"an_int8_field_with_a_key_longer_than_a_chunk"`
46 Int16 int16 `json:"an_int16_field_with_a_key_longer_than_a_chunk"`
47 Int32 int32 `json:"an_int32_field_with_a_key_longer_than_a_chunk"`
48 Int64 int64 `json:"an_int64_field_with_a_key_longer_than_a_chunk"`
49 Uint uint `json:"a_uint_field_with_a_key_longer_than_a_chunk"`
50 Uint8 uint8 `json:"a_uint8_field_with_a_key_longer_than_a_chunk"`
51 Float32 float32 `json:"a_float32_field_with_a_key_longer_than_a_chunk"`
52 Float64 float64 `json:"a_float64_field_with_a_key_longer_than_a_chunk"`
53 Bool bool `json:"a_bool_field_with_a_key_longer_than_a_chunk"`
54 String string `json:"a_string_field_with_a_key_longer_than_a_chunk"`
55 Bytes []byte `json:"a_bytes_field_with_a_key_longer_than_a_chunk"`
56 Slice []int `json:"a_slice_field_with_a_key_longer_than_a_chunk"`
57 Array [2]int `json:"an_array_field_with_a_key_longer_than_a_chunk"`
58 Array0 [0]int `json:"an_empty_array_field_with_a_key_longer_than_a_chunk"`
59 Map map[string]int `json:"a_map_field_with_a_key_longer_than_a_chunk"`
60 Interface any `json:"an_interface_field_with_a_key_longer_than_a_chunk"`
61 Struct longKeyEmbedded `json:"a_struct_field_with_a_key_longer_than_a_chunk"`
62 Ptr *int `json:"a_pointer_field_with_a_key_longer_than_a_chunk"`
63 PtrStruct *longKeyEmbedded `json:"a_struct_pointer_field_with_a_key_longer_than_a_chunk"`
64 Number json.Number `json:"a_number_field_with_a_key_longer_than_a_chunk"`
65 Marshaler longKeyMarshaler `json:"a_marshaler_field_with_a_key_longer_than_a_chunk"`
66 PtrMarsh longKeyPtrMarshaler `json:"a_pointer_marshaler_field_with_a_key_longer_than_a_chunk"`
67 PtrMarshP *longKeyPtrMarshaler `json:"a_pointer_marshaler_pointer_field_with_a_key_longer_than_a_chunk"`
68 Text longKeyTextMarshaler `json:"a_text_marshaler_field_with_a_key_longer_than_a_chunk"`
69 Node longKeyNode `json:"a_recursive_field_with_a_key_longer_than_a_chunk"`
70 Raw json.RawMessage `json:"a_raw_message_field_with_a_key_longer_than_a_chunk"`
71 longKeyEmbedded
72 }
73
74 type longKeysOmitEmpty struct {
75 Int int `json:"an_int_field_with_a_key_longer_than_a_chunk,omitempty"`
76 Int8 int8 `json:"an_int8_field_with_a_key_longer_than_a_chunk,omitempty"`
77 Int16 int16 `json:"an_int16_field_with_a_key_longer_than_a_chunk,omitempty"`
78 Int32 int32 `json:"an_int32_field_with_a_key_longer_than_a_chunk,omitempty"`
79 Int64 int64 `json:"an_int64_field_with_a_key_longer_than_a_chunk,omitempty"`
80 Uint uint `json:"a_uint_field_with_a_key_longer_than_a_chunk,omitempty"`
81 Uint8 uint8 `json:"a_uint8_field_with_a_key_longer_than_a_chunk,omitempty"`
82 Float32 float32 `json:"a_float32_field_with_a_key_longer_than_a_chunk,omitempty"`
83 Float64 float64 `json:"a_float64_field_with_a_key_longer_than_a_chunk,omitempty"`
84 Bool bool `json:"a_bool_field_with_a_key_longer_than_a_chunk,omitempty"`
85 String string `json:"a_string_field_with_a_key_longer_than_a_chunk,omitempty"`
86 Bytes []byte `json:"a_bytes_field_with_a_key_longer_than_a_chunk,omitempty"`
87 Slice []int `json:"a_slice_field_with_a_key_longer_than_a_chunk,omitempty"`
88 Array [2]int `json:"an_array_field_with_a_key_longer_than_a_chunk,omitempty"`
89 Array0 [0]int `json:"an_empty_array_field_with_a_key_longer_than_a_chunk,omitempty"`
90 Map map[string]int `json:"a_map_field_with_a_key_longer_than_a_chunk,omitempty"`
91 Interface any `json:"an_interface_field_with_a_key_longer_than_a_chunk,omitempty"`
92 Struct longKeyEmbedded `json:"a_struct_field_with_a_key_longer_than_a_chunk,omitempty"`
93 Ptr *int `json:"a_pointer_field_with_a_key_longer_than_a_chunk,omitempty"`
94 PtrStruct *longKeyEmbedded `json:"a_struct_pointer_field_with_a_key_longer_than_a_chunk,omitempty"`
95 Number json.Number `json:"a_number_field_with_a_key_longer_than_a_chunk,omitempty"`
96 Marshaler longKeyMarshaler `json:"a_marshaler_field_with_a_key_longer_than_a_chunk,omitempty"`
97 PtrMarsh longKeyPtrMarshaler `json:"a_pointer_marshaler_field_with_a_key_longer_than_a_chunk,omitempty"`
98 PtrMarshP *longKeyPtrMarshaler `json:"a_pointer_marshaler_pointer_field_with_a_key_longer_than_a_chunk,omitempty"`
99 Text longKeyTextMarshaler `json:"a_text_marshaler_field_with_a_key_longer_than_a_chunk,omitempty"`
100 Node longKeyNode `json:"a_recursive_field_with_a_key_longer_than_a_chunk,omitempty"`
101 Raw json.RawMessage `json:"a_raw_message_field_with_a_key_longer_than_a_chunk,omitempty"`
102 longKeyEmbedded
103 }
104
105 type longKeysString struct {
106 Int int `json:"an_int_field_with_a_key_longer_than_a_chunk,string"`
107 Uint uint16 `json:"a_uint_field_with_a_key_longer_than_a_chunk,string"`
108 Float32 float32 `json:"a_float32_field_with_a_key_longer_than_a_chunk,string"`
109 Float64 float64 `json:"a_float64_field_with_a_key_longer_than_a_chunk,string"`
110 Bool bool `json:"a_bool_field_with_a_key_longer_than_a_chunk,string"`
111 String string `json:"a_string_field_with_a_key_longer_than_a_chunk,string"`
112 Ptr *int `json:"a_pointer_field_with_a_key_longer_than_a_chunk,string"`
113 PtrOmit *float64 `json:"a_pointer_field_with_a_key_longer_than_a_chunk_omitted,omitempty,string"`
114 IntOmit int `json:"an_int_field_with_a_key_longer_than_a_chunk_omitted,omitempty,string"`
115 }
116
117 // the keys around the size of a chunk, with the quotes and the colon: 31, 32 and 33 bytes.
118 type keysAroundChunk struct {
119 A int `json:"a_key_of_28_characters_here_"`
120 B int `json:"a_key_of_29_characters_here__"`
121 C int `json:"a_key_of_30_characters_here___"`
122 D int `json:"a_key_of_28_characters_omit_,omitempty"`
123 E int `json:"a_key_of_29_characters_omit__,omitempty"`
124 F int `json:"a_key_of_30_characters_omit___,omitempty"`
125 }
126
127 func TestEncodeLongKey(t *testing.T) {
128 one := 1
129 f := 1.5
130 values := []any{
131 longKeys{},
132 &longKeys{},
133 longKeys{
134 Int: 1, Int8: -2, Int16: 3, Int32: -4, Int64: 5, Uint: 6, Uint8: 7, Float32: 1.5, Float64: -2.5,
135 Bool: true, String: "s", Bytes: []byte("b"), Slice: []int{1}, Array: [2]int{1, 2},
136 Map: map[string]int{"k": 1}, Interface: "i", Struct: longKeyEmbedded{1}, Ptr: &one,
137 PtrStruct: &longKeyEmbedded{2}, Number: "3", Marshaler: longKeyMarshaler{4},
138 PtrMarsh: longKeyPtrMarshaler{5}, PtrMarshP: &longKeyPtrMarshaler{6}, Text: longKeyTextMarshaler{7},
139 Node: longKeyNode{Next: &longKeyNode{V: 8}, V: 9}, Raw: json.RawMessage(`{"r":1}`),
140 longKeyEmbedded: longKeyEmbedded{10},
141 },
142 longKeysOmitEmpty{},
143 &longKeysOmitEmpty{},
144 longKeysOmitEmpty{
145 Int: 1, Int8: -2, Int16: 3, Int32: -4, Int64: 5, Uint: 6, Uint8: 7, Float32: 1.5, Float64: -2.5,
146 Bool: true, String: "s", Bytes: []byte("b"), Slice: []int{1}, Array: [2]int{1, 2},
147 Map: map[string]int{"k": 1}, Interface: "i", Struct: longKeyEmbedded{1}, Ptr: &one,
148 PtrStruct: &longKeyEmbedded{2}, Number: "3", Marshaler: longKeyMarshaler{4},
149 PtrMarsh: longKeyPtrMarshaler{5}, PtrMarshP: &longKeyPtrMarshaler{6}, Text: longKeyTextMarshaler{7},
150 Node: longKeyNode{Next: &longKeyNode{V: 8}, V: 9}, Raw: json.RawMessage(`{"r":1}`),
151 longKeyEmbedded: longKeyEmbedded{10},
152 },
153 // empty but not nil.
154 longKeysOmitEmpty{Bytes: []byte{}, Slice: []int{}, Map: map[string]int{}, Interface: 0, Raw: json.RawMessage{}},
155 longKeysString{},
156 &longKeysString{},
157 longKeysString{Int: -1, Uint: 2, Float32: 1.5, Float64: -2.5, Bool: true, String: "s", Ptr: &one, PtrOmit: &f, IntOmit: 3},
158 keysAroundChunk{},
159 keysAroundChunk{A: 1, B: 2, C: 3, D: 4, E: 5, F: 6},
160 []longKeysOmitEmpty{{}, {Int: 1}},
161 map[string]longKeysString{"k": {Int: 1}},
162 }
163 for _, v := range values {
164 expected, err := stdjson.Marshal(v)
165 if err != nil {
166 t.Fatal(err)
167 }
168 got, err := json.Marshal(v)
169 if err != nil {
170 t.Fatalf("%T: %v", v, err)
171 }
172 if string(got) != string(expected) {
173 t.Errorf("%T:\n got %s\nwant %s", v, got, expected)
174 }
175 // with the indent and the escape of HTML too.
176 expected, _ = stdjson.MarshalIndent(v, "", " ")
177 got, err = json.MarshalIndent(v, "", " ")
178 if err != nil {
179 t.Fatalf("%T: %v", v, err)
180 }
181 if string(got) != string(expected) {
182 t.Errorf("%T with indent:\n got %s\nwant %s", v, got, expected)
183 }
184 }
185 }
186
187 // a key which is longer than any chunk and is escaped: what a chunk can't hold.
188 func TestEncodeVeryLongKey(t *testing.T) {
189 type veryLongKey struct {
190 A int `json:"a_key_which_is_much_longer_than_a_chunk_of_the_VM_and_has_a_<tag>_and_an_&_in_it_to_escape,omitempty"`
191 B string `json:"another_key_which_is_much_longer_than_a_chunk_of_the_VM_with_a_value_after_it_of_any_length"`
192 }
193 for _, v := range []any{veryLongKey{}, veryLongKey{A: 1, B: strings.Repeat("x", 100)}} {
194 expected, _ := stdjson.Marshal(v)
195 got, err := json.Marshal(v)
196 if err != nil {
197 t.Fatal(err)
198 }
199 if string(got) != string(expected) {
200 t.Errorf("%T:\n got %s\nwant %s", v, got, expected)
201 }
202 }
203 }