Skip to content

Commit

Permalink
add expressions cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Oct 21, 2024
1 parent f35bad8 commit cd47922
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/yamler/yamler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ export function dump(obj: any): string {
export default class YamlER {
public filePath: string
public parentFile: string
public rootFile: string
public imports: Record<string, Import>
public content: string
public attributes: Record<string, any>
private expressionCallback: Function | undefined
private expressionsCache: Record<string, any>

constructor(filePath: string, options: { parentFile?: string, expressionCallback?: Function | undefined } = {}) {
this.filePath = resolve(path.dirname(options.parentFile || filePath), filePath)
this.parentFile = options.parentFile
this.expressionCallback = options.expressionCallback

// we only evaluate expressions on the final set of attributes so the cache will only be populated on the "root"
// instance of this class and not any of the instances created by imports
this.expressionsCache = {}
}

get baseDir(): string {
Expand Down Expand Up @@ -106,13 +112,25 @@ export default class YamlER {
this.content = Array.from(prepends).join('\n\n') + '\n\n' + this.content
}

private getCacheKey(name: string, args: any[]): string {
return `${name}:${JSON.stringify(args)}`
}

private evaluateExpression(path, name, args): any {
const cacheKey = this.getCacheKey(name, args)

if (cacheKey in this.expressionsCache)
return this.expressionsCache[cacheKey]

return this.expressionCallback(path, name, args)
}

private parseExpression(path: string, obj: string | undefined | null) {
if (obj && typeof(obj) === 'string') {
const expression = parseTemplateExpression(obj)

if (expression && this.expressionCallback) {
obj = this.expressionCallback(path, expression.funcName, expression.args)
}
if (expression && this.expressionCallback)
obj = this.evaluateExpression(path, expression.funcName, expression.args)
}
return obj
}
Expand Down

0 comments on commit cd47922

Please sign in to comment.