Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat allow dict values in generate event cmd #7938

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samcli/lib/generated_sample_events/event-mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@
"type": "string",
"default": "path/to/resource"
},
"querystringparameters": {
"type": "dict",
"default": "{\"foo\": \"bar\"}"
},
"account-id": {
"default": "123456789012"
},
Expand Down
20 changes: 19 additions & 1 deletion samcli/lib/generated_sample_events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def transform_val(self, properties, val):
if hashing is not None:
transformed = self.hash(hashing, transformed)

if properties.get("type") == "dict":
transformed = json.loads(transformed)

return transformed

@staticmethod
Expand Down Expand Up @@ -180,4 +183,19 @@ def generate_event(self, service_name: str, event_type: str, values_to_sub: Dict
data = json.dumps(data, indent=2)

# return the substituted file (A string containing the rendered template.)
return renderer.render(data, values_to_sub)
rendered = renderer.render(data, values_to_sub)

# Parse the rendered result
rendered_json = json.loads(rendered)

key_map = {k.lower(): k for k in rendered_json.keys()}

# Update dictionary values in rendered result
for key, value in values_to_sub.items():
if isinstance(value, dict):
original_key = key_map.get(key.lower())
if original_key and isinstance(rendered_json[original_key], str):
rendered_json[original_key] = value

# Return the final JSON with proper dictionary values
return json.dumps(rendered_json, indent=2)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"path": "/{{{path}}}",
"httpMethod": "{{{method}}}",
"isBase64Encoded": true,
"queryStringParameters": {
"foo": "bar"
},
"queryStringParameters": "{{{querystringparameters}}}",
"multiValueQueryStringParameters": {
"foo": [
"bar"
Expand Down