-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wip data-items standalone processing
- Loading branch information
1 parent
995e628
commit fff347e
Showing
4 changed files
with
219 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/app/shared/components/template/components/data-items/data-items.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { TemplateVariablesService } from "../../services/template-variables.service"; | ||
import { FlowTypes } from "packages/data-models"; | ||
import { flattenJson } from "../../utils"; | ||
import { getNestedProperty, setNestedProperty } from "src/app/shared/utils"; | ||
import { addStringDelimiters } from "packages/shared/src/utils/delimiters"; | ||
|
||
type IDynamicHashmap = Record<string, { type: FlowTypes.IDynamicPrefix; fieldName: string }>; | ||
export type IDataItemsEvalContext = { | ||
[prefix in FlowTypes.IDynamicPrefix]?: { [fieldName: string]: any }; | ||
}; | ||
|
||
@Injectable({ providedIn: "root" }) | ||
export class DataItemsService { | ||
constructor(private templateVariablesService: TemplateVariablesService) {} | ||
|
||
public async parseRow(row: FlowTypes.TemplateRow, templateRowMap = {}) { | ||
const parser = new DataItemsParser(); | ||
const parsed = parser.parse(row); | ||
const evalContext: IDataItemsEvalContext = {}; | ||
for (const { fieldName, type } of Object.values(parser.variables)) { | ||
const { parsedValue } = await this.templateVariablesService.getDynamicFieldValue( | ||
type, | ||
fieldName, | ||
templateRowMap | ||
); | ||
evalContext[type] ??= {}; | ||
evalContext[type][fieldName] = parsedValue; | ||
} | ||
return { parsed, evalContext }; | ||
} | ||
|
||
/** Copied from template-row service */ | ||
public async parseDataList(dataList: { [id: string]: any }) { | ||
const parsed: { [id: string]: any } = {}; | ||
for (const [listKey, listValue] of Object.entries(dataList)) { | ||
parsed[listKey] = listValue; | ||
for (const [itemKey, itemValue] of Object.entries(listValue)) { | ||
if (typeof itemValue === "string") { | ||
parsed[listKey][itemKey] = | ||
await this.templateVariablesService.evaluateConditionString(itemValue); | ||
} | ||
} | ||
} | ||
return parsed; | ||
} | ||
} | ||
|
||
// TODO - ideally full template parser should handle flattened | ||
// TODO - compare full and matched expression to calc if further processing required | ||
|
||
class DataItemsParser { | ||
public variables: IDynamicHashmap = {}; | ||
|
||
public parse(row: FlowTypes.TemplateRow) { | ||
const parsed = JSON.parse(JSON.stringify(row)) as FlowTypes.TemplateRow; | ||
const { _dynamicFields, rows } = parsed; | ||
|
||
if (rows) { | ||
parsed.rows = rows.map((r) => this.parse(r)); | ||
} | ||
|
||
if (_dynamicFields) { | ||
const flattened = flattenJson<FlowTypes.TemplateRowDynamicEvaluator[]>(_dynamicFields); | ||
for (const [key, evaluators] of Object.entries(flattened)) { | ||
const expressionVariables: { [prefix in FlowTypes.IDynamicPrefix]?: boolean } = {}; | ||
for (const evaluator of evaluators) { | ||
const { fieldName, type } = evaluator; | ||
// mark fields that are being tracked | ||
expressionVariables[type] = true; | ||
this.variables[`${type}.${fieldName}`] = { type, fieldName }; | ||
} | ||
|
||
const existing = getNestedProperty(parsed, key) as string; | ||
const delimited = addStringDelimiters(existing, Object.keys(expressionVariables)); | ||
// add delimeters but remove initial curly braces and replace all curly braces for `[]` | ||
// so that can be directly evaluated in JS | ||
const replaced = delimtedToJS(delimited); | ||
setNestedProperty(key, replaced, parsed); | ||
} | ||
delete parsed._dynamicFields; | ||
delete parsed._dynamicDependencies; | ||
} | ||
|
||
return parsed; | ||
} | ||
} | ||
|
||
function delimtedToJS(v: string) { | ||
return ( | ||
v | ||
.replace(/@/gi, "this.") | ||
// replace curly brackets for square | ||
.replace(/{/g, "[") | ||
.replace(/}/g, "]") | ||
); | ||
// if | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.