-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConfigPreprocessorComponentMapped.ts
109 lines (101 loc) 路 4.46 KB
/
ConfigPreprocessorComponentMapped.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
import type { Resource } from 'rdf-object';
import { IRIS_RDF } from '../rdf/Iris';
import type {
IComponentConfigPreprocessorHandleResponse,
} from './ConfigPreprocessorComponent';
import {
ConfigPreprocessorComponent,
} from './ConfigPreprocessorComponent';
import {
ConstructorArgumentsElementMappingHandlerCollectEntries,
} from './constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerCollectEntries';
import {
ConstructorArgumentsElementMappingHandlerElements,
} from './constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerElements';
import {
ConstructorArgumentsElementMappingHandlerFields,
} from './constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerFields';
import {
ConstructorArgumentsElementMappingHandlerKeyValue,
} from './constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerKeyValue';
import {
ConstructorArgumentsElementMappingHandlerList,
} from './constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerList';
import type {
IConstructorArgumentsElementMappingHandler,
} from './constructorargumentsmapping/IConstructorArgumentsElementMappingHandler';
import type {
IConstructorArgumentsMapper,
} from './constructorargumentsmapping/IConstructorArgumentsMapper';
import type { GenericsContext } from './GenericsContext';
/**
* Handles config that refer to a component as type.
* The component may have parameters that can be applied on the config.
* Additionally, the component applies a custom constructor arguments mapping for its parameters.
*/
export class ConfigPreprocessorComponentMapped extends ConfigPreprocessorComponent
implements IConstructorArgumentsMapper {
private readonly mappingHandlers: IConstructorArgumentsElementMappingHandler[] = [
new ConstructorArgumentsElementMappingHandlerKeyValue(),
new ConstructorArgumentsElementMappingHandlerCollectEntries(this.parameterHandler),
new ConstructorArgumentsElementMappingHandlerFields(),
new ConstructorArgumentsElementMappingHandlerElements(),
new ConstructorArgumentsElementMappingHandlerList(),
];
public canHandle(config: Resource): IComponentConfigPreprocessorHandleResponse | undefined {
const handleResponse = super.canHandle(config);
if (handleResponse && !handleResponse.component.property.constructorArguments) {
return;
}
return handleResponse;
}
public transformConstructorArguments(
config: Resource,
handleResponse: IComponentConfigPreprocessorHandleResponse,
): Resource {
const constructorArgs = handleResponse.component.property.constructorArguments;
const genericsContext = this.createGenericsContext(handleResponse, config);
return this.applyConstructorArgumentsParameters(config, constructorArgs, config, genericsContext);
}
public applyConstructorArgumentsParameters(
configRoot: Resource,
constructorArgs: Resource,
configElement: Resource,
genericsContext: GenericsContext,
): Resource {
// Check if this constructor args resource can be handled by one of the built-in handlers.
for (const handler of this.mappingHandlers) {
if (handler.canHandle(configRoot, constructorArgs, configElement, this, genericsContext)) {
return handler.handle(configRoot, constructorArgs, configElement, this, genericsContext);
}
}
// Fallback to original constructor args
return constructorArgs;
}
public getParameterValue(
configRoot: Resource,
parameter: Resource,
configElement: Resource,
rawValue: boolean,
genericsContext: GenericsContext,
): Resource | undefined {
let valueOut: Resource | undefined;
if (parameter.type === 'NamedNode' && parameter.value === IRIS_RDF.subject) {
valueOut = this.objectLoader.createCompactedResource(`"${configElement.value}"`);
} else if (parameter.type === 'NamedNode' && !parameter.property.fields) {
valueOut = this.parameterHandler
.applyParameterValues(configRoot, parameter, configElement, genericsContext);
} else {
valueOut = this.applyConstructorArgumentsParameters(configRoot, parameter, configElement, genericsContext);
}
// If the referenced IRI should become a plain string
if (rawValue) {
valueOut = valueOut?.list ?
this.objectLoader.createCompactedResource({
list: valueOut.list.map(valueOutSub => `"${valueOutSub.value}"`),
}) :
this.objectLoader.createCompactedResource(`"${valueOut ? valueOut.value : 'undefined'}"`);
}
return valueOut;
}
}