aboutsummaryrefslogtreecommitdiff
path: root/lib/private/dict_subject.bzl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/dict_subject.bzl')
-rw-r--r--lib/private/dict_subject.bzl22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/private/dict_subject.bzl b/lib/private/dict_subject.bzl
index 48d9463..f155a17 100644
--- a/lib/private/dict_subject.bzl
+++ b/lib/private/dict_subject.bzl
@@ -39,10 +39,13 @@ def _dict_subject_new(actual, meta, container_name = "dict", key_plural_name = "
# buildifier: disable=uninitialized
public = struct(
+ # keep sorted start
contains_exactly = lambda *a, **k: _dict_subject_contains_exactly(self, *a, **k),
contains_at_least = lambda *a, **k: _dict_subject_contains_at_least(self, *a, **k),
contains_none_of = lambda *a, **k: _dict_subject_contains_none_of(self, *a, **k),
+ get = lambda *a, **k: _dict_subject_get(self, *a, **k),
keys = lambda *a, **k: _dict_subject_keys(self, *a, **k),
+ # keep sorted end
)
self = struct(
actual = actual,
@@ -152,6 +155,25 @@ def _dict_subject_contains_none_of(self, none_of):
actual = "actual: {{\n{}\n}}".format(format_dict_as_lines(self.actual)),
)
+def _dict_subject_get(self, key, *, factory):
+ """Gets `key` from the actual dict wrapped in a subject.
+
+ Args:
+ self: implicitly added.
+ key: ([`object`]) the key to fetch.
+ factory: ([`callable`]) subject factory function, with the signature
+ of `def factory(value, *, meta)`, and returns the wrapped value.
+
+ Returns:
+ The return value of the `factory` arg.
+ """
+ if key not in self.actual:
+ fail("KeyError: '{key}' not found in {expr}".format(
+ key = key,
+ expr = self.meta.current_expr(),
+ ))
+ return factory(self.actual[key], meta = self.meta.derive("get({})".format(key)))
+
def _dict_subject_keys(self):
"""Returns a `CollectionSubject` for the dict's keys.