Skip to content

Commit 3c7724a

Browse files
committed
fix naming of deep-nested duplicate objects
1 parent 4a88b58 commit 3c7724a

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

json-to-go.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

json-to-go.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ function testFiles() {
158158

159159
const testCases = [
160160
"duplicate-top-level-structs",
161+
"double-nested-objects",
161162
];
162163

163164
for (const testCase of testCases) {

tests/double-nested-objects.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type AutoGenerated struct {
2+
First First `json:"first"`
3+
Second Second `json:"second"`
4+
}
5+
type Type struct {
6+
Short string `json:"short"`
7+
Long string `json:"long"`
8+
}
9+
type First struct {
10+
ID int `json:"id"`
11+
Type Type `json:"type"`
12+
}
13+
type SecondType struct {
14+
Long string `json:"long"`
15+
}
16+
type Second struct {
17+
ID int `json:"id"`
18+
SecondType SecondType `json:"type"`
19+
}

tests/double-nested-objects.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"first": {
3+
"id": 1,
4+
"type": {
5+
"short": "owa",
6+
"long": "objectwitharray"
7+
}
8+
},
9+
"second": {
10+
"id": 2,
11+
"type": {
12+
"long": "object"
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)