Fixed conversion of enum property values
This commit is contained in:
@@ -67,6 +67,16 @@ String Setting::getDisplayValue() const {
|
||||
} //Setting::getDisplayValue
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Append a value to the setting
|
||||
|
||||
value: The value to append
|
||||
--------------------------------------------------------------------*/
|
||||
void Setting::append(Value&& value) {
|
||||
m_values.emplace_back(value);
|
||||
} //Setting::append
|
||||
|
||||
|
||||
#ifdef ARCHICAD
|
||||
/*--------------------------------------------------------------------
|
||||
Receive a value from an Archicad property
|
||||
|
||||
@@ -76,6 +76,11 @@ namespace speckle::record::property {
|
||||
|
||||
// MARK: - Functions (const)
|
||||
|
||||
/*!
|
||||
Determine if the setting is empty, i.e. has no values
|
||||
@return True if the setting is empty
|
||||
*/
|
||||
bool empty() const { return m_values.empty(); }
|
||||
/*!
|
||||
Determine if the setting has a defined value
|
||||
@return True if a defined value is found
|
||||
@@ -94,6 +99,12 @@ namespace speckle::record::property {
|
||||
|
||||
// MARK: - Functions (mutating)
|
||||
|
||||
/*!
|
||||
Append a value to the setting
|
||||
@param value The value to append
|
||||
*/
|
||||
void append(Value&& value);
|
||||
|
||||
#ifdef ARCHICAD
|
||||
/*!
|
||||
Receive a value from an Archicad property
|
||||
|
||||
@@ -183,10 +183,22 @@ std::unique_ptr<Group> Template::getGroup() const {
|
||||
bool Template::convert(const API_PropertyValue& value, Setting& setting) const {
|
||||
auto measure = Template::convert(m_measure);
|
||||
switch (m_type) {
|
||||
case Type::single: case Type::singleEnum:
|
||||
case Type::single:
|
||||
return setting.receive(value.singleVariant, API_VariantStatusNormal, measure);
|
||||
case Type::array: case Type::arrayEnum:
|
||||
case Type::singleEnum:
|
||||
if (auto enumValue = findEnumValue(Value{value.singleVariant.variant, API_VariantStatusNormal, measure}); enumValue) {
|
||||
setting.append(std::move(*enumValue));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case Type::array:
|
||||
return setting.receive(value.listVariant, API_VariantStatusNormal, measure);
|
||||
case Type::arrayEnum:
|
||||
for (const auto& val : value.listVariant.variants) {
|
||||
if (auto enumValue = findEnumValue(Value{val, API_VariantStatusNormal, measure}); enumValue)
|
||||
setting.append(std::move(*enumValue));
|
||||
}
|
||||
return !setting.empty();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -207,6 +219,26 @@ bool Template::convert(const Setting& setting, API_PropertyValue& value) const {
|
||||
} //Template::convert
|
||||
#endif
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Find an enumerated value by key
|
||||
|
||||
key: The value key
|
||||
|
||||
return: The enum value paired with the specified key (nullopt on failure)
|
||||
--------------------------------------------------------------------*/
|
||||
Value::Option Template::findEnumValue(const Value& key) const {
|
||||
if (key.getValue() != nullptr) {
|
||||
if (auto iter = std::find_if(m_enumValues.begin(), m_enumValues.end(), [&key](const auto& value){
|
||||
if (value.getKey() == nullptr)
|
||||
return false;
|
||||
return *key.getValue() == *value.getKey();
|
||||
}); iter != m_enumValues.end())
|
||||
return *iter;
|
||||
}
|
||||
return std::nullopt;
|
||||
} //Template::findEnumValue
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Fill an inventory with the package items
|
||||
|
||||
@@ -156,6 +156,12 @@ namespace speckle::record::property {
|
||||
@return True if the conversion was successful
|
||||
*/
|
||||
bool convert(const Setting& setting, API_PropertyValue& value) const;
|
||||
/*!
|
||||
Find an enumerated value by key
|
||||
@param key The value key
|
||||
@return The enum value paired with the specified key (nullopt on failure)
|
||||
*/
|
||||
Value::Option findEnumValue(const Value& key) const;
|
||||
|
||||
// MARK: - Functions (mutating)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user