Skip to content

Commit 3e02bb2

Browse files
authored
Prevent over-eager merging of Scalars of same type into Varied kind (#205)
1 parent 9b7ac47 commit 3e02bb2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

runner/shape.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ func shapeMerge(a Shape, b Shape) Shape {
267267
Logln(`Missing type equality condition for %s merge: [%#v] and [%#v].`, b.Kind, a, b)
268268
}
269269

270+
// Both scalars of same type; nothing to merge here
271+
if b.Kind == a.Kind && b.Kind == ScalarKind {
272+
if a.ScalarShape.Name == b.ScalarShape.Name {
273+
return a
274+
}
275+
}
276+
270277
// Otherwise is a scalar or dissimilar kind so becomes varied
271278
return addUniqueVaried(a, b)
272279
}

runner/shape_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ func TestGetShape(t *testing.T) {
108108
},
109109
},
110110
},
111+
{
112+
`[{"a": "9", "b": 1}, {"a": "2", "b": 2}, {"a": 1, "b": 3}]`,
113+
Shape{
114+
Kind: ArrayKind,
115+
ArrayShape: &ArrayShape{
116+
Children: Shape{
117+
Kind: ObjectKind,
118+
ObjectShape: &ObjectShape{
119+
Children: map[string]Shape{
120+
"a": {
121+
Kind: VariedKind,
122+
VariedShape: &VariedShape{
123+
Children: []Shape{
124+
{Kind: ScalarKind, ScalarShape: &ScalarShape{Name: StringScalar}},
125+
{Kind: ScalarKind, ScalarShape: &ScalarShape{Name: NumberScalar}},
126+
},
127+
},
128+
},
129+
"b": {
130+
Kind: ScalarKind, ScalarShape: &ScalarShape{Name: NumberScalar},
131+
},
132+
},
133+
},
134+
},
135+
},
136+
},
137+
},
111138
{
112139
`[{"a": {"b": 1}, "c": "2"}]`,
113140
Shape{

0 commit comments

Comments
 (0)