载入中...
搜索中...
未找到
PropertyAccess.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <utility>
6
8
10 const auto found = std::find_if(properties.begin(), properties.end(),
11 [&path](const PropertyDescriptor &property) { return property.path == path; });
14}
15
17 if (!std::isfinite(number)) return rejected("property_access.property.finite", "Numeric values must be finite");
18 if (property.numeric.minimum && number < *property.numeric.minimum)
19 return rejected("property_access.property.minimum", "Value is below the minimum");
20 if (property.numeric.maximum && number > *property.numeric.maximum)
21 return rejected("property_access.property.maximum", "Value is above the maximum");
22 return accepted();
23}
24
26 std::size_t expected = 0;
27 switch (kind) {
28 case PropertyKind::Vec2: expected = 2; break;
29 case PropertyKind::Vec3: expected = 3; break;
31 case PropertyKind::Color: expected = 4; break;
32 default: return accepted();
33 }
34 if (items.size() != expected)
35 return rejected("property_access.property.arity", "Composite property has the wrong component count");
36 return accepted();
37}
38
39Result<void> validatePropertyValue(const PropertyDescriptor &property, const Value &value) {
41 return rejected("property_access.property.read-only", "Property is read-only");
42
43 const Value::Type type = value.type();
44 bool compatible = false;
45 switch (property.kind) {
46 case PropertyKind::Auto: compatible = true; break;
47 case PropertyKind::Bool: compatible = type == Value::Type::Bool; break;
48 case PropertyKind::Integer: compatible = type == Value::Type::Integer; break;
54 case PropertyKind::ReadOnlyText: compatible = type == Value::Type::String; break;
59 case PropertyKind::Array: compatible = type == Value::Type::Array; break;
61 case PropertyKind::Map: compatible = type == Value::Type::Object; break;
62 case PropertyKind::Action: compatible = true; break;
63 }
64 if (!compatible) return rejected("property_access.property.type", "Property value type does not match schema");
65
66 if (property.kind == PropertyKind::Enum) {
67 const auto *selected = value.getIf<std::string>();
68 if (!selected ||
69 std::find(property.choices.begin(), property.choices.end(), *selected) == property.choices.end())
70 return rejected("property_access.property.choice", "Value is not an allowed choice");
71 }
72
73 if (const auto *items = value.getIf<Value::Array>()) {
74 Result<void> arity = validateArrayArity(property.kind, *items);
75 if (!arity.ok()) return arity;
76 if (property.kind == PropertyKind::Color || property.kind == PropertyKind::Vec2 ||
77 property.kind == PropertyKind::Vec3 || property.kind == PropertyKind::Vec4) {
78 for (const Value &component : *items) {
79 double number = 0.0;
80 if (const auto *integer = component.getIf<std::int64_t>()) {
81 number = static_cast<double>(*integer);
82 } else if (const auto *floating = component.getIf<double>()) {
83 number = *floating;
84 } else {
85 return rejected("property_access.property.type", "Composite components must be numeric");
86 }
87 Result<void> componentResult = validateNumericComponent(property, number);
88 if (!componentResult.ok()) return componentResult;
89 }
90 return accepted();
91 }
92 }
93
94 double number = 0.0;
95 bool hasNumber = false;
96 if (const auto *integer = value.getIf<std::int64_t>()) {
97 number = static_cast<double>(*integer);
98 hasNumber = true;
99 } else if (const auto *floating = value.getIf<double>()) {
100 number = *floating;
101 hasNumber = true;
102 }
103 if (hasNumber) return validateNumericComponent(property, number);
104 return accepted();
105}
106
107} // namespace eve::property_access
std::string selected
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
double number
bool found
std::string kind
Move-only operation result carrying either a value or Status.
Definition Result.h:176
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:286
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Type
Runtime kind of the owned value.
Definition Value.h:38
@ Integer
Compatibility spelling for Int64.
@ Number
Compatibility spelling for Double.
std::vector< Value > Array
Definition Value.h:33
Result< void > validateArrayArity(PropertyKind kind, const Value::Array &items)
Result< void > rejected(std::string rule, std::string message, DiagnosticCode code=DiagnosticCode::PreconditionViolation)
Build a rejected write/validation result with a stable rule string.
Result< void > validateNumericComponent(const PropertyDescriptor &property, double number)
constexpr bool hasFlag(PropertyFlag value, PropertyFlag flag)
True when flag.
Result< void > accepted()
Build a successful write/validation result.
PropertyKind
Semantic property kind used by renderer-independent presenters.
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
One stable property description consumed by any UI shell.
eve::OptionalRef< const PropertyDescriptor > find(const std::string &path) const
Find a property by stable path, or an empty immediate borrow.
std::vector< PropertyDescriptor > properties