forked from Telodendria/Telodendria
Add support for arrays of primitives.
This commit is contained in:
parent
942d2aad18
commit
e9600a21e9
1 changed files with 146 additions and 33 deletions
|
@ -630,6 +630,7 @@ Main(Array * args)
|
||||||
fieldType++;
|
fieldType++;
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
fieldType[strlen(fieldType) - 1] = '\0';
|
||||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||||
|
jsonType = isEnum ? JSON_STRING : TypeToJsonType(fieldType);
|
||||||
|
|
||||||
StreamPrintf(implFile, " out->%s = ArrayCreate();\n", key);
|
StreamPrintf(implFile, " out->%s = ArrayCreate();\n", key);
|
||||||
StreamPrintf(implFile, " if (!out->%s)\n", key);
|
StreamPrintf(implFile, " if (!out->%s)\n", key);
|
||||||
|
@ -644,13 +645,77 @@ Main(Array * args)
|
||||||
StreamPrintf(implFile, "\n");
|
StreamPrintf(implFile, "\n");
|
||||||
StreamPrintf(implFile, " for (i = 0; i <ArraySize(arr); i++)\n");
|
StreamPrintf(implFile, " for (i = 0; i <ArraySize(arr); i++)\n");
|
||||||
StreamPrintf(implFile, " {\n");
|
StreamPrintf(implFile, " {\n");
|
||||||
|
StreamPrintf(implFile, " JsonValue *v = ArrayGet(arr, i);\n");
|
||||||
|
|
||||||
|
if (StrEquals(fieldType, "integer") ||
|
||||||
|
StrEquals(fieldType, "float") ||
|
||||||
|
StrEquals(fieldType, "boolean"))
|
||||||
|
{
|
||||||
|
char *cType;
|
||||||
|
|
||||||
|
if (StrEquals(fieldType, "integer"))
|
||||||
|
{
|
||||||
|
cType = "long";
|
||||||
|
}
|
||||||
|
else if (StrEquals(fieldType, "float"))
|
||||||
|
{
|
||||||
|
cType = "double";
|
||||||
|
}
|
||||||
|
else if (StrEquals(fieldType, "boolean"))
|
||||||
|
{
|
||||||
|
cType = "int";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Should never happen */
|
||||||
|
cType = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*fieldType = toupper(*fieldType);
|
||||||
|
|
||||||
|
StreamPrintf(implFile, " %s *ref;\n", cType);
|
||||||
|
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||||
|
StreamPrintf(implFile, " {\n");
|
||||||
|
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||||
|
StreamPrintf(implFile, " return 0;\n");
|
||||||
|
StreamPrintf(implFile, " }\n");
|
||||||
|
StreamPrintf(implFile, " ref = Malloc(sizeof(%s));\n", cType);
|
||||||
|
StreamPrintf(implFile, " if (!ref)\n");
|
||||||
|
StreamPrintf(implFile, " {\n");
|
||||||
|
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for array value.\";\n");
|
||||||
|
StreamPrintf(implFile, " return 0;\n");
|
||||||
|
StreamPrintf(implFile, " }\n");
|
||||||
|
StreamPrintf(implFile, " *ref = JsonValueAs%s(v);\n", fieldType);
|
||||||
|
StreamPrintf(implFile, " ArrayAdd(out->%s, ref);\n", key);
|
||||||
|
|
||||||
|
*fieldType = tolower(*fieldType);
|
||||||
|
}
|
||||||
|
else if (StrEquals(fieldType, "string"))
|
||||||
|
{
|
||||||
|
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||||
|
StreamPrintf(implFile, " {\n");
|
||||||
|
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||||
|
StreamPrintf(implFile, " return 0;\n");
|
||||||
|
StreamPrintf(implFile, " }\n");
|
||||||
|
StreamPrintf(implFile, " ArrayAdd(out->%s, StrDuplicate(JsonValueAsString(v)));\n", key);
|
||||||
|
}
|
||||||
|
else if (StrEquals(fieldType, "object"))
|
||||||
|
{
|
||||||
|
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||||
|
StreamPrintf(implFile, " {\n");
|
||||||
|
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||||
|
StreamPrintf(implFile, " return 0;\n");
|
||||||
|
StreamPrintf(implFile, " }\n");
|
||||||
|
StreamPrintf(implFile, " ArrayAdd(out->%s, JsonDuplicate(JsonValueAsObject(v)));\n", key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (isEnum)
|
if (isEnum)
|
||||||
{
|
{
|
||||||
StreamPrintf(implFile, " int parseResult;\n");
|
StreamPrintf(implFile, " int parseResult;\n");
|
||||||
}
|
}
|
||||||
StreamPrintf(implFile, " %s *parsed;\n", fieldType);
|
StreamPrintf(implFile, " %s *parsed;\n", fieldType);
|
||||||
StreamPrintf(implFile, " JsonValue *v = ArrayGet(arr, i);\n");
|
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", isEnum ? "JSON_STRING" : "JSON_OBJECT");
|
|
||||||
StreamPrintf(implFile, " {\n");
|
StreamPrintf(implFile, " {\n");
|
||||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||||
StreamPrintf(implFile, " return 0;\n");
|
StreamPrintf(implFile, " return 0;\n");
|
||||||
|
@ -680,6 +745,8 @@ Main(Array * args)
|
||||||
StreamPrintf(implFile, " return 0;\n");
|
StreamPrintf(implFile, " return 0;\n");
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
StreamPrintf(implFile, " ArrayAdd(out->%s, parsed);\n", key);
|
StreamPrintf(implFile, " ArrayAdd(out->%s, parsed);\n", key);
|
||||||
|
}
|
||||||
|
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
|
|
||||||
|
@ -795,9 +862,15 @@ Main(Array * args)
|
||||||
}
|
}
|
||||||
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||||
{
|
{
|
||||||
|
int isPrimitive;
|
||||||
|
|
||||||
fieldType++;
|
fieldType++;
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
fieldType[strlen(fieldType) - 1] = '\0';
|
||||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||||
|
isPrimitive = StrEquals(fieldType, "integer") ||
|
||||||
|
StrEquals(fieldType, "boolean") ||
|
||||||
|
StrEquals(fieldType, "float");
|
||||||
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
StreamPrintf(implFile, " if (val->%s)\n", key);
|
||||||
StreamPrintf(implFile, " {\n");
|
StreamPrintf(implFile, " {\n");
|
||||||
|
@ -810,7 +883,42 @@ Main(Array * args)
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
StreamPrintf(implFile, " for (i = 0; i < ArraySize(val->%s); i++)\n", key);
|
StreamPrintf(implFile, " for (i = 0; i < ArraySize(val->%s); i++)\n", key);
|
||||||
StreamPrintf(implFile, " {\n");
|
StreamPrintf(implFile, " {\n");
|
||||||
|
|
||||||
|
if (StrEquals(fieldType, "string"))
|
||||||
|
{
|
||||||
|
StreamPrintf(implFile, " ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->%s, i)));\n", key);
|
||||||
|
}
|
||||||
|
else if (!isPrimitive)
|
||||||
|
{
|
||||||
StreamPrintf(implFile, " ArrayAdd(jsonArr, JsonValueObject(%sToJson(ArrayGet(val->%s, i))));\n", fieldType, key);
|
StreamPrintf(implFile, " ArrayAdd(jsonArr, JsonValueObject(%sToJson(ArrayGet(val->%s, i))));\n", fieldType, key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char *cType;
|
||||||
|
|
||||||
|
if (StrEquals(fieldType, "integer"))
|
||||||
|
{
|
||||||
|
cType = "long";
|
||||||
|
}
|
||||||
|
else if (StrEquals(fieldType, "float"))
|
||||||
|
{
|
||||||
|
cType = "double";
|
||||||
|
}
|
||||||
|
else if (StrEquals(fieldType, "boolean"))
|
||||||
|
{
|
||||||
|
cType = "int";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Should never happen */
|
||||||
|
cType = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*fieldType = toupper(*fieldType);
|
||||||
|
StreamPrintf(implFile, " ArrayAdd(jsonArr, (JsonValue%s(*((%s *) ArrayGet(val->%s, i)))));\n", fieldType, cType, key);
|
||||||
|
*fieldType = tolower(*fieldType);
|
||||||
|
}
|
||||||
|
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueArray(jsonArr));\n", key);
|
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueArray(jsonArr));\n", key);
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
|
@ -875,16 +983,22 @@ Main(Array * args)
|
||||||
}
|
}
|
||||||
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||||
{
|
{
|
||||||
|
int isPrimitive;
|
||||||
|
|
||||||
fieldType++;
|
fieldType++;
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
fieldType[strlen(fieldType) - 1] = '\0';
|
||||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||||
|
isPrimitive = StrEquals(fieldType, "boolean") ||
|
||||||
|
StrEquals(fieldType, "float") ||
|
||||||
|
StrEquals(fieldType, "integer") ||
|
||||||
|
StrEquals(fieldType, "string");
|
||||||
|
|
||||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
StreamPrintf(implFile, " if (val->%s)\n", key);
|
||||||
StreamPrintf(implFile, " {\n");
|
StreamPrintf(implFile, " {\n");
|
||||||
StreamPrintf(implFile, " size_t i;\n");
|
StreamPrintf(implFile, " size_t i;\n");
|
||||||
StreamPrintf(implFile, " for (i = 0; i < ArraySize(val->%s); i++)\n", key);
|
StreamPrintf(implFile, " for (i = 0; i < ArraySize(val->%s); i++)\n", key);
|
||||||
StreamPrintf(implFile, " {\n");
|
StreamPrintf(implFile, " {\n");
|
||||||
StreamPrintf(implFile, " %sFree(ArrayGet(val->%s, i));\n", fieldType, key);
|
StreamPrintf(implFile, " %sFree(ArrayGet(val->%s, i));\n", (!isEnum && !isPrimitive) ? fieldType : "", key);
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
StreamPrintf(implFile, " ArrayFree(val->%s);\n", key);
|
StreamPrintf(implFile, " ArrayFree(val->%s);\n", key);
|
||||||
StreamPrintf(implFile, " }\n");
|
StreamPrintf(implFile, " }\n");
|
||||||
|
@ -901,7 +1015,6 @@ Main(Array * args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StreamPrintf(implFile, "}\n\n");
|
StreamPrintf(implFile, "}\n\n");
|
||||||
|
|
||||||
StreamPutc(headerFile, '\n');
|
StreamPutc(headerFile, '\n');
|
||||||
}
|
}
|
||||||
else if (StrEquals(typeType, "enum"))
|
else if (StrEquals(typeType, "enum"))
|
||||||
|
|
Loading…
Reference in a new issue