@@ -20,6 +20,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
2020 let innerTabs = 0 ;
2121 let parent = "" ;
2222 let globallySeenTypeNames = [ ] ;
23+ let previousParents = "" ;
2324
2425 try
2526 {
@@ -129,7 +130,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
129130 struct [ keyname ] = elem . value ;
130131 omitempty [ keyname ] = elem . count != scopeLength ;
131132 }
132- parseStruct ( depth + 1 , innerTabs , struct , omitempty ) ; // finally parse the struct !!
133+ parseStruct ( depth + 1 , innerTabs , struct , omitempty , previousParents ) ; // finally parse the struct !!
133134 }
134135 else if ( sliceType == "slice" ) {
135136 parseScope ( scope [ 0 ] , depth )
@@ -152,7 +153,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
152153 append ( parent )
153154 }
154155 }
155- parseStruct ( depth + 1 , innerTabs , scope ) ;
156+ parseStruct ( depth + 1 , innerTabs , scope , false , previousParents ) ;
156157 }
157158 }
158159 else {
@@ -165,7 +166,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
165166 }
166167 }
167168
168- function parseStruct ( depth , innerTabs , scope , omitempty )
169+ function parseStruct ( depth , innerTabs , scope , omitempty , oldParents )
169170 {
170171 if ( flatten ) {
171172 stack . push (
@@ -194,14 +195,15 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
194195 appender ( `${ parentType } struct {\n` ) ;
195196 ++ innerTabs ;
196197 const keys = Object . keys ( scope ) ;
198+ previousParents = parent
197199 for ( let i in keys )
198200 {
199201 const keyname = getOriginalName ( keys [ i ] ) ;
200202 indenter ( innerTabs )
201203 let typename
202204 // structs will be defined on the top level of the go file, so they need to be globally unique
203205 if ( typeof scope [ keys [ i ] ] === "object" && scope [ keys [ i ] ] !== null ) {
204- typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , parent )
206+ typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , previousParents )
205207 globallySeenTypeNames . push ( typename )
206208 } else {
207209 typename = uniqueTypeName ( format ( keyname ) , seenTypeNames )
@@ -220,20 +222,22 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
220222 }
221223 indenter ( -- innerTabs ) ;
222224 appender ( "}" ) ;
225+ previousParents = oldParents ;
223226 }
224227 else
225228 {
226229 append ( "struct {\n" ) ;
227230 ++ tabs ;
228231 const keys = Object . keys ( scope ) ;
232+ previousParents = parent
229233 for ( let i in keys )
230234 {
231235 const keyname = getOriginalName ( keys [ i ] ) ;
232236 indent ( tabs ) ;
233237 let typename
234238 // structs will be defined on the top level of the go file, so they need to be globally unique
235239 if ( typeof scope [ keys [ i ] ] === "object" && scope [ keys [ i ] ] !== null ) {
236- typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , parent )
240+ typename = uniqueTypeName ( format ( keyname ) , globallySeenTypeNames , previousParents )
237241 globallySeenTypeNames . push ( typename )
238242 } else {
239243 typename = uniqueTypeName ( format ( keyname ) , seenTypeNames )
@@ -256,6 +260,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
256260 }
257261 indent ( -- tabs ) ;
258262 append ( "}" ) ;
263+ previousParents = oldParents ;
259264 }
260265 if ( flatten )
261266 accumulator += stack . pop ( ) ;
0 commit comments