Skip to content

Commit

Permalink
Inherit TerraformJSONBase from Mapping ABC (#62)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
juliocc authored Nov 2, 2022
1 parent 4747bc2 commit 824d7f0
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions tftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand All @@ -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."
Expand All @@ -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."
Expand Down

0 comments on commit 824d7f0

Please sign in to comment.