Skip to content

Commit

Permalink
Use deepcopy to copy data during expansion
Browse files Browse the repository at this point in the history
Needed so that exporters can change data on individula nodes, if needed

Signed-off-by: Erik Jaegervall <[email protected]>
  • Loading branch information
erikbosch committed Nov 14, 2023
1 parent 56f1db5 commit 80e54c0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
17 changes: 17 additions & 0 deletions tests/instances/resources/instance_extended_attribute.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Vehicle:
type: branch
instances: Test[1,3]
description: High-level vehicle data.

Vehicle.SomeThing:
type: sensor
description: "test"
datatype: string
dbc:
signal: bababa
transform:
mapping:
- from: 0
to: false
- from: 1
to: true
30 changes: 30 additions & 0 deletions tests/instances/test_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# provisions of the license provided by the LICENSE file in this repository.
#

from typing import Dict
from vspec.model.vsstree import VSSNode
from vspec.model.constants import VSSType, VSSTreeType
import vspec
Expand Down Expand Up @@ -170,3 +171,32 @@ def test_exclusion_from_instance(request):

assert "Vehicle.ExcludeSomeThing" in name_list
assert "Vehicle.ExcludeNode" in name_list


def test_extended_attribute(request):
test_path = os.path.dirname(request.fspath)
# load the file
tree = vspec.load_tree(os.path.join(test_path, "resources/instance_extended_attribute.vspec"), [os.path.join(
test_path, "resources/")], VSSTreeType.SIGNAL_TREE)

# check if root node has 3 children
assert len(tree.children) == 3

# Programmatically change value of dbc/signal for instance in Test2

for child in tree.children:
if child.qualified_name() == "Vehicle.Test2":
assert len(child.children) == 1
assert child.children[0].name == "SomeThing" # <... SomeThing
assert len(child.children[0].extended_attributes.items()) == 1
assert isinstance(child.children[0].extended_attributes["dbc"], Dict)
assert isinstance(child.children[0].extended_attributes["dbc"]["signal"], str)
assert child.children[0].extended_attributes["dbc"]["signal"] == "bababa"
child.children[0].extended_attributes["dbc"]["signal"] = "lalala"

for child in tree.children:
if child.qualified_name() == "Vehicle.Test2":
assert child.children[0].extended_attributes["dbc"]["signal"] == "lalala"
else:
# Make sure that all other instances keeps same name
assert child.children[0].extended_attributes["dbc"]["signal"] == "bababa"
4 changes: 2 additions & 2 deletions tests/vspec/test_overlay_on_instance/test.vspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A:
description: Branch A.


# Instances defined a slist
# Instances defined as list

A.B:
type: branch
Expand Down Expand Up @@ -32,4 +32,4 @@ A.S.T:
type: sensor
unit: km
description: Signal A.S.T
comment: Orig comment.
comment: Orig comment.
4 changes: 3 additions & 1 deletion vspec/model/vsstree.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class VSSNode(Node):
deprecation = ""

def __deepcopy__(self, memo):
return VSSNode(self.name, self.source_dict.copy(), self.available_types.copy(),
# Deep copy of source_dict and children needed as overlay or programmatic changes
# in exporters otherwise risk changing values not only for current instances but also for others
return VSSNode(self.name, copy.deepcopy(self.source_dict), self.available_types.copy(),
parent=None, children=copy.deepcopy(self.children, memo))

def __init__(self, name, source_dict: dict, available_types: Set[str], parent=None,
Expand Down

0 comments on commit 80e54c0

Please sign in to comment.