@@ -102,7 +102,16 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
102
102
const existingValue = allFields [ keyname ] . value ;
103
103
const currentValue = scope [ i ] [ keyname ] ;
104
104
105
- if ( compareObjects ( existingValue , currentValue ) ) {
105
+ if ( ! areSameType ( existingValue , currentValue ) ) {
106
+ if ( existingValue !== null ) {
107
+ allFields [ keyname ] . value = null // force type "any" if types are not identical
108
+ console . warn ( `Warning: key "${ keyname } " uses multiple types. Defaulting to type "any".` )
109
+ }
110
+ allFields [ keyname ] . count ++
111
+ continue
112
+ }
113
+
114
+ if ( areObjects ( existingValue , currentValue ) ) {
106
115
const comparisonResult = compareObjectKeys (
107
116
Object . keys ( currentValue ) ,
108
117
Object . keys ( existingValue )
@@ -444,12 +453,19 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
444
453
return unique
445
454
}
446
455
447
- function compareObjects ( objectA , objectB ) {
456
+ function areObjects ( objectA , objectB ) {
448
457
const object = "[object Object]" ;
449
458
return Object . prototype . toString . call ( objectA ) === object
450
459
&& Object . prototype . toString . call ( objectB ) === object ;
451
460
}
452
461
462
+ function areSameType ( objectA , objectB ) {
463
+ // prototype.toString required to compare Arrays and Objects
464
+ const typeA = Object . prototype . toString . call ( objectA )
465
+ const typeB = Object . prototype . toString . call ( objectB )
466
+ return typeA === typeB
467
+ }
468
+
453
469
function compareObjectKeys ( itemAKeys , itemBKeys ) {
454
470
const lengthA = itemAKeys . length ;
455
471
const lengthB = itemBKeys . length ;
0 commit comments