| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | import {TagRenderingConfigJson} from "../Json/TagRenderingConfigJson"; | 
					
						
							|  |  |  | import {LayerConfigJson} from "../Json/LayerConfigJson"; | 
					
						
							|  |  |  | import {Utils} from "../../../Utils"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface DesugaringContext { | 
					
						
							|  |  |  |     tagRenderings: Map<string, TagRenderingConfigJson> | 
					
						
							|  |  |  |     sharedLayers: Map<string, LayerConfigJson> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export abstract class Conversion<TIn, TOut> { | 
					
						
							|  |  |  |     public readonly modifiedAttributes: string[]; | 
					
						
							|  |  |  |     protected readonly doc: string; | 
					
						
							| 
									
										
										
										
											2022-01-31 14:34:06 +01:00
										 |  |  |     public readonly name: string | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01:00
										 |  |  |     constructor(doc: string, modifiedAttributes: string[] = [], name: string) { | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         this.modifiedAttributes = modifiedAttributes; | 
					
						
							|  |  |  |         this.doc = doc + "\n\nModified attributes are\n" + modifiedAttributes.join(", "); | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01:00
										 |  |  |         this.name = name  | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |     public static strict<T>(fixed: { errors?: string[], warnings?: string[], information?: string[], result?: T }): T { | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:09 +01:00
										 |  |  |         if (fixed?.errors !== undefined &&  fixed?.errors?.length > 0) { | 
					
						
							|  |  |  |             throw fixed.errors.join("\n\n"); | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |         fixed.information?.forEach(i => console.log("    ", i)) | 
					
						
							|  |  |  |         const    yellow = (s) => "\x1b[33m"+s+"\x1b[0m" | 
					
						
							|  |  |  |         const red = s => '\x1b[31m'+s+'\x1b[0m' | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         fixed.warnings?.forEach(w => console.warn(red(`<!> `), yellow (w))) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         return fixed.result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-10 23:16:14 +01:00
										 |  |  |     abstract convert(json: TIn, context: string): { result: TOut, errors?: string[], warnings?: string[], information?: string[] } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |     public convertAll(jsons: TIn[], context: string): { result: TOut[], errors: string[], warnings: string[], information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:09 +01:00
										 |  |  |         if(jsons === undefined){ | 
					
						
							|  |  |  |             throw "convertAll received undefined - don't do this (at "+context+")" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         const result = [] | 
					
						
							|  |  |  |         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 < jsons.length; i++) { | 
					
						
							|  |  |  |             const json = jsons[i]; | 
					
						
							| 
									
										
										
										
											2022-02-04 01:05:35 +01:00
										 |  |  |             const r = this.convert(json, context + "[" + i + "]") | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             result.push(r.result) | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:09 +01:00
										 |  |  |             errors.push(...r.errors ?? []) | 
					
						
							|  |  |  |             warnings.push(...r.warnings ?? []) | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             information.push(...r.information ?? []) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result, | 
					
						
							|  |  |  |             errors, | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             warnings, | 
					
						
							|  |  |  |             information | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export abstract class DesugaringStep<T> extends Conversion<T, T> { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class OnEvery<X, T> extends DesugaringStep<T> { | 
					
						
							|  |  |  |     private readonly key: string; | 
					
						
							|  |  |  |     private readonly step: DesugaringStep<X>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(key: string, step: DesugaringStep<X>) { | 
					
						
							| 
									
										
										
										
											2022-02-08 00:56:47 +01:00
										 |  |  |         super("Applies " + step.name + " onto every object of the list `key`", [key], "OnEvery("+step.name+")"); | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         this.step = step; | 
					
						
							|  |  |  |         this.key = key; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |     convert(json: T, context: string): { result: T; errors?: string[]; warnings?: string[], information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         json = {...json} | 
					
						
							|  |  |  |         const step = this.step | 
					
						
							|  |  |  |         const key = this.key; | 
					
						
							| 
									
										
										
										
											2022-02-04 01:05:35 +01:00
										 |  |  |         const r = step.convertAll((<X[]>json[key]), context + "." + key) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         json[key] = r.result | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             ...r, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             result: json, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class OnEveryConcat<X, T> extends DesugaringStep<T> { | 
					
						
							|  |  |  |     private readonly key: string; | 
					
						
							|  |  |  |     private readonly step: Conversion<X, X[]>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(key: string, step: Conversion<X, X[]>) { | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01:00
										 |  |  |         super(`Applies ${step.name} onto every object of the list \`${key}\`. The results are concatenated and used as new list`, [key],  | 
					
						
							|  |  |  |             "OnEvery("+key+").Concat("+step.name+")"); | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         this.step = step; | 
					
						
							|  |  |  |         this.key = key; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |     convert(json: T, context: string): { result: T; errors?: string[]; warnings?: string[], information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         json = {...json} | 
					
						
							|  |  |  |         const step = this.step | 
					
						
							|  |  |  |         const key = this.key; | 
					
						
							|  |  |  |         const values = json[key] | 
					
						
							|  |  |  |         if (values === undefined) { | 
					
						
							|  |  |  |             // Move on - nothing to see here!
 | 
					
						
							|  |  |  |             return { | 
					
						
							|  |  |  |                 result: json, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-04 01:05:35 +01:00
										 |  |  |         const r = step.convertAll((<X[]>values), context + "." + key) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         const vals: X[][] = r.result | 
					
						
							|  |  |  |         json[key] = [].concat(...vals) | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             ...r, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             result: json, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class Fuse<T> extends DesugaringStep<T> { | 
					
						
							|  |  |  |     private readonly steps: DesugaringStep<T>[]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(doc: string, ...steps: DesugaringStep<T>[]) { | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01: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(", ") | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-02-17 23:54:14 +01:00
										 |  |  |         this.steps = Utils.NoNull(steps); | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01: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++) { | 
					
						
							|  |  |  |             const step = this.steps[i]; | 
					
						
							| 
									
										
										
										
											2022-02-04 01:05:35 +01:00
										 |  |  |             let r = step.convert(json, "While running step " +step.name + ": " + context) | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:09 +01:00
										 |  |  |             errors.push(...r.errors ?? []) | 
					
						
							|  |  |  |             warnings.push(...r.warnings ?? []) | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             information.push(...r.information ?? []) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             json = r.result | 
					
						
							|  |  |  |             if (errors.length > 0) { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: json, | 
					
						
							|  |  |  |             errors, | 
					
						
							| 
									
										
										
										
											2022-02-10 23:16:14 +01:00
										 |  |  |             warnings, | 
					
						
							|  |  |  |             information | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class SetDefault<T> extends DesugaringStep<T> { | 
					
						
							|  |  |  |     private readonly value: any; | 
					
						
							|  |  |  |     private readonly key: string; | 
					
						
							|  |  |  |     private readonly _overrideEmptyString: boolean; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(key: string, value: any, overrideEmptyString = false) { | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01:00
										 |  |  |         super("Sets " + key + " to a default value if undefined", [], "SetDefault of "+key); | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         this.key = key; | 
					
						
							|  |  |  |         this.value = value; | 
					
						
							|  |  |  |         this._overrideEmptyString = overrideEmptyString; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							|  |  |  |             json = {...json} | 
					
						
							|  |  |  |             json[this.key] = this.value | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: json | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |