Skip to content

Commit

Permalink
fix: workaround for jsonpath issue with hyphenated json keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Apr 5, 2021
1 parent 1eb1816 commit 77a7fb3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sam-patterns-cli",
"version": "0.0.6",
"version": "0.0.7",
"description": "Command line interface for quickly using patterns from https://github.com/aws-samples/serverless-patterns/",
"main": "index.js",
"scripts": {
Expand Down
81 changes: 42 additions & 39 deletions src/commands/import/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { Separator } = require("inquirer");

async function transform(template) {
const metadata = template.Metadata;
if (!metadata || !metadata.PatternTransform) {
if (!metadata || !metadata.PatternTransform) {
return template;
}
console.log("Applying transforms...");
Expand All @@ -19,8 +19,8 @@ async function placeholderTransforms(template) {

let templateString = JSON.stringify(template);
for (const property of metadata.PatternTransform.Placeholders || []) {
const value = await inputUtil.text(property.Message ||
`Placeholder value for ${property.Placeholder}:`
const value = await inputUtil.text(
property.Message || `Placeholder value for ${property.Placeholder}:`
);
templateString = templateString.replaceAll(property.Placeholder, value);
}
Expand All @@ -29,44 +29,47 @@ async function placeholderTransforms(template) {
async function propertyTransforms(template) {
const metadata = template.Metadata;
for (const property of metadata.PatternTransform.Properties || []) {
if (!jp.query(template, property.JSONPath).length)
continue
let defaultValue, value;
switch (property.InputType) {
case "number":
defaultValue = jp.query(template, property.JSONPath);
value = parseInt(
await inputUtil.text(
`${property.Message || "Set value for " + property.JSONPath}:`,
defaultValue.length ? defaultValue[0] : undefined
)
);
try {
if (!jp.query(template, property.JSONPath).length) continue;
let defaultValue, value;
switch (property.InputType) {
case "number":
defaultValue = jp.query(template, property.JSONPath);
value = parseInt(
await inputUtil.text(
`${property.Message || "Set value for " + property.JSONPath}:`,
defaultValue.length ? defaultValue[0] : undefined
)
);

break;
case "string":
case "text":
defaultValue = jp.query(template, property.JSONPath);
value = await inputUtil.text(
property.Message,
defaultValue.length ? defaultValue[0] : undefined
);
break;
case "runtime-select":
value =
process.env.SAM_PATTERNS_DEFAULT_RUNTIME ||
(await inputUtil.list(
`Select Lambda runtime for ${JSONPathToFrieldlyName(
property.JSONPath
)}.`,
[
...runtimes.filter((p) => p.latest),
new Separator("*** Older versions ***"),
...runtimes.filter((p) => !p.latest),
]
));
break;
break;
case "string":
case "text":
defaultValue = jp.query(template, property.JSONPath);
value = await inputUtil.text(
property.Message,
defaultValue.length ? defaultValue[0] : undefined
);
break;
case "runtime-select":
value =
process.env.SAM_PATTERNS_DEFAULT_RUNTIME ||
(await inputUtil.list(
`Select Lambda runtime for ${JSONPathToFrieldlyName(
property.JSONPath
)}.`,
[
...runtimes.filter((p) => p.latest),
new Separator("*** Older versions ***"),
...runtimes.filter((p) => !p.latest),
]
));
break;
}
if (value) jp.value(template, property.JSONPath, value);
} catch (err) {
console.log("Could not parse path " + property.JSONPath);
}
if (value) jp.value(template, property.JSONPath, value);
}
return template;
}
Expand Down

0 comments on commit 77a7fb3

Please sign in to comment.