mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-15 01:24:45 +02:00
Fixed meta field handling in JsonUtils::inherit function, removed
workarounds
This commit is contained in:
@ -1014,7 +1014,7 @@ const JsonNode & JsonUtils::getSchema(std::string URI)
|
|||||||
return getSchemaByName(filename).resolvePointer(URI.substr(posHash + 1));
|
return getSchemaByName(filename).resolvePointer(URI.substr(posHash + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool noOverride)
|
void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool ignoreOverride, bool copyMeta)
|
||||||
{
|
{
|
||||||
if (dest.getType() == JsonNode::JsonType::DATA_NULL)
|
if (dest.getType() == JsonNode::JsonType::DATA_NULL)
|
||||||
{
|
{
|
||||||
@ -1022,6 +1022,14 @@ void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool noOverride)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool hasNull = dest.isNull() || source.isNull();
|
||||||
|
bool sameType = dest.getType() == source.getType();
|
||||||
|
bool sourceNumeric = source.getType() == JsonNode::JsonType::DATA_FLOAT || source.getType() == JsonNode::JsonType::DATA_INTEGER;
|
||||||
|
bool destNumeric = dest.getType() == JsonNode::JsonType::DATA_FLOAT || dest.getType() == JsonNode::JsonType::DATA_INTEGER;
|
||||||
|
bool bothNumeric = sourceNumeric && destNumeric;
|
||||||
|
|
||||||
|
assert( hasNull || sameType || bothNumeric );
|
||||||
|
|
||||||
switch (source.getType())
|
switch (source.getType())
|
||||||
{
|
{
|
||||||
case JsonNode::JsonType::DATA_NULL:
|
case JsonNode::JsonType::DATA_NULL:
|
||||||
@ -1040,30 +1048,33 @@ void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool noOverride)
|
|||||||
}
|
}
|
||||||
case JsonNode::JsonType::DATA_STRUCT:
|
case JsonNode::JsonType::DATA_STRUCT:
|
||||||
{
|
{
|
||||||
if(!noOverride && vstd::contains(source.flags, "override"))
|
if(!ignoreOverride && vstd::contains(source.flags, "override"))
|
||||||
{
|
{
|
||||||
std::swap(dest, source);
|
std::swap(dest, source);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (copyMeta)
|
||||||
|
dest.meta = source.meta;
|
||||||
|
|
||||||
//recursively merge all entries from struct
|
//recursively merge all entries from struct
|
||||||
for(auto & node : source.Struct())
|
for(auto & node : source.Struct())
|
||||||
merge(dest[node.first], node.second, noOverride);
|
merge(dest[node.first], node.second, ignoreOverride);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source, bool noOverride)
|
void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source, bool ignoreOverride, bool copyMeta)
|
||||||
{
|
{
|
||||||
// uses copy created in stack to safely merge two nodes
|
// uses copy created in stack to safely merge two nodes
|
||||||
merge(dest, source, noOverride);
|
merge(dest, source, ignoreOverride, copyMeta);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonUtils::inherit(JsonNode & descendant, const JsonNode & base)
|
void JsonUtils::inherit(JsonNode & descendant, const JsonNode & base)
|
||||||
{
|
{
|
||||||
JsonNode inheritedNode(base);
|
JsonNode inheritedNode(base);
|
||||||
merge(inheritedNode, descendant, true);
|
merge(inheritedNode, descendant, true, true);
|
||||||
descendant.swap(inheritedNode);
|
descendant.swap(inheritedNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ namespace JsonUtils
|
|||||||
* null : if value in source is present but set to null it will delete entry in dest
|
* null : if value in source is present but set to null it will delete entry in dest
|
||||||
* @note this function will destroy data in source
|
* @note this function will destroy data in source
|
||||||
*/
|
*/
|
||||||
DLL_LINKAGE void merge(JsonNode & dest, JsonNode & source, bool noOverride = false);
|
DLL_LINKAGE void merge(JsonNode & dest, JsonNode & source, bool ignoreOverride = false, bool copyMeta = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief recursively merges source into dest, replacing identical fields
|
* @brief recursively merges source into dest, replacing identical fields
|
||||||
@ -194,7 +194,7 @@ namespace JsonUtils
|
|||||||
* null : if value in source is present but set to null it will delete entry in dest
|
* null : if value in source is present but set to null it will delete entry in dest
|
||||||
* @note this function will preserve data stored in source by creating copy
|
* @note this function will preserve data stored in source by creating copy
|
||||||
*/
|
*/
|
||||||
DLL_LINKAGE void mergeCopy(JsonNode & dest, JsonNode source, bool noOverride = false);
|
DLL_LINKAGE void mergeCopy(JsonNode & dest, JsonNode source, bool ignoreOverride = false, bool copyMeta = false);
|
||||||
|
|
||||||
/** @brief recursively merges descendant into copy of base node
|
/** @brief recursively merges descendant into copy of base node
|
||||||
* Result emulates inheritance semantic
|
* Result emulates inheritance semantic
|
||||||
|
@ -360,17 +360,9 @@ void CObjectClassesHandler::beforeValidate(JsonNode & object)
|
|||||||
{
|
{
|
||||||
for (auto & entry : object["types"].Struct())
|
for (auto & entry : object["types"].Struct())
|
||||||
{
|
{
|
||||||
JsonNode base = object["base"];
|
JsonUtils::inherit(entry.second, object["base"]);
|
||||||
base.setMeta(entry.second.meta);
|
|
||||||
|
|
||||||
JsonUtils::inherit(entry.second, base);
|
|
||||||
for (auto & templ : entry.second["templates"].Struct())
|
for (auto & templ : entry.second["templates"].Struct())
|
||||||
{
|
JsonUtils::inherit(templ.second, entry.second["base"]);
|
||||||
JsonNode subBase = entry.second["base"];
|
|
||||||
subBase.setMeta(entry.second.meta);
|
|
||||||
|
|
||||||
JsonUtils::inherit(templ.second, subBase);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user