Skip to content

Commit

Permalink
dsl: Add OpenDataType.extend() helper method to add undeclared proper…
Browse files Browse the repository at this point in the history
…ties.
  • Loading branch information
aszs committed Sep 29, 2024
1 parent 1e7c064 commit e1a7a0a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/examples/dsl_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run(self, task: TaskView) -> bool:
extra = self.a_requirement.extra # type: ignore
self.a_requirement.copy_of_extra = extra # type: ignore

self.data_list.append(MyDataType())
self.data_list.append(MyDataType().extend(additional=1))
self.data_list[0].ports.source = port80
self.data_list[0].ports.target = tosca.datatypes.NetworkPortDef(8080)
return True
Expand Down
4 changes: 3 additions & 1 deletion tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ def test_computed_properties():
"source": 80,
"source_range": None,
},

"a_list": [1],
"data_list": [
{
Expand All @@ -310,7 +311,8 @@ def test_computed_properties():
"target_range": None,
"source": 80,
"source_range": None,
}
},
"additional": 1
}
],
"extra": "extra",
Expand Down
5 changes: 4 additions & 1 deletion tests/test_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ def test_envvar_type():
import tosca
from tosca import Property, DEFAULT
import unfurl
from unfurl.configurators.templates.docker import unfurl_datatypes_DockerContainer

with tosca.set_evaluation_mode("spec"):

Expand Down Expand Up @@ -820,8 +821,10 @@ class pcls(tosca.InstanceProxy):
generic_envvars = unfurl.datatypes.EnvironmentVariables(DBASE="aaaa", URL=True)

assert generic_envvars.to_yaml() == {"DBASE": "aaaa", "URL": True}
assert OpenDataType(a=1, b="b").to_yaml() == {"a": 1, "b": "b"}
assert OpenDataType(a=1, b="b").extend(c="c").to_yaml() == {"a": 1, "b": "b", "c": "c"}
assert Namespace.MyDataType(name="foo").to_yaml() == {"name": "foo"}
# make sure DockerContainer is an OpenDataType
unfurl_datatypes_DockerContainer().extend(labels=dict(foo="bar"))


test_relationship_yaml = """
Expand Down
6 changes: 6 additions & 0 deletions tosca-package/tosca/_tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,7 @@ def _make_dataclass(cls):
# we need _Tosca_Fields not dataclasses.Fields
# so for any declarations of tosca fields (properties, requirements, etc)
# missing a _Tosca_Fields, set one before calling _process_class()
global_state.spec = "spec"
global_state._in_process_class = True
try:
annotations = cls.__dict__.get("__annotations__")
Expand Down Expand Up @@ -3183,6 +3184,11 @@ def __init__(self, _name="", **kw):
self.__dict__[k] = kw.pop(k)
super().__init__(_name, **kw)

def extend(self, **kw) -> Self:
"Add undeclared properties to the data type."
self.__dict__.update(kw)
return self


class CapabilityType(_OwnedToscaType):
_type_section: ClassVar[str] = "capability_types"
Expand Down
4 changes: 2 additions & 2 deletions unfurl/configurators/templates/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
ToscaOutputs,
operation,
valid_values,
OpenDataType
)
import tosca
import unfurl.configurators.ansible
from unfurl.tosca_plugins.artifacts import *


class unfurl_datatypes_DockerContainer(DataType):
class unfurl_datatypes_DockerContainer(OpenDataType):
_type_name = "unfurl.datatypes.DockerContainer"
_type_metadata = {"additionalProperties": True}
environment: Union["unfurl.datatypes.EnvironmentVariables", None] = Property(
factory=lambda: (unfurl.datatypes.EnvironmentVariables())
)
Expand Down

0 comments on commit e1a7a0a

Please sign in to comment.