Add support for fixed-size arrays (#5313)

This commit is contained in:
svenk177
2019-06-18 00:15:13 +02:00
committed by Wouter van Oortmerssen
parent 0d2cebccfe
commit e635141d5b
40 changed files with 2113 additions and 220 deletions

View File

@@ -42,6 +42,7 @@ std::string GenNativeType(BaseType type) {
case BASE_TYPE_FLOAT:
case BASE_TYPE_DOUBLE: return "number";
case BASE_TYPE_STRING: return "string";
case BASE_TYPE_ARRAY: return "array";
default: return "";
}
}
@@ -70,6 +71,7 @@ std::string GenType(const Type &type) {
return GenTypeRef(type.enum_def);
}
switch (type.base_type) {
case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru
case BASE_TYPE_VECTOR: {
std::string typeline;
typeline.append("\"type\" : \"array\", \"items\" : { ");
@@ -156,8 +158,16 @@ class JsonSchemaGenerator : public BaseGenerator {
const auto &properties = structure->fields.vec;
for (auto prop = properties.cbegin(); prop != properties.cend(); ++prop) {
const auto &property = *prop;
std::string typeLine(" \"" + property->name + "\" : { " +
GenType(property->value.type) + " }");
std::string arrayInfo = "";
if (IsArray(property->value.type)) {
arrayInfo = ",\n \"minItems\": " +
NumToString(property->value.type.fixed_length) +
",\n \"maxItems\": " +
NumToString(property->value.type.fixed_length);
}
std::string typeLine =
" \"" + property->name + "\" : {\n" + " " +
GenType(property->value.type) + arrayInfo + "\n }";
if (property != properties.back()) { typeLine.append(","); }
code_ += typeLine;
}