Skip to content

Commit

Permalink
make non-matching types in arrays type "any"
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jul 25, 2024
1 parent 25237b6 commit 2ae1568
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
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 @@ -159,6 +159,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"
}

0 comments on commit 2ae1568

Please sign in to comment.