AggregateDescriptor.ts
35 lines
| 986 B
| video/mp2t
|
TypeScriptLexer
|
|
r115 | import { Descriptor, isDescriptor, Parse } from "./interfaces"; | ||
|
|
r49 | import { ActivationContext } from "./ActivationContext"; | ||
| import { isPrimitive } from "../safe"; | ||||
|
|
r114 | export class AggregateDescriptor<T> implements Descriptor<Parse<T>> { | ||
|
|
r113 | _value: T; | ||
|
|
r49 | |||
|
|
r113 | constructor(value: T) { | ||
|
|
r49 | this._value = value; | ||
| } | ||||
|
|
r114 | activate<S>(context: ActivationContext<S>) { | ||
|
|
r49 | return this._parse(this._value, context, "$value"); | ||
| } | ||||
|
|
r114 | _parse<S, V>(value: V, context: ActivationContext<S>, path: string): Parse<V> { | ||
|
|
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"; | ||||
| } | ||||
| } | ||||
