Skip to content

Commit

Permalink
treat empty objects as matching objects
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jul 31, 2024
1 parent 73be549 commit bee7877
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
continue;
}

// if both one of the two objects is empty assume they are identical
if (currentKeys.length == 0 && existingKeys.length > 0 ||
currentKeys.length > 0 && existingKeys.length == 0) {
allFields[keyname].count++;
insideOmitEmpty = true // as the whole object is empty, all nested elements are omitempty
continue;
}

const comparisonResult = compareObjectKeys(
Object.keys(currentValue),
Object.keys(existingValue)
Expand Down
1 change: 1 addition & 0 deletions json-to-go.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function testFiles() {
"double-nested-objects",
"array-with-nonmatching-types",
"array-with-mergable-objects",
"array-with-mergable-empty-object",
];

for (const testCase of testCases) {
Expand Down
14 changes: 14 additions & 0 deletions tests/array-with-mergable-empty-object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type AutoGenerated struct {
Booleanfield bool `json:"booleanfield"`
Somearray []Somearray `json:"somearray"`
Date string `json:"date"`
}
type Features struct {
Age int `json:"age,omitempty"`
Height int `json:"height,omitempty"`
}
type Somearray struct {
ID int `json:"id"`
Name string `json:"name"`
Features Features `json:"features"`
}
19 changes: 19 additions & 0 deletions tests/array-with-mergable-empty-object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"booleanfield": true,
"somearray": [
{
"id": 1,
"name": "John Doe",
"features": {
"age": 49,
"height": 175
}
},
{
"id": 3,
"name": "John Doe",
"features": {}
}
],
"date": "2024-07-24"
}

0 comments on commit bee7877

Please sign in to comment.