Skip to content

Commit

Permalink
fix(config): issue with nested keys in conditional template strings
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 4, 2019
1 parent 458ac9e commit 35ad3df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 4 additions & 4 deletions garden-service/src/template-string-parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ FormatString
return options.getKey(key)
}
/ FormatStart a:Key Or b:Key FormatEnd {
return options.resolve(a, { allowUndefined: true })
return options.getKey(a, { allowUndefined: true })
.then(result => {
return result || options.resolve(b, { allowUndefined: false })
return result || options.getKey(b, { allowUndefined: false })
})
}
/ FormatStart a:Key Or b:StringLiteral FormatEnd {
return options.resolve(a, { allowUndefined: true })
return options.getKey(a, { allowUndefined: true })
.then(result => {
return result || b
})
}
// These would be odd in configuration, but there's no reason to throw if it comes up.
/ FormatStart a:StringLiteral Or b:StringLiteral FormatEnd {
return a
return a || b
}
/ FormatStart a:StringLiteral FormatEnd {
return a
Expand Down
4 changes: 3 additions & 1 deletion garden-service/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ async function getParser() {
export async function resolveTemplateString(string: string, context: ConfigContext, opts: ContextResolveOpts = {}) {
const parser = await getParser()
const parsed = parser.parse(string, {
getKey: async (key: string[]) => context.resolve({ key, nodePath: [], opts }),
getKey: async (key: string[], resolveOpts?: ContextResolveOpts) => {
return context.resolve({ key, nodePath: [], opts: { ...opts, ...resolveOpts || {} } })
},
// need this to allow nested template strings
resolve: async (parts: StringOrStringPromise[], resolveOpts?: ContextResolveOpts) => {
const s = (await Bluebird.all(parts)).join("")
Expand Down
22 changes: 22 additions & 0 deletions garden-service/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ describe("resolveTemplateString", async () => {
expect(res).to.equal("123")
})

it("should handle a conditional between two nested identifiers", async () => {
const res = await resolveTemplateString(
"${a.b || c.d}",
new TestContext({
a: { b: undefined },
c: { d: "123" },
}),
)
expect(res).to.equal("123")
})

it("should handle a conditional between two nested identifiers where the first resolves", async () => {
const res = await resolveTemplateString(
"${a.b || c.d}",
new TestContext({
a: { b: "123" },
c: { d: undefined },
}),
)
expect(res).to.equal("123")
})

it("should handle a conditional between two identifiers without spaces", async () => {
const res = await resolveTemplateString(
"${a||b}",
Expand Down

0 comments on commit 35ad3df

Please sign in to comment.