载入中...
搜索中...
未找到
PropertySchemaJson.cpp
浏览该文件的文档.
2
4
5namespace eve::editing {
6namespace {
7
8const char* propertyTypeName(PropertyType type) {
9 switch (type) {
10 case PropertyType::Bool: return "boolean";
11 case PropertyType::Int: return "integer";
12 case PropertyType::Float: return "number";
13 case PropertyType::String: return "string";
14 case PropertyType::Enum: return "string";
15 case PropertyType::Color: return "object";
16 case PropertyType::Vec2: return "array";
17 case PropertyType::Vec3: return "array";
18 case PropertyType::Vec4: return "array";
19 case PropertyType::Transform: return "object";
20 case PropertyType::AssetRef: return "string";
21 case PropertyType::ObjectRef: return "string";
22 case PropertyType::Struct: return "object";
23 case PropertyType::Array: return "array";
24 case PropertyType::Map: return "object";
25 case PropertyType::Action: return "object";
26 case PropertyType::ReadOnlyText: return "string";
27 }
28 return "string";
29}
30
31Value propertyDescriptorToJsonSchema(const PropertyDescriptor& descriptor) {
33 node["type"] = propertyTypeName(descriptor.type);
34 node["title"] = descriptor.displayNameKey;
35 node["description"] = descriptor.descriptionKey;
36 node["category"] = descriptor.category;
37 if (descriptor.type == PropertyType::Enum && !descriptor.enumItems.empty()) {
38 Value::Array items;
39 items.reserve(descriptor.enumItems.size());
40 for (const std::string& item : descriptor.enumItems) items.emplace_back(item);
41 node["enum"] = std::move(items);
42 }
45 node["items"] = Value::Object{{"type", "number"}};
46 const int count = descriptor.type == PropertyType::Vec2 ? 2 : (descriptor.type == PropertyType::Vec3 ? 3 : 4);
47 node["minItems"] = count;
48 node["maxItems"] = count;
49 }
50 if (descriptor.numeric.minimum) node["minimum"] = *descriptor.numeric.minimum;
51 if (descriptor.numeric.maximum) node["maximum"] = *descriptor.numeric.maximum;
52 if (hasPropertyFlag(descriptor.flags, PropertyFlag::ReadOnly)) node["readOnly"] = true;
53 return node;
54}
55
56} // namespace
57
58PropertySchema emptyObjectPropertySchema(std::string_view typeId) {
59 PropertySchema schema;
60 schema.typeId = std::string(typeId);
61 schema.version = 1;
62 return schema;
63}
64
65Result<Value> propertySchemaToJsonSchema(const PropertySchema& schema, std::string_view schemaId) {
66 if (schemaId.empty())
67 return failed<Value>(Status::Rejected, RuleId("editing.schema.empty-id"),
68 "JSON Schema projection requires a non-empty schema id");
69
70 if (schema.documentSchema.type() != Value::Type::Null) {
71 if (!schema.documentSchema.getIf<Value::Object>() || !schema.properties.empty())
72 return failed<Value>(Status::Rejected, RuleId("editing.schema.conflicting-shapes"),
73 "Document schema must be an object and cannot coexist with flat properties");
74 return applied<Value>(schema.documentSchema);
75 }
76
78 root["$schema"] = "https://json-schema.org/draft/2020-12/schema";
79 root["$id"] = std::string(schemaId);
80 root["type"] = "object";
81 root["typeId"] = schema.typeId.empty() ? std::string(schemaId) : schema.typeId;
82 root["version"] = static_cast<std::int64_t>(schema.version);
83
86 for (const PropertyDescriptor& descriptor : schema.properties) {
87 const std::string key = descriptor.path.value();
88 if (key.empty())
89 return failed<Value>(Status::Rejected, RuleId("editing.schema.empty-path"),
90 "PropertySchema entries must use a non-empty path");
91 properties.emplace(key, propertyDescriptorToJsonSchema(descriptor));
93 }
94 root["properties"] = std::move(properties);
95 root["required"] = std::move(required);
96 root["additionalProperties"] = false;
97 return applied<Value>(Value(std::move(root)));
98}
99
100} // namespace eve::editing
int root
Definition AnimSmr.cpp:119
std::string descriptor
SQObjectPtr key
Definition ECS.cpp:155
std::map< std::string, PropertyRule > properties
bool required
const RoadNode * node
int count
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
Type type() const
Return the active value type.
const T * getIf() const
Return an immediately borrowed typed value.
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
PropertyType
PropertyType public API.
StrongId< RuleIdTag > RuleId
Definition EditingIds.h:60
Result< Value > propertySchemaToJsonSchema(const PropertySchema &schema, std::string_view schemaId)
Project a PropertySchema into a JSON Schema object tree (pointer-free Value).
PropertySchema emptyObjectPropertySchema(std::string_view typeId)
Build an explicit empty-object payload schema.
constexpr bool hasPropertyFlag(PropertyFlag value, PropertyFlag flag)
True when property flag.
PropertyDescriptor public API.
EVENGINE_API_PLATFORM public API.
std::vector< PropertyDescriptor > properties
Value documentSchema
Owned complete JSON Schema for nested documents; mutually exclusive with properties.