From 824d7f03cab8ed66a98774d3ce3aff8a8f1cc758 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Wed, 2 Nov 2022 14:43:24 +0100 Subject: [PATCH] Inherit TerraformJSONBase from Mapping ABC (#62) Inhering from collections.abc.Mapping gives us __contains__, keys, items, values, get, __eq__, and __ne__ for free. It also makes objects behave more dict-like --- tftest.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tftest.py b/tftest.py index ae9b7fb..02cab59 100644 --- a/tftest.py +++ b/tftest.py @@ -154,7 +154,7 @@ def parse_args(init_vars=None, tf_vars=None, targets=None, **kw): return cmd_args -class TerraformJSONBase(object): +class TerraformJSONBase(collections.abc.Mapping): "Base class for JSON wrappers." def __init__(self, raw): @@ -163,6 +163,12 @@ def __init__(self, raw): def __bytes__(self): return bytes(self._raw) + def __getitem__(self, index): + return self._raw[index] + + def __iter__(self): + return iter(self._raw) + def __len__(self): return len(self._raw) @@ -187,12 +193,6 @@ def __getattr__(self, name): def __getitem__(self, name): return self._raw[name].get('value') - def __contains__(self, name): - return name in self._raw - - def __iter__(self): - return iter(self._raw) - class TerraformPlanModule(TerraformJSONBase): "Minimal wrapper for parsed plan output modules." @@ -218,12 +218,6 @@ def resources(self): for res in self._raw.get('resources', [])) return self._resources - def __getitem__(self, name): - return self._raw[name] - - def __contains__(self, name): - return name in self._raw - class TerraformPlanOutput(TerraformJSONBase): "Minimal wrapper for Terraform plan JSON output."