Skip to content

Commit ce94e3d

Browse files
committed
treat empty objects as matching objects
1 parent 73be549 commit ce94e3d

4 files changed

+42
-0
lines changed

json-to-go.js

+8
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
156156
continue;
157157
}
158158

159+
// if both one of the two objects is empty assume they are identical
160+
if (currentKeys.length == 0 && existingKeys.length > 0 ||
161+
currentKeys.length > 0 && existingKeys.length == 0) {
162+
allFields[keyname].count++;
163+
insideOmitEmpty = true // as the whole object is empty, all nested elements are omitempty
164+
continue;
165+
}
166+
159167
const comparisonResult = compareObjectKeys(
160168
Object.keys(currentValue),
161169
Object.keys(existingValue)

json-to-go.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ function testFiles() {
164164
"double-nested-objects",
165165
"array-with-nonmatching-types",
166166
"array-with-mergable-objects",
167+
"array-with-mergable-empty-object",
167168
];
168169

169170
for (const testCase of testCases) {
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type AutoGenerated struct {
2+
Booleanfield bool `json:"booleanfield"`
3+
Somearray []Somearray `json:"somearray"`
4+
Date string `json:"date"`
5+
}
6+
type Features struct {
7+
Age int `json:"age,omitempty"`
8+
Height int `json:"height,omitempty"`
9+
}
10+
type Somearray struct {
11+
ID int `json:"id"`
12+
Name string `json:"name"`
13+
Features Features `json:"features"`
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"booleanfield": true,
3+
"somearray": [
4+
{
5+
"id": 1,
6+
"name": "John Doe",
7+
"features": {
8+
"age": 49,
9+
"height": 175
10+
}
11+
},
12+
{
13+
"id": 3,
14+
"name": "John Doe",
15+
"features": {}
16+
}
17+
],
18+
"date": "2024-07-24"
19+
}

0 commit comments

Comments
 (0)