Commit 482a9278 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

[CLIENT] Allow setting a parameter from a model

parent bf69e503
Pipeline #76855 failed with stages
in 9 minutes and 6 seconds
......@@ -134,6 +134,9 @@ class ParamBase:
def f(p, v):
for n, d in self._getItemDataDicts():
if p in d:
# If val is not from the expected class, pass to model layer
if not is_special_type_item(v, d[p]['klass']):
to_model = d[p]['to_model']
return to_model(v)
return v
......@@ -174,6 +177,9 @@ class ParamBase:
model[name] = f(name, val)
for n, d in self._getItemDataDicts():
if name in d:
# If val is not from the expected class, get it from model layer
if not is_special_type_item(val, d[name]['klass']):
val = d[name]['from_model'](val)
d[name]['value'] = val
......@@ -295,7 +301,9 @@ class ParamArray(list):
def __setitem__(self, key, value):
model_value = self.__to_model__(value)
# If val is not from the expected class, pass to model layer
direct = not is_special_type_item(value, self.__data__['klass'])
model_value = value if direct else self.__to_model__(value)
self.__model__.__setitem__(key, model_value)
# store a copy of the value
value = self.__from_model__(model_value)
......@@ -421,6 +429,14 @@ def get_schema_type(schema, name=None, sub_schema=None, name_prefix=''):
return type(type_name, (ParamBase,), type_dict)
def get_type_from_factory(factory):
return factory.func if type(factory) is partial else factory
def is_special_type_item(item, factory):
return isinstance(item, get_type_from_factory(factory))
def get_type_data(name, schema_json):
schema = json.loads(schema_json)
klass = get_schema_type(schema)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment