|
| 1 | +#include <sourcemeta/blaze/documentation.h> |
| 2 | + |
| 3 | +#include <sourcemeta/blaze/alterschema.h> |
| 4 | + |
| 5 | +#include <sourcemeta/core/jsonschema.h> |
| 6 | + |
| 7 | +#include <cassert> // assert |
| 8 | +#include <utility> // std::move |
| 9 | + |
| 10 | +namespace sourcemeta::blaze { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +auto type_expression_of(const sourcemeta::core::JSON &schema) |
| 15 | + -> Documentation::Type::Expression { |
| 16 | + Documentation::Type::Expression expression; |
| 17 | + |
| 18 | + if (!schema.is_object()) { |
| 19 | + return expression; |
| 20 | + } |
| 21 | + |
| 22 | + if (schema.defines("enum") && schema.at("enum").is_array()) { |
| 23 | + Documentation::Type::Expression::Enumeration enumeration; |
| 24 | + for (const auto &value : schema.at("enum").as_array()) { |
| 25 | + enumeration.values.push_back(value); |
| 26 | + } |
| 27 | + expression.value = std::move(enumeration); |
| 28 | + return expression; |
| 29 | + } |
| 30 | + |
| 31 | + if (schema.defines("type") && schema.at("type").is_string()) { |
| 32 | + const auto &type{schema.at("type").to_string()}; |
| 33 | + if (type == "object") { |
| 34 | + expression.value = Documentation::Type::Expression::Object{}; |
| 35 | + } else if (type == "string") { |
| 36 | + expression.value = Documentation::Type::Expression::Primitive::String; |
| 37 | + } else if (type == "integer") { |
| 38 | + expression.value = Documentation::Type::Expression::Primitive::Integer; |
| 39 | + } else if (type == "number") { |
| 40 | + expression.value = Documentation::Type::Expression::Primitive::Number; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return expression; |
| 45 | +} |
| 46 | + |
| 47 | +auto notes_of(const sourcemeta::core::JSON &schema) -> Documentation::Notes { |
| 48 | + Documentation::Notes notes; |
| 49 | + if (!schema.is_object()) { |
| 50 | + return notes; |
| 51 | + } |
| 52 | + if (schema.defines("title") && schema.at("title").is_string()) { |
| 53 | + notes.title = schema.at("title").to_string(); |
| 54 | + } |
| 55 | + if (schema.defines("description") && schema.at("description").is_string()) { |
| 56 | + notes.description = schema.at("description").to_string(); |
| 57 | + } |
| 58 | + if (schema.defines("default")) { |
| 59 | + notes.default_value = schema.at("default"); |
| 60 | + } |
| 61 | + return notes; |
| 62 | +} |
| 63 | + |
| 64 | +auto is_required_property(const sourcemeta::core::JSON &schema, |
| 65 | + const sourcemeta::core::JSON::String &property) |
| 66 | + -> bool { |
| 67 | + if (!schema.is_object() || !schema.defines("required") || |
| 68 | + !schema.at("required").is_array()) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + for (const auto &item : schema.at("required").as_array()) { |
| 72 | + if (item.is_string() && item.to_string() == property) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + } |
| 76 | + return false; |
| 77 | +} |
| 78 | + |
| 79 | +auto walk_schema(const sourcemeta::core::JSON &schema, const bool include_root) |
| 80 | + -> Documentation; |
| 81 | + |
| 82 | +auto walk_properties(const sourcemeta::core::JSON &schema, |
| 83 | + std::vector<Documentation::Row> &rows) -> void { |
| 84 | + if (!schema.is_object() || !schema.defines("properties") || |
| 85 | + !schema.at("properties").is_object()) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + for (const auto &entry : schema.at("properties").as_object()) { |
| 90 | + Documentation::Row row; |
| 91 | + row.path = "/" + entry.first; |
| 92 | + row.type.expression = type_expression_of(entry.second); |
| 93 | + row.notes = notes_of(entry.second); |
| 94 | + row.required = is_required_property(schema, entry.first); |
| 95 | + rows.push_back(std::move(row)); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +auto walk_any_of(const sourcemeta::core::JSON &schema, |
| 100 | + std::vector<Documentation::Section> &children) -> void { |
| 101 | + if (!schema.is_object() || !schema.defines("anyOf") || |
| 102 | + !schema.at("anyOf").is_array()) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + Documentation::Section section; |
| 107 | + section.label = "Any of"; |
| 108 | + for (const auto &branch : schema.at("anyOf").as_array()) { |
| 109 | + section.children.push_back(walk_schema(branch, false)); |
| 110 | + } |
| 111 | + children.push_back(std::move(section)); |
| 112 | +} |
| 113 | + |
| 114 | +auto walk_schema(const sourcemeta::core::JSON &schema, const bool include_root) |
| 115 | + -> Documentation { |
| 116 | + Documentation documentation; |
| 117 | + |
| 118 | + if (include_root) { |
| 119 | + Documentation::Row root; |
| 120 | + root.path = "(root)"; |
| 121 | + root.type.expression = type_expression_of(schema); |
| 122 | + root.notes = notes_of(schema); |
| 123 | + documentation.rows.push_back(std::move(root)); |
| 124 | + } |
| 125 | + |
| 126 | + walk_properties(schema, documentation.rows); |
| 127 | + walk_any_of(schema, documentation.children); |
| 128 | + |
| 129 | + return documentation; |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace |
| 133 | + |
| 134 | +auto to_documentation(const sourcemeta::core::JSON &schema, |
| 135 | + const sourcemeta::core::SchemaWalker &walker, |
| 136 | + const sourcemeta::core::SchemaResolver &resolver) |
| 137 | + -> Documentation { |
| 138 | + // Canonicalize the schema for easier analysis |
| 139 | + sourcemeta::core::SchemaTransformer canonicalizer; |
| 140 | + sourcemeta::blaze::add(canonicalizer, |
| 141 | + sourcemeta::blaze::AlterSchemaMode::Canonicalizer); |
| 142 | + sourcemeta::core::JSON canonical{schema}; |
| 143 | + [[maybe_unused]] const auto canonicalized{canonicalizer.apply( |
| 144 | + canonical, walker, resolver, |
| 145 | + [](const auto &, const auto, const auto, const auto &, |
| 146 | + [[maybe_unused]] const auto applied) { assert(applied); })}; |
| 147 | + assert(canonicalized.first); |
| 148 | + |
| 149 | + // Frame the canonicalized schema with reference information |
| 150 | + sourcemeta::core::SchemaFrame frame{ |
| 151 | + sourcemeta::core::SchemaFrame::Mode::References}; |
| 152 | + frame.analyse(canonical, walker, resolver); |
| 153 | + |
| 154 | + return walk_schema(canonical, true); |
| 155 | +} |
| 156 | + |
| 157 | +} // namespace sourcemeta::blaze |
0 commit comments