@@ -20,6 +20,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
20
20
let innerTabs = 0 ;
21
21
let parent = "" ;
22
22
let globallySeenTypeNames = [ ] ;
23
+ let previousParents = "" ;
23
24
24
25
try
25
26
{
@@ -129,7 +130,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
129
130
struct [ keyname ] = elem . value ;
130
131
omitempty [ keyname ] = elem . count != scopeLength ;
131
132
}
132
- parseStruct ( depth + 1 , innerTabs , struct , omitempty ) ; // finally parse the struct !!
133
+ parseStruct ( depth + 1 , innerTabs , struct , omitempty , previousParents ) ; // finally parse the struct !!
133
134
}
134
135
else if ( sliceType == "slice" ) {
135
136
parseScope ( scope [ 0 ] , depth )
@@ -152,7 +153,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
152
153
append ( parent )
153
154
}
154
155
}
155
- parseStruct ( depth + 1 , innerTabs , scope ) ;
156
+ parseStruct ( depth + 1 , innerTabs , scope , false , previousParents ) ;
156
157
}
157
158
}
158
159
else {
@@ -165,7 +166,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
165
166
}
166
167
}
167
168
168
- function parseStruct ( depth , innerTabs , scope , omitempty )
169
+ function parseStruct ( depth , innerTabs , scope , omitempty , oldParents )
169
170
{
170
171
if ( flatten ) {
171
172
stack . push (
@@ -194,14 +195,15 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
194
195
appender ( `${ parentType } struct {\n` ) ;
195
196
++ innerTabs ;
196
197
const keys = Object . keys ( scope ) ;
198
+ previousParents = parent
197
199
for ( let i in keys )
198
200
{
199
201
const keyname = getOriginalName ( keys [ i ] ) ;
200
202
indenter ( innerTabs )
201
203
let typename
202
204
// structs will be defined on the top level of the go file, so they need to be globally unique
203
205
if ( typeof scope [ keys [ i ] ] === "object" && scope [ keys [ i ] ] !== null ) {
204
- typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , parent )
206
+ typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , previousParents )
205
207
globallySeenTypeNames . push ( typename )
206
208
} else {
207
209
typename = uniqueTypeName ( format ( keyname ) , seenTypeNames )
@@ -220,20 +222,22 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
220
222
}
221
223
indenter ( -- innerTabs ) ;
222
224
appender ( "}" ) ;
225
+ previousParents = oldParents ;
223
226
}
224
227
else
225
228
{
226
229
append ( "struct {\n" ) ;
227
230
++ tabs ;
228
231
const keys = Object . keys ( scope ) ;
232
+ previousParents = parent
229
233
for ( let i in keys )
230
234
{
231
235
const keyname = getOriginalName ( keys [ i ] ) ;
232
236
indent ( tabs ) ;
233
237
let typename
234
238
// structs will be defined on the top level of the go file, so they need to be globally unique
235
239
if ( typeof scope [ keys [ i ] ] === "object" && scope [ keys [ i ] ] !== null ) {
236
- typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , parent )
240
+ typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , previousParents )
237
241
globallySeenTypeNames . push ( typename )
238
242
} else {
239
243
typename = uniqueTypeName ( format ( keyname ) , seenTypeNames )
@@ -256,6 +260,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
256
260
}
257
261
indent ( -- tabs ) ;
258
262
append ( "}" ) ;
263
+ previousParents = oldParents ;
259
264
}
260
265
if ( flatten )
261
266
accumulator += stack . pop ( ) ;
0 commit comments