diff options
Diffstat (limited to 'src/buildstream/_yaml.pyx')
| -rw-r--r-- | src/buildstream/_yaml.pyx | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index eb73ef4d6..71c3b7949 100644 --- a/src/buildstream/_yaml.pyx +++ b/src/buildstream/_yaml.pyx @@ -104,6 +104,16 @@ cdef class ScalarNode(Node): "{}: Value of '{}' is not of the expected type '{}'" .format(provenance, path, bool.__name__, self.value)) + cpdef int as_int(self) except *: + try: + return int(self.value) + except ValueError: + provenance = node_get_provenance(self) + path = node_find_target(provenance.toplevel, self)[-1] + raise LoadError(LoadErrorReason.INVALID_DATA, + "{}: Value of '{}' is not of the expected type '{}'" + .format(provenance, path, int.__name__)) + cpdef str as_str(self): # We keep 'None' as 'None' to simplify the API's usage and allow chaining for users if self.value is None: @@ -165,6 +175,11 @@ cdef class MappingNode(Node): cdef ScalarNode scalar = self.get_scalar(key, default) return scalar.as_bool() + cpdef int get_int(self, str key, object default=_sentinel) except *: + # TODO: don't go through 'get_scalar', we can directly get everything and optimize + cdef ScalarNode scalar = self.get_scalar(key, default) + return scalar.as_int() + cpdef str get_str(self, str key, object default=_sentinel): # TODO: don't go through 'get_scalar', we can directly get everything and optimize cdef ScalarNode scalar = self.get_scalar(key, default) |
