AggregateDescriptor.ts
40 lines
| 1.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r118 | import { Descriptor } from "./interfaces"; | ||
|
|
r49 | import { ActivationContext } from "./ActivationContext"; | ||
| import { isPrimitive } from "../safe"; | ||||
|
|
r118 | import { isDescriptor } from "./traits"; | ||
|
|
r49 | |||
|
|
r120 | export class AggregateDescriptor<S extends object, T> implements Descriptor<S, T> { | ||
|
|
r118 | _value: any; | ||
|
|
r49 | |||
|
|
r118 | constructor(value: any) { | ||
|
|
r49 | this._value = value; | ||
| } | ||||
|
|
r118 | activate(context: ActivationContext<S>): T { | ||
|
|
r49 | return this._parse(this._value, context, "$value"); | ||
| } | ||||
|
|
r118 | _parse(value: any, context: ActivationContext<S>, path: string): any { | ||
|
|
r49 | if (isPrimitive(value)) | ||
|
|
r114 | return value as any; | ||
|
|
r49 | |||
| if (isDescriptor(value)) | ||||
| return context.activate(value, path); | ||||
| if (value instanceof Array) | ||||
|
|
r114 | return value.map((x, i) => this._parse(x, context, `${path}[${i}]`)) as any; | ||
|
|
r49 | |||
|
|
r114 | const t: any = {}; | ||
| for (const p in value) | ||||
|
|
r49 | t[p] = this._parse(value[p], context, `${path}.${p}`); | ||
| return t; | ||||
| } | ||||
| toString() { | ||||
| return "@walk"; | ||||
| } | ||||
|
|
r129 | |||
| clone() { | ||||
| return this; | ||||
| } | ||||
|
|
r49 | } | ||
