This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
ConcatController.ts
131 lines (108 loc) · 4.29 KB
/
ConcatController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2021. Sendanor <info@sendanor.fi>. All rights reserved.
import {
isReadonlyJsonArray,
isReadonlyJsonObject,
ReadonlyJsonAny,
ReadonlyJsonArray,
ReadonlyJsonObject
} from "../../../../core/Json";
import { Name } from "../../../types/Name";
import { LogService } from "../../../../core/LogService";
import { ControllerType } from "../../types/ControllerType";
import { PipelineContext } from "../../../PipelineContext";
import { StringUtils } from "../../../../core/StringUtils";
import { BaseStepController } from "../types/BaseStepController";
import { ConcatStep, isConcatStep, parseConcatStep } from "./ConcatStep";
import { Controller } from "../../types/Controller";
import { ConcatControllerAction, isConcatControllerAction } from "./ConcatControllerAction";
import { concat } from "../../../../core/functions/concat";
import { reduce } from "../../../../core/functions/reduce";
import { isArray, isArrayOf } from "../../../../core/types/Array";
const LOG = LogService.createLogger('ConcatController');
export class ConcatController extends BaseStepController {
public static parseControllerModel (model: any) : ConcatStep | undefined {
return parseConcatStep(model);
}
public static isControllerModel (model: any) : model is ConcatStep {
return isConcatStep(model);
}
public static createController (
context : PipelineContext,
model : ConcatStep
) : Controller {
return new ConcatController(
context,
model.name,
model,
ConcatControllerAction.CONCAT,
model.output
);
}
/**
*
* @param context The context object, which contains variables, etc.
* @param name The user defined name of the step
* @param input This is usually the primary argument for the action.
* @param action This is usually the value for the property which defines the type of the
* step, eg. `"file"` or `"command"`.
* @param outputVariable The variable name where to save successful results of this step
*/
public constructor (
context : PipelineContext,
name : Name,
input : ReadonlyJsonAny,
action : string | undefined = undefined,
outputVariable : string | undefined = undefined
) {
super(
context,
ControllerType.VARIABLE,
`ConcatController#${name}`,
`concat#${name}`,
name,
input,
action,
outputVariable
);
}
public run (
action : ReadonlyJsonAny | undefined,
input : ReadonlyJsonAny | undefined
) : ReadonlyJsonAny | undefined {
if (!isConcatControllerAction(action)) {
LOG.debug(`run: action = `, action);
throw new TypeError(`[concat#${this.getName()}] failed to compile the action property: ${StringUtils.toString(action)}`);
}
if ( action === ConcatControllerAction.CONCAT ) {
if (!isConcatStep(input)) {
throw new TypeError(`[concat#${this.getName()}] failed to compile the input property as variable: ${StringUtils.toString(input)}`);
}
const concatValues = input?.concat;
if (isArrayOf<ReadonlyJsonArray[]>(concatValues, isReadonlyJsonArray)) {
return concat([], ...concatValues);
} else if (isArrayOf<ReadonlyJsonObject>(concatValues, isReadonlyJsonObject)) {
return reduce(
concatValues,
(obj: ReadonlyJsonObject, item : ReadonlyJsonObject) : ReadonlyJsonObject => {
return {
...obj,
...item
};
},
{}
);
} else if (isArray(concatValues)) {
return concatValues.join('');
} else {
return [concatValues];
}
} else {
LOG.debug(`run: action = `, action);
throw new TypeError(`Unimplemented action: ${action}`)
}
}
}
export function isConcatController (value: any) : value is ConcatController {
return value instanceof ConcatController;
}
export default ConcatController;