mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-11 02:02:22 +00:00
selector: fail safe instead of silently corrupting on ambiguous dotted keys
An unquoted query segment like "Foo.Bar.Baz" is ambiguous whenever a pset is literally named "Foo.Bar" (see #6937, ifccsv spreadsheet export/import). get_element_value already returned None for the simple case, but if a shorter, coincidentally-named pset/property also existed (e.g. pset "Foo" with property "Bar"), it silently returned that unrelated value instead. set_element_value had the same problem in reverse: it silently wrote into the wrong property/pset rather than reporting the extra unresolved key. Both now fail safely (None / a raised SetElementValueException) so bad data is never produced. The documented quoted/regex query forms already round-trip correctly and are unaffected. ifccsv now catches that exception per-column so one unresolved query doesn't abort the whole import. Generated with the assistance of an AI coding tool.
This commit is contained in:
@@ -557,6 +557,9 @@ def _get_element_value(element: ifcopenshell.entity_instance, keys: list[str]) -
|
||||
else:
|
||||
results.append(subvalue)
|
||||
value = results
|
||||
else:
|
||||
# No more keys can be applied to a terminal value.
|
||||
value = None
|
||||
return value
|
||||
|
||||
|
||||
@@ -803,6 +806,12 @@ def set_element_value(
|
||||
|
||||
element = result
|
||||
elif isinstance(element, dict): # Such as from the result of a prior get_pset
|
||||
if len(keys) != i + 1:
|
||||
raise SetElementValueException(
|
||||
f"Failed to set value '{value}' for element '{original_element}' with query '{query}': "
|
||||
f"'{key}' is not the last key. If a name contains a literal '.', quote it, "
|
||||
'e.g. "Pset.Name".Property or /Pset\\.Name/.Property.'
|
||||
)
|
||||
pset = ifc_file.by_id(element["id"])
|
||||
if isinstance(key, re.Pattern):
|
||||
for prop, prop_value in element.items():
|
||||
|
||||
Reference in New Issue
Block a user