Skip to content

Commit

Permalink
Add recursive hasprop/getprop helpers
Browse files Browse the repository at this point in the history
It is now possible to get a nested datavalue using the convenient
function call datavalue("path.to.nested.value", default="some-default")

This patch also provides general recursive getprop/setprop functions.
The specialized datavalue helper is preferred because it can give the
user a more useful error.
  • Loading branch information
Filippo Cremonese committed May 4, 2021
1 parent 33f4fb3 commit ba13605
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions .orchestra/config/lib/util.lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,49 @@
#@ load("@ytt:assert", "assert")

#@ fail = "dummy-string-because-starlark-does-not-have-is-operator"
#@ undefined = "another-dummy-string-because-starlark-does-not-have-is-operator"

#@ def recursive_hasattr(obj, attrname):
#@ leftmost_attrname, sep, rest = attrname.partition(".")
#@ if not hasattr(obj, leftmost_attrname):
#@ return False
#@ end
#@ if not sep and not rest:
#@ return True
#@ end
#@ attr_value = getattr(obj, leftmost_attrname)
#@ return recursive_hasattr(attr_value, rest)
#@ end

#@ def _recursive_getattr(obj, attrname, default, complete_attrname):
#@ leftmost_attrname, sep, rest = attrname.partition(".")
#@ if not hasattr(obj, leftmost_attrname):
#@ if default == fail:
#@ assert.fail("Could not get property " + complete_attrname)
#@ else:
#@ return default
#@ end
#@ end
#@ attr_value = getattr(obj, leftmost_attrname)
#@ if not sep and not rest:
#@ return attr_value
#@ end
#@ return _recursive_getattr(attr_value, rest, default, complete_attrname)
#@ end

#@ def recursive_getattr(obj, attrname, default=fail):
#@ return _recursive_getattr(obj, attrname, default, attrname)
#@ end

#@ def datavalue(attrname, default=fail):
#@ if not hasattr(data.values, attrname):
#@ value = recursive_getattr(data.values, attrname, default=undefined)
#@ if value == undefined:
#@ if default == fail:
#@ assert.fail("You must provide a data value for " + attrname)
#@ else:
#@ return default
#@ end
#@ else:
#@ return getattr(data.values, attrname)
#@ return value
#@ end
#@ end

0 comments on commit ba13605

Please sign in to comment.