Skip to content

Commit

Permalink
handle expression return types better
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Oct 27, 2024
1 parent 8c86ea8 commit fa51757
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/yamler/yamler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ export default class YamlER {
let result = obj
for (const match of matches) {
const expression = parseTemplateExpression(match)

if (expression && this.expressionCallback) {
const value = await this.evaluateExpression(this.baseDir, this.attributes, path, expression.funcName, expression.args)
result = result.replace(match, value)

if (result == match)
result = value // this maintains the type when the expression is not embedded in a string
else
result = result.replace(match, value)
}
}

Expand All @@ -164,7 +169,11 @@ export default class YamlER {
found ||= keyHasExpression

if (Array.isArray(value)) {
const results = await Promise.all(value.map(item => this.parseExpression(path, item)))
const results = []

for (const item of value)
results.push(await this.parseExpression(path, item))

newValue = results.map(([val]) => val)
found ||= results.some(([_, hasExp]) => hasExp)
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/yamler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ describe('YamlER', () => {
let yaml

const expressionCallback = (baseDir, attributes, path, key, args) => {
if (key == 'true') return true
if (key == 'false') return false
if (key == 'null') return null
if (key == 'undefined') return undefined
if (key == 'get') return dig(attributes, args[0])
if (key == 'expression') return '${{ ' + args[0] + ' ' + args[1] + ' }}'
return '<' + [key].concat(args).join(' ') + '>'
Expand Down Expand Up @@ -105,4 +109,32 @@ describe('YamlER', () => {
const result = await loadFile(yaml, expressionCallback)
expect(result.value3).toBe('value1 is value1 and value2 is value2')
})

it('maintains the expression return value type when not embedded in a string', async () => {
yaml = tempYamlFile({
value1: '${{ null }}',
value2: '${{ undefined }}',
value3: '${{ true }}',
value4: '${{ false }}',
})
const result = await loadFile(yaml, expressionCallback)
expect(result.value1).toBe(null)
expect(result.value2).toBe(undefined)
expect(result.value3).toBe(true)
expect(result.value4).toBe(false)
})

it('handles types in embedded strings', async () => {
yaml = tempYamlFile({
value1: 'embedded expression with a ${{ null }} value',
value2: 'embedded expression with a ${{ undefined }} value',
value3: 'embedded expression with a ${{ true }} value',
value4: 'embedded expression with a ${{ false }} value',
})
const result = await loadFile(yaml, expressionCallback)
expect(result.value1).toBe('embedded expression with a null value')
expect(result.value2).toBe('embedded expression with a undefined value')
expect(result.value3).toBe('embedded expression with a true value')
expect(result.value4).toBe('embedded expression with a false value')
})
})

0 comments on commit fa51757

Please sign in to comment.