diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2016-12-19 00:01:39 +0900 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2016-12-19 00:03:23 +0900 |
commit | c1c3c2e63ffb8bdec17dd1e79166d5ae25661ef7 (patch) | |
tree | 98ebfdfd0b2595fe6c7ce75ccfe88b2c4eba81d9 /buildstream/_yaml.py | |
parent | b7698a26622f57d81666f5956b9b0b977e4f818d (diff) | |
download | buildstream-c1c3c2e63ffb8bdec17dd1e79166d5ae25661ef7.tar.gz |
_yaml.py: Attempt simple type conversions in _yaml.node_get()
It's typical that we want to specify a number in the yaml but
want to fetch a string value from it, allow this conversion for
simple numbers and strings, but dont allow it for dictionaries
and lists (which python happily converts without raising ValueError).
Diffstat (limited to 'buildstream/_yaml.py')
-rw-r--r-- | buildstream/_yaml.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index 97f57f6b6..2f9a00119 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -278,9 +278,21 @@ def node_get(node, expected_type, key, indices=[], default_value=None): path += '[%d]' % index if not isinstance(value, expected_type): - raise LoadError(LoadErrorReason.INVALID_DATA, - "%s: Value of '%s' is not of the expected type '%s'" % - (str(provenance), path, expected_type.__name__)) + # Attempt basic conversions if possible, typically we want to + # be able to specify numeric values and convert them to strings, + # but we dont want to try converting dicts/lists + try: + if not (expected_type == list or + expected_type == dict or + isinstance(value, list) or + isinstance(value, dict)): + value = expected_type(value) + else: + raise ValueError() + except ValueError: + raise LoadError(LoadErrorReason.INVALID_DATA, + "%s: Value of '%s' is not of the expected type '%s'" % + (str(provenance), path, expected_type.__name__)) return value |