diff --git a/garden-service/src/template-string-parser.pegjs b/garden-service/src/template-string-parser.pegjs index 1c82ebd807..8dadf8aaa8 100644 --- a/garden-service/src/template-string-parser.pegjs +++ b/garden-service/src/template-string-parser.pegjs @@ -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 diff --git a/garden-service/src/template-string.ts b/garden-service/src/template-string.ts index fec6174057..80630e8820 100644 --- a/garden-service/src/template-string.ts +++ b/garden-service/src/template-string.ts @@ -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("") diff --git a/garden-service/test/unit/src/template-string.ts b/garden-service/test/unit/src/template-string.ts index 54215af985..0c7a95dc89 100644 --- a/garden-service/test/unit/src/template-string.ts +++ b/garden-service/test/unit/src/template-string.ts @@ -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}",