Skip to content

Commit a35c83b

Browse files
mike-hosseinimholt
authored andcommitted
Fixes issue with duplicate non-anonymous structs (#49)
1 parent ce11b7f commit a35c83b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

json-to-go.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ function jsonToGo(json, typename, flatten = true)
1414
let go = "";
1515
let tabs = 0;
1616

17+
const seen = [];
18+
const stack = [];
1719
let accumulator = "";
1820
let innerTabs = 0;
19-
let stack = [];
2021
let parent = "";
2122

2223
try
@@ -149,7 +150,13 @@ function jsonToGo(json, typename, flatten = true)
149150

150151
if (flatten && depth >= 2)
151152
{
152-
appender(`type ${parent} ` + "struct {\n");
153+
const parentType = `type ${parent}`;
154+
if (seen.includes(parentType)) {
155+
stack.pop();
156+
return
157+
}
158+
seen.push(parentType);
159+
appender(`${parentType} struct {\n`);
153160
++innerTabs;
154161
const keys = Object.keys(scope);
155162
for (let i in keys)

0 commit comments

Comments
 (0)