Skip to content

Commit

Permalink
changed default behaviour of .json when it received an empty value (n…
Browse files Browse the repository at this point in the history
…ow returns the default class)
  • Loading branch information
DinisCruz committed Jan 3, 2025
1 parent 4cb33a0 commit fca465f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions osbot_utils/helpers/sqlite/Sqlite__Table__Create.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init__(self, table_name):
self.set_default_fields()

def add_field(self, field_data: dict):
sqlite_field = Sqlite__Field.from_json(field_data)
if sqlite_field:
if field_data and isinstance(field_data, dict):
sqlite_field = Sqlite__Field.from_json(field_data)
self.fields.append(sqlite_field)
return True
return False
Expand Down
2 changes: 1 addition & 1 deletion osbot_utils/type_safe/Type_Safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def from_json(cls, json_data, raise_on_not_found=False):
json_data = json_parse(json_data)
if json_data: # if there is no data or is {} then don't create an object (since this could be caused by bad data being provided)
return cls().deserialize_from_dict(json_data,raise_on_not_found=raise_on_not_found)
return None
return cls()

# todo: see if it is possible to add recursive protection to this logic
def serialize_to_dict(obj):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/helpers/sqlite/test_Sqlite__Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def assert_sqlite_field_type(field_name, field_type, expected_type_local):
sqlite_field = Sqlite__Field.from_json(field_data)
assert sqlite_field.__locals__() == expected_data

assert Sqlite__Field.from_json({}) is None
assert Sqlite__Field.from_json({}).obj() == Sqlite__Field().obj()

assert_sqlite_field_type('id' , int , Sqlite__Field__Type.INTEGER)
assert_sqlite_field_type('aaaa' , float, Sqlite__Field__Type.REAL )
Expand Down

0 comments on commit fca465f

Please sign in to comment.