Skip to content

Commit

Permalink
Add extrinsic to merge array of objects into one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Topher-the-Geek committed Jul 15, 2016
1 parent 78d6cb5 commit 22e113a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cfpp/extrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ def kms__encrypt_file(config, context, args):
return base64.b64encode(ciphertext["CiphertextBlob"])


@extrinsic
def merge(config, context, args):
_raise_unless_array_of_dictionaries(context, args)
result = {}
for arg in args:
result.update(arg)
return result


class ContextException(Exception):
def __init__(self, context, message):
message = "%s: %s" % (".".join(context), message)
Expand Down Expand Up @@ -243,6 +252,14 @@ def _raise_unless_array_of_strings(context, arg):
raise UnexpectedArgumentTypeException(context, unicode, element)


def _raise_unless_array_of_dictionaries(context, arg):
if not isinstance(arg, list):
raise UnexpectedArgumentTypeException(context, list, arg)
for element in arg:
if not isinstance(element, dict):
raise UnexpectedArgumentTypeException(context, dict, element)


def _raise_unless_include_args(context, arg):
if not isinstance(arg, list):
raise UnexpectedArgumentTypeException(context, list, arg)
Expand Down
15 changes: 15 additions & 0 deletions tests/0005_merge.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "",
"Resources": {
"FirstResource": {
"Tag": "FirstTag"
},
"SecondResource": {
"Tag": "SecondTag"
},
"OverriddenResource": {
"Tag": "OverriddenTag"
}
}
}
19 changes: 19 additions & 0 deletions tests/0005_merge.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "",
"Resources": {
"CFPP::Merge": [
{
"CFPP::JsonFile": "0005_resource_snippet.json"
},
{
"SecondResource": {
"Tag": "SecondTag"
},
"OverriddenResource": {
"Tag": "OverriddenTag"
}
}
]
}
}
8 changes: 8 additions & 0 deletions tests/0005_resource_snippet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"FirstResource": {
"Tag": "FirstTag"
},
"OverriddenResource": {
"Tag": "OriginalTag"
}
}

0 comments on commit 22e113a

Please sign in to comment.