Skip to content

Commit

Permalink
Merge pull request mholt#16 from yanpozka/master
Browse files Browse the repository at this point in the history
Support for array of structs (fixes mholt#6)
  • Loading branch information
mholt committed Feb 5, 2016
2 parents 5fc090e + 274a3d0 commit db97bb7
Showing 1 changed file with 62 additions and 17 deletions.
79 changes: 62 additions & 17 deletions json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ function jsonToGo(json, typename)
{
if (Array.isArray(scope))
{
var sliceType;
for (var i = 0; i < scope.length; i++)
var sliceType, scopeLength = scope.length;

for (var i = 0; i < scopeLength; i++)
{
var thisType = goType(scope[i]);
if (!sliceType)
Expand All @@ -55,33 +56,77 @@ function jsonToGo(json, typename)
break;
}
}

append("[]");
if (sliceType == "struct")
parseScope(scope[0]);
if (sliceType == "struct") {
var allFields = {};

// for each field counts how many times appears
for (var i = 0; i < scopeLength; i++)
{
var keys = Object.keys(scope[i])
for (var k in keys)
{
var keyname = keys[k];
if (!(keyname in allFields)) {
allFields[keyname] = {
value: scope[i][keyname],
count: 0
}
}

allFields[keyname].count++;
}
}

// create a common struct with all fields found in the current array
// omitempty dict indicates if a field is optional
var keys = Object.keys(allFields), struct = {}, omitempty = {};
for (var k in keys)
{
var keyname = keys[k], elem = allFields[keyname];

struct[keyname] = elem.value;
omitempty[keyname] = elem.count != scopeLength;
}

parseStruct(struct, omitempty); // finally parse the struct !!
}
else
append(sliceType || "interface{}");
}
else
{
append("struct {\n");
++tabs;
var keys = Object.keys(scope);
for (var i in keys)
{
var keyname = keys[i];
indent(tabs);
append(format(keyname)+" ");
parseScope(scope[keyname]);
append(' `json:"'+keyname+'"`\n');
}
indent(--tabs);
append("}");
parseStruct(scope);
}
}
else
append(goType(scope));
}

function parseStruct(scope, omitempty)
{
append("struct {\n");
++tabs;
var keys = Object.keys(scope);
for (var i in keys)
{
var keyname = keys[i];
indent(tabs);
append(format(keyname)+" ");
parseScope(scope[keyname]);

append(' `json:"'+keyname);
if (omitempty && omitempty[keyname] === true)
{
append(',omitempty');
}
append('"`\n');
}
indent(--tabs);
append("}");
}

function indent(tabs)
{
for (var i = 0; i < tabs; i++)
Expand Down

0 comments on commit db97bb7

Please sign in to comment.