| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { LayerConfigJson } from "../Json/LayerConfigJson" | 
					
						
							|  |  |  | import { Utils } from "../../../Utils" | 
					
						
							| 
									
										
										
										
											2023-09-17 13:45:46 +02:00
										 |  |  | import { QuestionableTagRenderingConfigJson } from "../Json/QuestionableTagRenderingConfigJson" | 
					
						
							| 
									
										
										
										
											2023-11-02 04:35:32 +01:00
										 |  |  | import { ConversionContext } from "./ConversionContext" | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface DesugaringContext { | 
					
						
							| 
									
										
										
										
											2023-09-17 13:45:46 +02:00
										 |  |  |     tagRenderings: Map<string, QuestionableTagRenderingConfigJson> | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  | export type ConversionMsgLevel = "debug" | "information" | "warning" | "error" | 
					
						
							| 
									
										
										
										
											2024-01-19 17:31:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  | export interface ConversionMessage { | 
					
						
							| 
									
										
										
										
											2024-01-19 17:31:35 +01:00
										 |  |  |     readonly context: ConversionContext | 
					
						
							|  |  |  |     readonly message: string | 
					
						
							|  |  |  |     readonly level: ConversionMsgLevel | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     public convertStrict(json: TIn, context?: ConversionContext): TOut { | 
					
						
							|  |  |  |         context ??= ConversionContext.construct([], []) | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  |         context = context.inOperation(this.name) | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         const fixed = this.convert(json, context) | 
					
						
							|  |  |  |         for (const msg of context.messages) { | 
					
						
							| 
									
										
										
										
											2023-10-30 17:00:39 +01:00
										 |  |  |             if (msg.level === "debug") { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |             ConversionContext.print(msg) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (context.hasErrors()) { | 
					
						
							| 
									
										
										
										
											2022-03-17 23:04:00 +01:00
										 |  |  |             throw "Detected one or more errors, stopping now" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         return fixed | 
					
						
							| 
									
										
										
										
											2022-07-06 11:14:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     public abstract convert(json: TIn, context: ConversionContext): TOut | 
					
						
							| 
									
										
										
										
											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> {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  | export 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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(json: TIn, context: ConversionContext): TOut { | 
					
						
							|  |  |  |         const r0 = this._step0.convert(json, context.inOperation(this._step0.name)) | 
					
						
							|  |  |  |         return this._step1.convert(r0, context.inOperation(this._step1.name)) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  | export 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(json: TIn, context: ConversionContext): TOut { | 
					
						
							|  |  |  |         return this._f(json) | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 00:39:55 +01:00
										 |  |  | export class Bypass<T> extends DesugaringStep<T> { | 
					
						
							|  |  |  |     private readonly _applyIf: (t: T) => boolean | 
					
						
							|  |  |  |     private readonly _step: DesugaringStep<T> | 
					
						
							| 
									
										
										
										
											2024-01-19 17:31:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 00:39:55 +01:00
										 |  |  |     constructor(applyIf: (t: T) => boolean, step: DesugaringStep<T>) { | 
					
						
							|  |  |  |         super("Applies the step on the object, if the object satisfies the predicate", [], "Bypass") | 
					
						
							|  |  |  |         this._applyIf = applyIf | 
					
						
							|  |  |  |         this._step = step | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     convert(json: T, context: ConversionContext): T { | 
					
						
							|  |  |  |         if (!this._applyIf(json)) { | 
					
						
							|  |  |  |             return json | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return this._step.convert(json, context) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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> | 
					
						
							| 
									
										
										
										
											2023-10-30 16:32:43 +01:00
										 |  |  |     private readonly _msg: string | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 00:39:55 +01:00
										 |  |  |     constructor(step: Conversion<X, Y>, options?: { msg?: string }) { | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2023-11-30 00:39:55 +01:00
										 |  |  |         this._msg = options?.msg | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(values: X[], context: ConversionContext): Y[] { | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         if (values === undefined || values === null) { | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |             return <undefined | null>values | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         const step = this._step | 
					
						
							|  |  |  |         const result: Y[] = [] | 
					
						
							| 
									
										
										
										
											2023-10-17 16:06:58 +02:00
										 |  |  |         const c = context.inOperation("each") | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         for (let i = 0; i < values.length; i++) { | 
					
						
							| 
									
										
										
										
											2023-10-30 16:32:43 +01:00
										 |  |  |             if (this._msg) { | 
					
						
							| 
									
										
										
										
											2023-10-31 01:39:01 +01:00
										 |  |  |                 console.log( | 
					
						
							| 
									
										
										
										
											2023-10-30 17:00:39 +01:00
										 |  |  |                     this._msg, | 
					
						
							|  |  |  |                     `: ${i + 1}/${values.length}`, | 
					
						
							|  |  |  |                     values[i]?.["id"] !== undefined ? values[i]?.["id"] : "" | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-10-30 16:32:43 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-11-13 13:45:22 +01:00
										 |  |  |             const r = step.convert(values[i], c.enter(i)) | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |             result.push(r) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         return 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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(json: T, context: ConversionContext): T { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const key = this.key | 
					
						
							| 
									
										
										
										
											2024-02-21 18:47:12 +01:00
										 |  |  |         const value: P = json?.[key] | 
					
						
							| 
									
										
										
										
											2022-04-06 03:06:50 +02:00
										 |  |  |         if (value === undefined || value === null) { | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  |             return json | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  |         json = { ...json } | 
					
						
							|  |  |  |         const step = this.step(json) | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         json[key] = step.convert(value, context.enter(key).inOperation("on[" + key + "]")) | 
					
						
							|  |  |  |         return json | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:45:20 +01:00
										 |  |  |     convert(json: T, _: ConversionContext): T { | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         return 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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(values: X[], context: ConversionContext): T[] { | 
					
						
							| 
									
										
										
										
											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!
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |             return <undefined | null>values | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         const vals: T[][] = new Each(this._step).convert(values, context.inOperation("concat")) | 
					
						
							|  |  |  |         return [].concat(...vals) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(json: T, context: ConversionContext): X { | 
					
						
							|  |  |  |         const values = this._conversion.convert(json, context.inOperation("firstOf")) | 
					
						
							|  |  |  |         if (values.length === 0) { | 
					
						
							|  |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         return values[0] | 
					
						
							| 
									
										
										
										
											2022-07-03 13:18:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  | export class Cached<TIn, TOut> extends Conversion<TIn, TOut> { | 
					
						
							|  |  |  |     private _step: Conversion<TIn, TOut> | 
					
						
							|  |  |  |     private readonly key: string | 
					
						
							| 
									
										
										
										
											2024-01-19 17:31:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  |     constructor(step: Conversion<TIn, TOut>) { | 
					
						
							|  |  |  |         super("Secretly caches the output for the given input", [], "cached") | 
					
						
							|  |  |  |         this._step = step | 
					
						
							|  |  |  |         this.key = "__super_secret_caching_key_" + step.name | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     convert(json: TIn, context: ConversionContext): TOut { | 
					
						
							|  |  |  |         if (json[this.key]) { | 
					
						
							|  |  |  |             return json[this.key] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const converted = this._step.convert(json, context) | 
					
						
							|  |  |  |         Object.defineProperty(json, this.key, { | 
					
						
							|  |  |  |             value: converted, | 
					
						
							|  |  |  |             enumerable: false, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         return converted | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-01-19 17:31:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | export class Fuse<T> extends DesugaringStep<T> { | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  |     protected debug = false | 
					
						
							| 
									
										
										
										
											2024-01-19 17:31:35 +01: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))), | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  |             "Fuse(" + steps.map((s) => s.name).join(", ") + ")" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |         this.steps = Utils.NoNull(steps) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  |     public enableDebugging(): Fuse<T> { | 
					
						
							|  |  |  |         this.debug = true | 
					
						
							|  |  |  |         return this | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |     convert(json: T, context: ConversionContext): T { | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  |         const timings = [] | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         for (let i = 0; i < this.steps.length; i++) { | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  |             const start = new Date() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const step = this.steps[i] | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |                 const r = step.convert(json, context.inOperation(step.name)) | 
					
						
							| 
									
										
										
										
											2023-10-12 16:55:26 +02:00
										 |  |  |                 if (r === undefined || r === null) { | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |                     break | 
					
						
							| 
									
										
										
										
											2022-04-22 03:17:40 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |                 json = r | 
					
						
							| 
									
										
										
										
											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
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-31 11:49:14 +01:00
										 |  |  |             if (this.debug) { | 
					
						
							|  |  |  |                 const stop = new Date() | 
					
						
							|  |  |  |                 const timeNeededMs = stop.getTime() - start.getTime() | 
					
						
							|  |  |  |                 timings.push(timeNeededMs) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (this.debug) { | 
					
						
							|  |  |  |             console.log("Time needed,", timings.join(", ")) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         return json | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:45:20 +01:00
										 |  |  |     convert(json: T, _: ConversionContext): T { | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         if (json === undefined) { | 
					
						
							|  |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 04:16:52 +02:00
										 |  |  |         return json | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |