| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/goccy/go-json" |
| 8 |
) |
| 9 |
|
| 10 |
// A recursive struct is encoded by jumping to its own opcodes with a new frame of the pointers. |
| 11 |
// The frame must not overlap the one of the caller, whatever the struct has before the recursive field. |
| 12 |
|
| 13 |
type recursiveMapNode struct { |
| 14 |
Meta map[string]any `json:"meta"` |
| 15 |
Children []recursiveMapNode `json:"children"` |
| 16 |
} |
| 17 |
|
| 18 |
type recursiveLeadingFieldsNode struct { |
| 19 |
EvaluationPath string `json:"evaluationPath"` |
| 20 |
SchemaLocation string `json:"schemaLocation"` |
| 21 |
Annotations map[string]any `json:"annotations"` |
| 22 |
Details []recursiveLeadingFieldsNode `json:"details"` |
| 23 |
} |
| 24 |
|
| 25 |
type recursiveSliceOfInterfaceNode struct { |
| 26 |
Alerts [][]any |
| 27 |
Items []recursiveSliceOfInterfaceNode |
| 28 |
} |
| 29 |
|
| 30 |
type recursiveInterfaceSliceNode struct { |
| 31 |
Alerts []any |
| 32 |
Items []recursiveSliceOfInterfaceNode |
| 33 |
} |
| 34 |
|
| 35 |
type recursiveInterfaceNode struct { |
| 36 |
Value any |
| 37 |
Children []recursiveInterfaceNode |
| 38 |
} |
| 39 |
|
| 40 |
type recursivePtrNode struct { |
| 41 |
Meta map[string]any |
| 42 |
Next *recursivePtrNode |
| 43 |
} |
| 44 |
|
| 45 |
func TestEncodeRecursiveStructWithInterface(t *testing.T) { |
| 46 |
for _, test := range []struct { |
| 47 |
name string |
| 48 |
v any |
| 49 |
}{ |
| 50 |
{"map of interface in nested node", []recursiveMapNode{{Children: []recursiveMapNode{{Meta: map[string]any{"a": 1}}}}}}, |
| 51 |
{"map of interface in every node", recursiveMapNode{ |
| 52 |
Meta: map[string]any{"a": 1}, |
| 53 |
Children: []recursiveMapNode{ |
| 54 |
{Meta: map[string]any{"b": "x"}, Children: []recursiveMapNode{{Meta: map[string]any{"c": true}}}}, |
| 55 |
{Meta: map[string]any{"d": []any{1, "y"}}}, |
| 56 |
}, |
| 57 |
}}, |
| 58 |
{"leading fields and map of interface", recursiveLeadingFieldsNode{ |
| 59 |
Details: []recursiveLeadingFieldsNode{{Annotations: map[string]any{"description": "d"}}}, |
| 60 |
}}, |
| 61 |
{"slice of slice of interface", recursiveSliceOfInterfaceNode{ |
| 62 |
Items: []recursiveSliceOfInterfaceNode{{Alerts: [][]any{{2}}}}, |
| 63 |
}}, |
| 64 |
{"slice of interface", recursiveInterfaceSliceNode{ |
| 65 |
Alerts: []any{1}, |
| 66 |
Items: []recursiveSliceOfInterfaceNode{{Alerts: [][]any{{2, "a"}}}}, |
| 67 |
}}, |
| 68 |
{"interface holding the recursive struct", recursiveInterfaceNode{ |
| 69 |
Value: recursiveInterfaceNode{Value: 1}, |
| 70 |
Children: []recursiveInterfaceNode{{Value: map[string]any{"k": recursiveInterfaceNode{Value: "v"}}}}, |
| 71 |
}}, |
| 72 |
{"pointer recursion with map of interface", &recursivePtrNode{ |
| 73 |
Meta: map[string]any{"a": 1}, |
| 74 |
Next: &recursivePtrNode{Meta: map[string]any{"b": map[string]any{"c": 2}}, Next: &recursivePtrNode{}}, |
| 75 |
}}, |
| 76 |
} { |
| 77 |
t.Run(test.name, func(t *testing.T) { |
| 78 |
expected, err := stdjson.Marshal(test.v) |
| 79 |
if err != nil { |
| 80 |
t.Fatal(err) |
| 81 |
} |
| 82 |
for i := 0; i < 2; i++ { |
| 83 |
got, err := json.Marshal(test.v) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("call %d: %v", i, err) |
| 86 |
} |
| 87 |
if string(got) != string(expected) { |
| 88 |
t.Fatalf("call %d: expected %s but got %s", i, expected, got) |
| 89 |
} |
| 90 |
} |
| 91 |
}) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// A recursive struct which is embedded, or which is referred to from an embedded struct. |
| 96 |
// The opcodes of an embedded struct are only the fields, and the ones of a struct have the braces and |
| 97 |
// the check of nil, so a recursive code must jump to the right ones. |
| 98 |
|
| 99 |
type recursiveEmbeddedInner struct { |
| 100 |
Inner *recursiveEmbeddedInner `json:"inner"` |
| 101 |
} |
| 102 |
|
| 103 |
type recursiveEmbeddedByPointer struct { |
| 104 |
*recursiveEmbeddedInner |
| 105 |
} |
| 106 |
|
| 107 |
type recursiveEmbeddedByValue struct { |
| 108 |
recursiveEmbeddedInner |
| 109 |
Name string `json:"name"` |
| 110 |
} |
| 111 |
|
| 112 |
type recursiveCustomer struct { |
| 113 |
IDs []*recursiveIDMapping |
| 114 |
} |
| 115 |
|
| 116 |
type recursiveIDMapping struct { |
| 117 |
Customer *recursiveCustomer |
| 118 |
} |
| 119 |
|
| 120 |
type recursiveCustomerHolder struct { |
| 121 |
recursiveCustomer |
| 122 |
MyField string |
| 123 |
} |
| 124 |
|
| 125 |
// the recursive struct is embedded in the struct which it refers to. |
| 126 |
type recursiveLabel struct { |
| 127 |
Parts []*recursiveResLabel `json:"parts"` |
| 128 |
} |
| 129 |
|
| 130 |
type recursiveResLabel struct { |
| 131 |
ID int `json:"id"` |
| 132 |
recursiveLabel |
| 133 |
} |
| 134 |
|
| 135 |
func TestEncodeEmbeddedRecursiveStruct(t *testing.T) { |
| 136 |
for _, test := range []struct { |
| 137 |
name string |
| 138 |
v any |
| 139 |
}{ |
| 140 |
{"nil embedded pointer", &recursiveEmbeddedByPointer{}}, |
| 141 |
{"embedded pointer", &recursiveEmbeddedByPointer{&recursiveEmbeddedInner{}}}, |
| 142 |
{"embedded pointer with recursion", &recursiveEmbeddedByPointer{&recursiveEmbeddedInner{&recursiveEmbeddedInner{&recursiveEmbeddedInner{}}}}}, |
| 143 |
{"embedded value", recursiveEmbeddedByValue{Name: "n"}}, |
| 144 |
{"embedded value with recursion", recursiveEmbeddedByValue{recursiveEmbeddedInner{&recursiveEmbeddedInner{}}, "n"}}, |
| 145 |
{"embedded struct referring to itself through a slice", []recursiveCustomerHolder{{MyField: "111"}}}, |
| 146 |
{"populated embedded struct referring to itself", []recursiveCustomerHolder{{ |
| 147 |
recursiveCustomer: recursiveCustomer{IDs: []*recursiveIDMapping{{}, {Customer: &recursiveCustomer{IDs: []*recursiveIDMapping{{}}}}}}, |
| 148 |
MyField: "111", |
| 149 |
}}}, |
| 150 |
{"struct embedded in the struct it refers to", &recursiveLabel{Parts: []*recursiveResLabel{{ID: 1}}}}, |
| 151 |
{"nested struct embedded in the struct it refers to", &recursiveLabel{Parts: []*recursiveResLabel{ |
| 152 |
{ID: 1, recursiveLabel: recursiveLabel{Parts: []*recursiveResLabel{{ID: 2}, nil}}}, |
| 153 |
}}}, |
| 154 |
{"start from the embedding struct", &recursiveResLabel{ID: 1, recursiveLabel: recursiveLabel{Parts: []*recursiveResLabel{{ID: 2}}}}}, |
| 155 |
} { |
| 156 |
t.Run(test.name, func(t *testing.T) { |
| 157 |
expected, err := stdjson.Marshal(test.v) |
| 158 |
if err != nil { |
| 159 |
t.Fatal(err) |
| 160 |
} |
| 161 |
for i := 0; i < 2; i++ { |
| 162 |
got, err := json.Marshal(test.v) |
| 163 |
if err != nil { |
| 164 |
t.Fatalf("call %d: %v", i, err) |
| 165 |
} |
| 166 |
if string(got) != string(expected) { |
| 167 |
t.Fatalf("call %d: expected %s but got %s", i, expected, got) |
| 168 |
} |
| 169 |
} |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// The frame of the pointers of a recursive code must not overlap the frame it is jumped from. |
| 175 |
// Otherwise the indent to restore, which is kept at the end of a frame, is overwritten by a pointer, |
| 176 |
// and gigabytes of the indent are written. |
| 177 |
|
| 178 |
type recursiveIndentNode struct { |
| 179 |
Next *recursiveIndentNode `json:"next"` |
| 180 |
} |
| 181 |
|
| 182 |
type recursiveIndentTree struct { |
| 183 |
Name string `json:"name"` |
| 184 |
Children []recursiveIndentTree `json:"children"` |
| 185 |
} |
| 186 |
|
| 187 |
type recursiveIndentMutualA struct { |
| 188 |
B *recursiveIndentMutualB `json:"b"` |
| 189 |
} |
| 190 |
|
| 191 |
type recursiveIndentMutualB struct { |
| 192 |
Value int `json:"value"` |
| 193 |
A *recursiveIndentMutualA `json:"a"` |
| 194 |
} |
| 195 |
|
| 196 |
func TestEncodeDeepRecursiveStructWithIndent(t *testing.T) { |
| 197 |
const maxDepth = 6 |
| 198 |
values := map[string][]any{} |
| 199 |
for depth := 1; depth <= maxDepth; depth++ { |
| 200 |
node := &recursiveIndentNode{} |
| 201 |
tree := recursiveIndentTree{Name: "leaf"} |
| 202 |
mutual := &recursiveIndentMutualA{} |
| 203 |
for i := 1; i < depth; i++ { |
| 204 |
node = &recursiveIndentNode{Next: node} |
| 205 |
tree = recursiveIndentTree{Name: "node", Children: []recursiveIndentTree{tree, {Name: "sibling"}}} |
| 206 |
mutual = &recursiveIndentMutualA{B: &recursiveIndentMutualB{Value: i, A: mutual}} |
| 207 |
} |
| 208 |
values["pointer"] = append(values["pointer"], node) |
| 209 |
values["slice"] = append(values["slice"], tree) |
| 210 |
values["mutual"] = append(values["mutual"], mutual) |
| 211 |
values["in interface"] = append(values["in interface"], []any{node, map[string]any{"k": tree}}) |
| 212 |
} |
| 213 |
for name, vs := range values { |
| 214 |
t.Run(name, func(t *testing.T) { |
| 215 |
for i, v := range vs { |
| 216 |
depth := i + 1 |
| 217 |
expected, err := stdjson.Marshal(v) |
| 218 |
if err != nil { |
| 219 |
t.Fatal(err) |
| 220 |
} |
| 221 |
got, err := json.Marshal(v) |
| 222 |
if err != nil { |
| 223 |
t.Fatalf("depth %d: %v", depth, err) |
| 224 |
} |
| 225 |
if string(got) != string(expected) { |
| 226 |
t.Fatalf("depth %d: expected %s but got %s", depth, expected, got) |
| 227 |
} |
| 228 |
expectedIndent, err := stdjson.MarshalIndent(v, "", " ") |
| 229 |
if err != nil { |
| 230 |
t.Fatal(err) |
| 231 |
} |
| 232 |
gotIndent, err := json.MarshalIndent(v, "", " ") |
| 233 |
if err != nil { |
| 234 |
t.Fatalf("depth %d ( indent ): %v", depth, err) |
| 235 |
} |
| 236 |
if string(gotIndent) != string(expectedIndent) { |
| 237 |
// the broken output can be gigabytes, so it is not printed. |
| 238 |
t.Fatalf("depth %d ( indent ): expected %d bytes but got %d bytes", depth, len(expectedIndent), len(gotIndent)) |
| 239 |
} |
| 240 |
} |
| 241 |
}) |
| 242 |
} |
| 243 |
} |