From c1c3c2e63ffb8bdec17dd1e79166d5ae25661ef7 Mon Sep 17 00:00:00 2001 From: Tristan Van Berkom Date: Mon, 19 Dec 2016 00:01:39 +0900 Subject: _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). --- buildstream/_yaml.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'buildstream/_yaml.py') 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 -- cgit v1.2.1