@@ -75,6 +75,8 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
75
75
appender ( slice ) ;
76
76
else
77
77
append ( slice )
78
+
79
+ // TODO: structs need a proper recursive solution to fix omitempty and nesting
78
80
if ( sliceType == "struct" ) {
79
81
const allFields = { } ;
80
82
@@ -96,6 +98,28 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
96
98
const currentValue = scope [ i ] [ keyname ] ;
97
99
98
100
if ( compareObjects ( existingValue , currentValue ) ) {
101
+ const currentKeys = Object . keys ( currentValue )
102
+ const existingKeys = Object . keys ( existingValue )
103
+
104
+ // try to merge two object fields instead of creating separate ones
105
+ if ( typeof existingValue === "object"
106
+ && existingKeys . length > 0
107
+ && typeof currentValue === "object"
108
+ && currentKeys . length > 0
109
+ ) {
110
+ var mergedValues = existingValue
111
+ for ( const key of currentKeys ) {
112
+ if ( ! Object . keys ( mergedValues ) . includes ( key ) ) {
113
+ mergedValues [ key ] = currentValue [ key ]
114
+ // TODO: find a proper handling of omitempty for nested structs
115
+ allOmitempty = true
116
+ }
117
+ }
118
+ allFields [ keyname ] . value = mergedValues ;
119
+ allFields [ keyname ] . count ++ ;
120
+ continue ;
121
+ }
122
+
99
123
const comparisonResult = compareObjectKeys (
100
124
Object . keys ( currentValue ) ,
101
125
Object . keys ( existingValue )
0 commit comments