Skip to content

Commit

Permalink
split assign_default_values into two funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Jul 23, 2022
1 parent a58b732 commit 6f8b52c
Showing 1 changed file with 58 additions and 44 deletions.
102 changes: 58 additions & 44 deletions st2common/st2common/util/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ def _assign_subschema_default_values(self, subschema, instance):
subschema_is_dict = isinstance(subschema, dict)
iterator = subschema.items() if subschema_is_dict else enumerate(subschema)

# _get_*_schema ensures that schema_item is always a dict
# _get_flattened_*_schema ensures that schema_item is always a dict
for schema_item_key, schema_item in iterator:
has_default_value = "default" in schema_item

if isinstance(instance, dict):
has_instance_value = schema_item_key in instance
else:
Expand All @@ -318,54 +320,66 @@ def _assign_subschema_default_values(self, subschema, instance):
except (KeyError, IndexError):
instance_value = None

has_default_value = "default" in schema_item
default_value = schema_item.get("default", None)
if has_default_value and not has_instance_value:
# instance value is not provided, but default value is, use a default value
instance[schema_item_key] = default_value

schema_item_type = schema_item.get("type", None)
is_mutatable = schema_item_type in ["array", "object"]

if schema_item_type == "object":
has_properties = schema_item.get("properties", None)
has_pattern_properties = schema_item.get("patternProperties", None)
has_additional_properties = schema_item.get(
"additionalProperties", None
if has_default_value or has_instance_value or is_mutatable:
instance[schema_item_key] = self._assign_default_values(
schema=schema_item,
instance=instance_value,
can_replace_instance=not has_instance_value,
)

# Inspect nested object properties
if (
has_properties
or has_pattern_properties
or has_additional_properties
):
if not instance_value:
instance_value = instance[schema_item_key] = {}

properties_schema = self._get_flattened_object_properties_schema(
schema_item,
object_keys=instance_value.keys(),
)
def _assign_default_values(self, schema, instance, can_replace_instance=False):
has_default_value = "default" in schema
default_value = schema.get("default", None)
if has_default_value and can_replace_instance and instance is None:
# instance value is not provided, but default value is, use a default value
instance = default_value

self._assign_subschema_default_values(
schema=properties_schema, instance=instance_value
)
elif schema_item_type == "array":
has_items = schema_item.get("items", None)
has_additional_items = schema_item.get("additionalItems", None)

# Inspect nested array items
if has_items or has_additional_items:
if not instance_value:
instance_value = instance[schema_item_key] = []

items_schema = self._get_flattened_array_items_schema(
schema_item,
items_count=len(instance_value),
)
self._assign_subschema_default_values(
schema=items_schema, instance=instance_value
)
schema_type = schema.get("type", None)

if schema_type == "object":
has_properties = schema.get("properties", None)
has_pattern_properties = schema.get("patternProperties", None)
has_additional_properties = schema.get(
"additionalProperties", None
)

# Inspect nested object properties
if (
has_properties
or has_pattern_properties
or has_additional_properties
):
if not instance:
instance = {}

properties_schema = self._get_flattened_object_properties_schema(
schema,
object_keys=instance.keys(),
)

self._assign_subschema_default_values(
schema=properties_schema, instance=instance
)

elif schema_type == "array":
has_items = schema.get("items", None)
has_additional_items = schema.get("additionalItems", None)

# Inspect nested array items
if has_items or has_additional_items:
if not instance:
instance = []

items_schema = self._get_flattened_array_items_schema(
schema,
items_count=len(instance),
)
self._assign_subschema_default_values(
schema=items_schema, instance=instance
)

return instance

Expand Down

0 comments on commit 6f8b52c

Please sign in to comment.