Skip to content

Add support for merged objects in arrays #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
treat empty objects as matching objects
  • Loading branch information
grische committed Jul 31, 2024
commit bee7877235fbfe5da35d3fea4f747a8f62267138
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"
}