Skip to content

Commit

Permalink
Separate decoding of complex values into specific method
Browse files Browse the repository at this point in the history
  • Loading branch information
dbendall committed Jul 13, 2023
1 parent 2572a18 commit e1a9349
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,22 @@ def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, val
The prepared value.
"""
if value is not None and (self.field_is_complex(field) or value_is_complex):
return json.loads(value)
return self.decode_complex_value(field_name, field, value)
return value

def decode_complex_value(self, field_name: str, field: FieldInfo, value: Any) -> Any:
"""
Decode the value for a complex field
Args:
value: The value of the field which is to be decoded
field: The field which is being decoded
Returns:
The decoded value for further preparation
"""
return json.loads(value)

@abstractmethod
def __call__(self) -> dict[str, Any]:
pass
Expand Down Expand Up @@ -414,7 +427,7 @@ def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, val
else:
# field is complex and there's a value, decode that as JSON, then add explode_env_vars
try:
value = json.loads(value)
value = self.decode_complex_value(field_name, field, value)
except ValueError as e:
if not allow_parse_failure:
raise e
Expand Down Expand Up @@ -516,7 +529,7 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
is_complex, allow_json_failure = self._field_is_complex(target_field)
if is_complex:
try:
env_val = json.loads(env_val)
env_val = self.decode_complex_value(last_key, target_field, env_val)
except ValueError as e:
if not allow_json_failure:
raise e
Expand Down

0 comments on commit e1a9349

Please sign in to comment.