Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include "flatbuffers/code_generators.h" |
| 19 | #include "flatbuffers/idl.h" |
| 20 | #include "flatbuffers/util.h" |
| 21 | |
| 22 | namespace flatbuffers { |
| 23 | |
| 24 | static std::string GeneratedFileName(const std::string &path, |
| 25 | const std::string &file_name) { |
| 26 | return path + file_name + ".schema.json"; |
| 27 | } |
| 28 | |
| 29 | namespace jsons { |
| 30 | |
| 31 | std::string GenNativeType(BaseType type) { |
| 32 | switch (type) { |
| 33 | case BASE_TYPE_BOOL: return "boolean"; |
| 34 | case BASE_TYPE_CHAR: |
| 35 | case BASE_TYPE_UCHAR: |
| 36 | case BASE_TYPE_SHORT: |
| 37 | case BASE_TYPE_USHORT: |
| 38 | case BASE_TYPE_INT: |
| 39 | case BASE_TYPE_UINT: |
| 40 | case BASE_TYPE_LONG: |
| 41 | case BASE_TYPE_ULONG: |
| 42 | case BASE_TYPE_FLOAT: |
| 43 | case BASE_TYPE_DOUBLE: return "number"; |
| 44 | case BASE_TYPE_STRING: return "string"; |
| 45 | case BASE_TYPE_ARRAY: return "array"; |
| 46 | default: return ""; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | template<class T> std::string GenFullName(const T *enum_def) { |
| 51 | std::string full_name; |
| 52 | const auto &name_spaces = enum_def->defined_namespace->components; |
| 53 | for (auto ns = name_spaces.cbegin(); ns != name_spaces.cend(); ++ns) { |
| 54 | full_name.append(*ns + "_"); |
| 55 | } |
| 56 | full_name.append(enum_def->name); |
| 57 | return full_name; |
| 58 | } |
| 59 | |
| 60 | template<class T> std::string GenTypeRef(const T *enum_def) { |
| 61 | return "\"$ref\" : \"#/definitions/" + GenFullName(enum_def) + "\""; |
| 62 | } |
| 63 | |
| 64 | std::string GenType(const std::string &name) { |
| 65 | return "\"type\" : \"" + name + "\""; |
| 66 | } |
| 67 | |
| 68 | std::string GenType(const Type &type) { |
| 69 | if (type.enum_def != nullptr && !type.enum_def->is_union) { |
| 70 | // it is a reference to an enum type |
| 71 | return GenTypeRef(type.enum_def); |
| 72 | } |
| 73 | switch (type.base_type) { |
| 74 | case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru |
| 75 | case BASE_TYPE_VECTOR: { |
| 76 | std::string typeline; |
| 77 | typeline.append("\"type\" : \"array\", \"items\" : { "); |
| 78 | if (type.element == BASE_TYPE_STRUCT) { |
| 79 | typeline.append(GenTypeRef(type.struct_def)); |
| 80 | } else { |
| 81 | typeline.append(GenType(GenNativeType(type.element))); |
| 82 | } |
| 83 | typeline.append(" }"); |
| 84 | return typeline; |
| 85 | } |
| 86 | case BASE_TYPE_STRUCT: { |
| 87 | return GenTypeRef(type.struct_def); |
| 88 | } |
| 89 | case BASE_TYPE_UNION: { |
| 90 | std::string union_type_string("\"anyOf\": ["); |
| 91 | const auto &union_types = type.enum_def->Vals(); |
| 92 | for (auto ut = union_types.cbegin(); ut < union_types.cend(); ++ut) { |
| 93 | auto &union_type = *ut; |
| 94 | if (union_type->union_type.base_type == BASE_TYPE_NONE) { continue; } |
| 95 | if (union_type->union_type.base_type == BASE_TYPE_STRUCT) { |
| 96 | union_type_string.append( |
| 97 | "{ " + GenTypeRef(union_type->union_type.struct_def) + " }"); |
| 98 | } |
| 99 | if (union_type != *type.enum_def->Vals().rbegin()) { |
| 100 | union_type_string.append(","); |
| 101 | } |
| 102 | } |
| 103 | union_type_string.append("]"); |
| 104 | return union_type_string; |
| 105 | } |
| 106 | case BASE_TYPE_UTYPE: return GenTypeRef(type.enum_def); |
| 107 | default: return GenType(GenNativeType(type.base_type)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | class JsonSchemaGenerator : public BaseGenerator { |
| 112 | private: |
| 113 | CodeWriter code_; |
| 114 | |
| 115 | public: |
| 116 | JsonSchemaGenerator(const Parser &parser, const std::string &path, |
| 117 | const std::string &file_name) |
| 118 | : BaseGenerator(parser, path, file_name, "", "") {} |
| 119 | |
| 120 | explicit JsonSchemaGenerator(const BaseGenerator &base_generator) |
| 121 | : BaseGenerator(base_generator) {} |
| 122 | |
| 123 | bool generate() { |
| 124 | code_.Clear(); |
| 125 | code_ += "{"; |
| 126 | code_ += " \"$schema\": \"http://json-schema.org/draft-04/schema#\","; |
| 127 | code_ += " \"definitions\": {"; |
| 128 | for (auto e = parser_.enums_.vec.cbegin(); e != parser_.enums_.vec.cend(); |
| 129 | ++e) { |
| 130 | code_ += " \"" + GenFullName(*e) + "\" : {"; |
| 131 | code_ += " " + GenType("string") + ","; |
| 132 | std::string enumdef(" \"enum\": ["); |
| 133 | for (auto enum_value = (*e)->Vals().begin(); |
| 134 | enum_value != (*e)->Vals().end(); ++enum_value) { |
| 135 | enumdef.append("\"" + (*enum_value)->name + "\""); |
| 136 | if (*enum_value != (*e)->Vals().back()) { enumdef.append(", "); } |
| 137 | } |
| 138 | enumdef.append("]"); |
| 139 | code_ += enumdef; |
| 140 | code_ += " },"; // close type |
| 141 | } |
| 142 | for (auto s = parser_.structs_.vec.cbegin(); |
| 143 | s != parser_.structs_.vec.cend(); ++s) { |
| 144 | const auto &structure = *s; |
| 145 | code_ += " \"" + GenFullName(structure) + "\" : {"; |
| 146 | code_ += " " + GenType("object") + ","; |
| 147 | std::string comment; |
| 148 | const auto &comment_lines = structure->doc_comment; |
| 149 | for (auto comment_line = comment_lines.cbegin(); |
| 150 | comment_line != comment_lines.cend(); ++comment_line) { |
| 151 | comment.append(*comment_line); |
| 152 | } |
| 153 | if (comment.size() > 0) { |
| 154 | code_ += " \"description\" : \"" + comment + "\","; |
| 155 | } |
| 156 | code_ += " \"properties\" : {"; |
| 157 | |
| 158 | const auto &properties = structure->fields.vec; |
| 159 | for (auto prop = properties.cbegin(); prop != properties.cend(); ++prop) { |
| 160 | const auto &property = *prop; |
| 161 | std::string arrayInfo = ""; |
| 162 | if (IsArray(property->value.type)) { |
| 163 | arrayInfo = ",\n \"minItems\": " + |
| 164 | NumToString(property->value.type.fixed_length) + |
| 165 | ",\n \"maxItems\": " + |
| 166 | NumToString(property->value.type.fixed_length); |
| 167 | } |
| 168 | std::string typeLine = |
| 169 | " \"" + property->name + "\" : {\n" + " " + |
| 170 | GenType(property->value.type) + arrayInfo + "\n }"; |
| 171 | if (property != properties.back()) { typeLine.append(","); } |
| 172 | code_ += typeLine; |
| 173 | } |
| 174 | code_ += " },"; // close properties |
| 175 | |
| 176 | std::vector<FieldDef *> requiredProperties; |
| 177 | std::copy_if(properties.begin(), properties.end(), |
| 178 | back_inserter(requiredProperties), |
| 179 | [](FieldDef const *prop) { return prop->required; }); |
| 180 | if (requiredProperties.size() > 0) { |
| 181 | std::string required_string(" \"required\" : ["); |
| 182 | for (auto req_prop = requiredProperties.cbegin(); |
| 183 | req_prop != requiredProperties.cend(); ++req_prop) { |
| 184 | required_string.append("\"" + (*req_prop)->name + "\""); |
| 185 | if (*req_prop != requiredProperties.back()) { |
| 186 | required_string.append(", "); |
| 187 | } |
| 188 | } |
| 189 | required_string.append("],"); |
| 190 | code_ += required_string; |
| 191 | } |
| 192 | code_ += " \"additionalProperties\" : false"; |
| 193 | std::string closeType(" }"); |
| 194 | if (*s != parser_.structs_.vec.back()) { closeType.append(","); } |
| 195 | code_ += closeType; // close type |
| 196 | } |
| 197 | code_ += " },"; // close definitions |
| 198 | |
| 199 | // mark root type |
| 200 | code_ += " \"$ref\" : \"#/definitions/" + |
| 201 | GenFullName(parser_.root_struct_def_) + "\""; |
| 202 | |
| 203 | code_ += "}"; // close schema root |
| 204 | const std::string file_path = GeneratedFileName(path_, file_name_); |
| 205 | const std::string final_code = code_.ToString(); |
| 206 | return SaveFile(file_path.c_str(), final_code, false); |
| 207 | } |
| 208 | }; |
| 209 | } // namespace jsons |
| 210 | |
| 211 | bool GenerateJsonSchema(const Parser &parser, const std::string &path, |
| 212 | const std::string &file_name) { |
| 213 | jsons::JsonSchemaGenerator generator(parser, path, file_name); |
| 214 | return generator.generate(); |
| 215 | } |
| 216 | } // namespace flatbuffers |