Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default values during initlization. #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@
"additionalProperties": False,
}

default_values = {
"properties": {
"name": {"type": "string", "default": "Peter"},
"lastname": {"type": "string"},
"height": {
"type": "object",
"properties": {
"value": {"type": "number"},
"unit": {"type": "string", "default": "m"},
},
},
}
}


class TestCore(unittest.TestCase):
def test_create_invalid_object(self):
Expand Down Expand Up @@ -285,3 +299,26 @@ def test_recursive_models(self):

self.assertEqual(mom.children[0].age, 15)
self.assertEqual(mom.children[1].age, 3)

def test_default_values(self):
Person = warlock.model_factory(default_values)

default = Person()
self.assertEqual(default.name, "Peter")
self.assertEqual(default.height, {"unit": "m"})

with_name = Person(name="Mary")
self.assertEqual(with_name.name, "Mary")
self.assertEqual(with_name.height, {"unit": "m"})

with_empty_height = Person(height={})
self.assertEqual(with_empty_height.name, "Peter")
self.assertEqual(with_empty_height.height, {"unit": "m"})

with_height_value = Person(height={"value": 1.2})
self.assertEqual(with_height_value.name, "Peter")
self.assertEqual(with_height_value.height, {"value": 1.2, "unit": "m"})

with_height = Person(height={"value": 120.0, "unit": "cm"})
self.assertEqual(with_height.name, "Peter")
self.assertEqual(with_height.height, {"value": 120.0, "unit": "cm"})
40 changes: 40 additions & 0 deletions warlock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,43 @@ def model_factory(schema, base_class=model.Model, name=None, resolver=None):
resolver = resolver

class Model(base_class):
def _setnested(self, path: list[str], value, root=None):
if root is None:
root = self
if len(path) > 1:
head = path[0]
if not head in root:
root[head] = {}
self._setnested(path[1:], value, root=root[head])
elif len(path) == 1:
root[path[0]] = value

def _pathexists(self, path: list[str], root=None):
if root is None:
root = self
if len(path) == 1:
return path[0] in root
elif len(path) == 0:
return False
else:
head = path[0]
if head in root:
return self._pathexists(path[1:], root=root[head])
else:
return False

def _setdefaults(self, path: list[str], schema_props: dict):
for name, prop in schema_props.items():
if "type" in prop:
local_path = path.copy()
local_path.append(name)
if prop["type"] == "object":
if "properties" in prop:
self._setdefaults(local_path, prop["properties"])
elif "default" in prop and not name in self:
if not self._pathexists(local_path):
self._setnested(local_path, prop["default"])

def __init__(self, *args, **kwargs):
self.__dict__["schema"] = schema
self.__dict__["resolver"] = resolver
Expand All @@ -42,6 +79,9 @@ def __init__(self, *args, **kwargs):

base_class.__init__(self, *args, **kwargs)

if "properties" in schema:
self._setdefaults([], schema["properties"])

if resolver is not None:
Model.resolver = resolver

Expand Down