Skip to content

Commit f954713

Browse files
b-ryanmdelaurentis
authored andcommitted
Allow specification of default values to the Schema.from_dict function (#42)
* Allow specification of default values to the Schema.from_dict function When using the from_dict method, you might be in a situation where you want every Schema created to have a certain property, like "inclusion": "automatic". This allows you to do that like: ```python Schema.from_dict(dict_, inclusion="automatic") ``` * Add test that confirms overriding from_dict options happens
1 parent a3ad3c7 commit f954713

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

setup.py

100644100755
File mode changed.

singer/schema.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,22 @@ def to_dict(self):
7878
return result
7979

8080
@classmethod
81-
def from_dict(cls, data):
82-
'''Initialize a Schema object based on the JSON Schema structure.'''
83-
kwargs = {}
81+
def from_dict(cls, data, **schema_defaults):
82+
'''Initialize a Schema object based on the JSON Schema structure.
83+
84+
:param schema_defaults: The default values to the Schema
85+
constructor.'''
86+
kwargs = schema_defaults.copy()
8487
properties = data.get('properties')
8588
items = data.get('items')
8689

8790
if properties:
8891
kwargs['properties'] = {
89-
k: Schema.from_dict(v) for k, v in properties.items()
92+
k: Schema.from_dict(v, **schema_defaults)
93+
for k, v in properties.items()
9094
}
9195
if items:
92-
kwargs['items'] = Schema.from_dict(items)
96+
kwargs['items'] = Schema.from_dict(items, **schema_defaults)
9397
for key in STANDARD_KEYS:
9498
if key in data:
9599
kwargs[key] = data[key]

tests/test_schema.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class TestSchema(unittest.TestCase):
2525
'properties': {
2626
'a_string': string_dict,
2727
'an_array': array_dict
28-
}
28+
},
29+
'inclusion': 'whatever',
2930
}
3031

3132
# Schema object forms of the same schemas as above
@@ -37,7 +38,8 @@ class TestSchema(unittest.TestCase):
3738

3839
object_obj = Schema(type='object',
3940
properties={'a_string': string_obj,
40-
'an_array': array_obj})
41+
'an_array': array_obj},
42+
inclusion='whatever')
4143

4244
def test_string_to_dict(self):
4345
self.assertEquals(self.string_dict, self.string_obj.to_dict())
@@ -49,23 +51,29 @@ def test_array_to_dict(self):
4951
self.assertEquals(self.array_dict, self.array_obj.to_dict())
5052

5153
def test_object_to_dict(self):
52-
self.assertEquals(self.object_dict, self.object_obj.to_dict())
54+
self.assertEquals(self.object_dict, self.object_obj.to_dict())
5355

5456
def test_string_from_dict(self):
5557
self.assertEquals(self.string_obj, Schema.from_dict(self.string_dict))
5658

5759
def test_integer_from_dict(self):
5860
self.assertEquals(self.integer_obj, Schema.from_dict(self.integer_dict))
5961

60-
def test_array_from_dict(self):
62+
def test_array_from_dict(self):
6163
self.assertEquals(self.array_obj, Schema.from_dict(self.array_dict))
6264

63-
def test_object_from_dict(self):
64-
self.assertEquals(self.object_obj, Schema.from_dict(self.object_dict))
65-
65+
def test_object_from_dict(self):
66+
self.assertEquals(self.object_obj, Schema.from_dict(self.object_dict))
67+
6668
def test_repr_atomic(self):
6769
self.assertEquals(self.string_obj, eval(repr(self.string_obj)))
6870

6971
def test_repr_recursive(self):
7072
self.assertEquals(self.object_obj, eval(repr(self.object_obj)))
7173

74+
def test_object_from_dict_with_defaults(self):
75+
schema = Schema.from_dict(self.object_dict, inclusion='automatic')
76+
self.assertEquals('whatever', schema.inclusion,
77+
msg='The schema value should override the default')
78+
self.assertEquals('automatic', schema.properties['a_string'].inclusion)
79+
self.assertEquals('automatic', schema.properties['an_array'].items.inclusion)

0 commit comments

Comments
 (0)