| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { TagRenderingConfigJson } from "../Json/TagRenderingConfigJson" | 
					
						
							|  |  |  | import { LayerConfigJson } from "../Json/LayerConfigJson" | 
					
						
							|  |  |  | import { Utils } from "../../../Utils" | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface DesugaringContext { | 
					
						
							|  |  |  |     tagRenderings: Map<string, TagRenderingConfigJson> | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     sharedLayers: Map<string, LayerConfigJson> | 
					
						
							| 
									
										
										
										
											2022-06-13 03:26:14 +02:00
										 |  |  |     publicLayers?: Set<string> | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export abstract class Conversion<TIn, TOut> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public readonly modifiedAttributes: string[] | 
					
						
							| 
									
										
										
										
											2022-01-31 14:34:06 +01:00
										 |  |  |     public readonly name: string | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     protected readonly doc: string | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01:00
										 |  |  |     constructor(doc: string, modifiedAttributes: string[] = [], name: string) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.modifiedAttributes = modifiedAttributes | 
					
						
							|  |  |  |         this.doc = doc + "\n\nModified attributes are\n" + modifiedAttributes.join(", ") | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         this.name = name | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static strict<T>(fixed: { | 
					
						
							|  |  |  |         errors?: string[] | 
					
						
							|  |  |  |         warnings?: string[] | 
					
						
							|  |  |  |         information?: string[] | 
					
						
							|  |  |  |         result?: T | 
					
						
							|  |  |  |     }): T { | 
					
						
							|  |  |  |         fixed.information?.forEach((i) => console.log("    ", i)) | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         const yellow = (s) => "\x1b[33m" + s + "\x1b[0m" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const red = (s) => "\x1b[31m" + s + "\x1b[0m" | 
					
						
							|  |  |  |         fixed.warnings?.forEach((w) => console.warn(red(`<!> `), yellow(w))) | 
					
						
							| 
									
										
										
										
											2022-03-17 23:04:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         if (fixed?.errors !== undefined && fixed?.errors?.length > 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             fixed.errors?.forEach((e) => console.error(red(`ERR ` + e))) | 
					
						
							| 
									
										
										
										
											2022-03-17 23:04:00 +01:00
										 |  |  |             throw "Detected one or more errors, stopping now" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return fixed.result | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 01:05:35 +01:00
										 |  |  |     public convertStrict(json: TIn, context: string): TOut { | 
					
						
							|  |  |  |         const fixed = this.convert(json, context) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         return DesugaringStep.strict(fixed) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public convertJoin( | 
					
						
							|  |  |  |         json: TIn, | 
					
						
							|  |  |  |         context: string, | 
					
						
							|  |  |  |         errors: string[], | 
					
						
							|  |  |  |         warnings?: string[], | 
					
						
							|  |  |  |         information?: string[] | 
					
						
							|  |  |  |     ): TOut { | 
					
						
							| 
									
										
										
										
											2022-07-06 11:14:19 +02:00
										 |  |  |         const fixed = this.convert(json, context) | 
					
						
							|  |  |  |         errors?.push(...(fixed.errors ?? [])) | 
					
						
							|  |  |  |         warnings?.push(...(fixed.warnings ?? [])) | 
					
						
							|  |  |  |         information?.push(...(fixed.information ?? [])) | 
					
						
							|  |  |  |         return fixed.result | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public andThenF<X>(f: (tout: TOut) => X): Conversion<TIn, X> { | 
					
						
							|  |  |  |         return new Pipe(this, new Pure(f)) | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     abstract convert( | 
					
						
							|  |  |  |         json: TIn, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: TOut; errors?: string[]; warnings?: string[]; information?: string[] } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | export abstract class DesugaringStep<T> extends Conversion<T, T> {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | class Pipe<TIn, TInter, TOut> extends Conversion<TIn, TOut> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _step0: Conversion<TIn, TInter> | 
					
						
							|  |  |  |     private readonly _step1: Conversion<TInter, TOut> | 
					
						
							| 
									
										
										
										
											2023-01-17 02:54:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor(step0: Conversion<TIn, TInter>, step1: Conversion<TInter, TOut>) { | 
					
						
							|  |  |  |         super("Merges two steps with different types", [], `Pipe(${step0.name}, ${step1.name})`) | 
					
						
							|  |  |  |         this._step0 = step0 | 
					
						
							|  |  |  |         this._step1 = step1 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         json: TIn, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: TOut; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							|  |  |  |         const r0 = this._step0.convert(json, context) | 
					
						
							|  |  |  |         const { result, errors, information, warnings } = r0 | 
					
						
							|  |  |  |         if (result === undefined && errors.length > 0) { | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |             return { | 
					
						
							|  |  |  |                 ...r0, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 result: undefined, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const r = this._step1.convert(result, context) | 
					
						
							| 
									
										
										
										
											2023-01-17 02:54:33 +01:00
										 |  |  |         Utils.PushList(errors, r.errors) | 
					
						
							|  |  |  |         Utils.PushList(warnings, r.warnings) | 
					
						
							|  |  |  |         Utils.PushList(information, r.information) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |             result: r.result, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             errors, | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             warnings, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             information, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | class Pure<TIn, TOut> extends Conversion<TIn, TOut> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _f: (t: TIn) => TOut | 
					
						
							| 
									
										
										
										
											2023-01-17 02:54:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor(f: (t: TIn) => TOut) { | 
					
						
							|  |  |  |         super("Wrapper around a pure function", [], "Pure") | 
					
						
							|  |  |  |         this._f = f | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     convert( | 
					
						
							|  |  |  |         json: TIn, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: TOut; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							|  |  |  |         return { result: this._f(json) } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | export class Each<X, Y> extends Conversion<X[], Y[]> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _step: Conversion<X, Y> | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor(step: Conversion<X, Y>) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             "Applies the given step on every element of the list", | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             "OnEach(" + step.name + ")" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         this._step = step | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         values: X[], | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: Y[]; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         if (values === undefined || values === null) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return { result: undefined } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         const information: string[] = [] | 
					
						
							|  |  |  |         const warnings: string[] = [] | 
					
						
							|  |  |  |         const errors: string[] = [] | 
					
						
							|  |  |  |         const step = this._step | 
					
						
							|  |  |  |         const result: Y[] = [] | 
					
						
							|  |  |  |         for (let i = 0; i < values.length; i++) { | 
					
						
							|  |  |  |             const r = step.convert(values[i], context + "[" + i + "]") | 
					
						
							| 
									
										
										
										
											2023-01-17 02:54:33 +01:00
										 |  |  |             Utils.PushList(information, r.information) | 
					
						
							|  |  |  |             Utils.PushList(warnings, r.warnings) | 
					
						
							|  |  |  |             Utils.PushList(errors, r.errors) | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |             result.push(r.result) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             information, | 
					
						
							|  |  |  |             errors, | 
					
						
							|  |  |  |             warnings, | 
					
						
							|  |  |  |             result, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | export class On<P, T> extends DesugaringStep<T> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly key: string | 
					
						
							|  |  |  |     private readonly step: (t: T) => Conversion<P, P> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(key: string, step: Conversion<P, P> | ((t: T) => Conversion<P, P>)) { | 
					
						
							|  |  |  |         super( | 
					
						
							|  |  |  |             "Applies " + step.name + " onto property `" + key + "`", | 
					
						
							|  |  |  |             [key], | 
					
						
							|  |  |  |             `On(${key}, ${step.name})` | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         if (typeof step === "function") { | 
					
						
							|  |  |  |             this.step = step | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             this.step = (_) => step | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.key = key | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         json: T, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: T; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							|  |  |  |         json = { ...json } | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         const step = this.step(json) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const key = this.key | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         const value: P = json[key] | 
					
						
							|  |  |  |         if (value === undefined || value === null) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return { result: json } | 
					
						
							| 
									
										
										
										
											2022-03-29 00:20:10 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         const r = step.convert(value, context + "." + key) | 
					
						
							|  |  |  |         json[key] = r.result | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             ...r, | 
					
						
							|  |  |  |             result: json, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  | export class Pass<T> extends Conversion<T, T> { | 
					
						
							|  |  |  |     constructor(message?: string) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super(message ?? "Does nothing, often to swap out steps in testing", [], "Pass") | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         json: T, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: T; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             result: json, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | export class Concat<X, T> extends Conversion<X[], T[]> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _step: Conversion<X, T[]> | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     constructor(step: Conversion<X, T[]>) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             "Executes the given step, flattens the resulting list", | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             "Concat(" + step.name + ")" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         this._step = step | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         values: X[], | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: T[]; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-02-28 20:21:37 +01:00
										 |  |  |         if (values === undefined || values === null) { | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             // Move on - nothing to see here!
 | 
					
						
							|  |  |  |             return { | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |                 result: undefined, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         const r = new Each(this._step).convert(values, context) | 
					
						
							|  |  |  |         const vals: T[][] = r.result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const flattened: T[] = [].concat(...vals) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             ...r, | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |             result: flattened, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | export class FirstOf<T, X> extends Conversion<T, X> { | 
					
						
							|  |  |  |     private readonly _conversion: Conversion<T, X[]> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 13:18:05 +02:00
										 |  |  |     constructor(conversion: Conversion<T, X[]>) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             "Picks the first result of the conversion step", | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             "FirstOf(" + conversion.name + ")" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         this._conversion = conversion | 
					
						
							| 
									
										
										
										
											2022-07-03 13:18:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         json: T, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: X; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							|  |  |  |         const reslt = this._conversion.convert(json, context) | 
					
						
							| 
									
										
										
										
											2022-07-03 13:18:05 +02:00
										 |  |  |         return { | 
					
						
							|  |  |  |             ...reslt, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             result: reslt.result[0], | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-03 13:18:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | export class Fuse<T> extends DesugaringStep<T> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly steps: DesugaringStep<T>[] | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor(doc: string, ...steps: DesugaringStep<T>[]) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             (doc ?? "") + | 
					
						
							|  |  |  |                 "This fused pipeline of the following steps: " + | 
					
						
							|  |  |  |                 steps.map((s) => s.name).join(", "), | 
					
						
							|  |  |  |             Utils.Dedup([].concat(...steps.map((step) => step.modifiedAttributes))), | 
					
						
							|  |  |  |             "Fuse of " + steps.map((s) => s.name).join(", ") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         this.steps = Utils.NoNull(steps) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     convert( | 
					
						
							|  |  |  |         json: T, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: T; errors: string[]; warnings: string[]; information: string[] } { | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         const errors = [] | 
					
						
							|  |  |  |         const warnings = [] | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |         const information = [] | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         for (let i = 0; i < this.steps.length; i++) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const step = this.steps[i] | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-04-22 03:17:40 +02:00
										 |  |  |                 let r = step.convert(json, "While running step " + step.name + ": " + context) | 
					
						
							| 
									
										
										
										
											2023-05-17 13:22:13 +02:00
										 |  |  |                 if(r.result["tagRenderings"]?.some(tr => tr === undefined)){ | 
					
						
							|  |  |  |                     throw step.name+" introduced an undefined tagRendering" | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 errors.push(...(r.errors ?? [])) | 
					
						
							|  |  |  |                 warnings.push(...(r.warnings ?? [])) | 
					
						
							|  |  |  |                 information.push(...(r.information ?? [])) | 
					
						
							| 
									
										
										
										
											2022-04-22 03:17:40 +02:00
										 |  |  |                 json = r.result | 
					
						
							|  |  |  |                 if (errors.length > 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     break | 
					
						
							| 
									
										
										
										
											2022-04-22 03:17:40 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 console.error("Step " + step.name + " failed due to ", e, e.stack) | 
					
						
							| 
									
										
										
										
											2022-04-22 03:17:40 +02:00
										 |  |  |                 throw e | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: json, | 
					
						
							|  |  |  |             errors, | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             warnings, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             information, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class SetDefault<T> extends DesugaringStep<T> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly value: any | 
					
						
							|  |  |  |     private readonly key: string | 
					
						
							|  |  |  |     private readonly _overrideEmptyString: boolean | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor(key: string, value: any, overrideEmptyString = false) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super("Sets " + key + " to a default value if undefined", [], "SetDefault of " + key) | 
					
						
							|  |  |  |         this.key = key | 
					
						
							|  |  |  |         this.value = value | 
					
						
							|  |  |  |         this._overrideEmptyString = overrideEmptyString | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |     convert(json: T, context: string): { result: T } { | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         if (json[this.key] === undefined || (json[this.key] === "" && this._overrideEmptyString)) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             json = { ...json } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             json[this.key] = this.value | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             result: json, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |