Skip to content
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

make non-matching types in arrays type "any" #133

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
const existingValue = allFields[keyname].value;
const currentValue = scope[i][keyname];

if (compareObjects(existingValue, currentValue)) {
if (!areSameType(existingValue, currentValue)) {
if(existingValue !== null) {
allFields[keyname].value = null // force type "any" if types are not identical
console.warn(`Warning: key "${keyname}" uses multiple types. Defaulting to type "any".`)
}
allFields[keyname].count++
continue
}

if (areObjects(existingValue, currentValue)) {
const comparisonResult = compareObjectKeys(
Object.keys(currentValue),
Object.keys(existingValue)
Expand Down Expand Up @@ -444,12 +453,19 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
return unique
}

function compareObjects(objectA, objectB) {
function areObjects(objectA, objectB) {
const object = "[object Object]";
return Object.prototype.toString.call(objectA) === object
&& Object.prototype.toString.call(objectB) === object;
}

function areSameType(objectA, objectB) {
// prototype.toString required to compare Arrays and Objects
const typeA = Object.prototype.toString.call(objectA)
const typeB = Object.prototype.toString.call(objectB)
return typeA === typeB
}

function compareObjectKeys(itemAKeys, itemBKeys) {
const lengthA = itemAKeys.length;
const lengthB = itemBKeys.length;
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 @@ -162,6 +162,7 @@ function testFiles() {
const testCases = [
"duplicate-top-level-structs",
"double-nested-objects",
"array-with-nonmatching-types",
];

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