AggregateDescriptor.ts
37 lines
| 966 B
| video/mp2t
|
TypeScriptLexer
|
|
r49 | import { Descriptor, isDescriptor } from "./interfaces"; | ||
| import { ActivationContext } from "./ActivationContext"; | ||||
| import { isPrimitive } from "../safe"; | ||||
|
|
r113 | export class AggregateDescriptor<T> implements Descriptor<T> { | ||
| _value: T; | ||||
|
|
r49 | |||
|
|
r113 | constructor(value: T) { | ||
|
|
r49 | this._value = value; | ||
| } | ||||
| activate(context: ActivationContext) { | ||||
| return this._parse(this._value, context, "$value"); | ||||
| } | ||||
| // TODO: make async | ||||
|
|
r113 | _parse(value: T, context: ActivationContext, path: string) { | ||
|
|
r49 | if (isPrimitive(value)) | ||
| return value; | ||||
| if (isDescriptor(value)) | ||||
| return context.activate(value, path); | ||||
| if (value instanceof Array) | ||||
| return value.map((x, i) => this._parse(x, context, `${path}[${i}]`)); | ||||
| const t = {}; | ||||
| for (const p of Object.keys(value)) | ||||
| t[p] = this._parse(value[p], context, `${path}.${p}`); | ||||
| return t; | ||||
| } | ||||
| toString() { | ||||
| return "@walk"; | ||||
| } | ||||
| } | ||||
