Skip to content

Commit 970c80f

Browse files
committed
fix: Added transformations for matcher.
1 parent 63fff9e commit 970c80f

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.150",
3+
"version": "1.0.152",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/plugin/plugin.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class Plugin {
7070
const schema = resource.settings.schema as JSONSchemaType<any> | undefined;
7171
const requiredPropertyNames = (
7272
resource.settings.importAndDestroy?.requiredParameters
73+
?? (typeof resource.settings.allowMultiple === 'object' ? resource.settings.allowMultiple.identifyingParameters : null)
7374
?? schema?.required
7475
?? undefined
7576
) as any;
@@ -101,27 +102,7 @@ export class Plugin {
101102
throw new Error(`Resource of type ${resourceConfig.core.type} could not be found for match`);
102103
}
103104

104-
if (!resource.settings.allowMultiple) {
105-
return { match: array.find((r) => r.core.type === resourceConfig.core.type) }
106-
}
107-
108-
const parameterMatcher = resource?.parsedSettings.matcher;
109-
const match = array.find((r) => {
110-
if (resourceConfig.core.type !== r.core.type) {
111-
return false;
112-
}
113-
114-
// If the user specifies the same name for the resource and it's not auto-generated (a number) then it's the same resource
115-
if (resourceConfig.core.name === r.core.name
116-
&& resourceConfig.core.name
117-
&& Number.isInteger(Number.parseInt(resourceConfig.core.name, 10))
118-
) {
119-
return true;
120-
}
121-
122-
return parameterMatcher(resourceConfig.parameters, r.parameters);
123-
});
124-
105+
const match = await resource.match(resourceConfig, array);
125106
return { match }
126107
}
127108

@@ -179,10 +160,7 @@ export class Plugin {
179160

180161
if (invalidMultipleConfigs.length > 0) {
181162
throw new Error(
182-
`Multiples of the following configs were found but only 1 is allowed.
183-
184-
[${invalidMultipleConfigs.map(([k]) => k).join(', ')}]
185-
`)
163+
`Multiples of the following configs were found but only 1 is allowed. [${invalidMultipleConfigs.map(([k, v]) => `${v}x ${k}`).join(', ')}] found.`)
186164
}
187165

188166
await this.crossValidateResources(data.configs);

src/resource/resource-controller.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ export class ResourceController<T extends StringIndexedObject> {
102102
}
103103
}
104104

105+
async match(resource: ResourceJson, array: Array<ResourceJson>): Promise<ResourceJson | undefined> {
106+
if (resource.core.type !== this.typeId) {
107+
throw new Error(`Unknown type passed into match method: ${resource.core.type} for ${this.typeId}`);
108+
}
109+
110+
if (!this.parsedSettings.allowMultiple) {
111+
return array.find((r) => r.core.type === resource.core.type)
112+
}
113+
114+
115+
const { name, type } = resource.core;
116+
const parameterMatcher = this.parsedSettings.matcher;
117+
118+
for (const resourceToMatch of array) {
119+
if (type !== resourceToMatch.core.type) {
120+
return undefined;
121+
}
122+
123+
// If the user specifies the same name for the resource and it's not auto-generated (a number) then it's the same resource
124+
if (name === resourceToMatch.core.name
125+
&& name
126+
&& Number.isInteger(Number.parseInt(name, 10))
127+
) {
128+
return resourceToMatch;
129+
}
130+
131+
const originalParams = structuredClone(resource.parameters) as Partial<T>;
132+
const paramsToMatch = structuredClone(resourceToMatch.parameters) as Partial<T>;
133+
134+
this.addDefaultValues(originalParams);
135+
await this.applyTransformParameters(originalParams);
136+
137+
this.addDefaultValues(paramsToMatch);
138+
await this.applyTransformParameters(paramsToMatch);
139+
140+
const match = parameterMatcher(originalParams, paramsToMatch);
141+
if (match) {
142+
return resourceToMatch;
143+
}
144+
}
145+
}
146+
105147
async plan(
106148
core: ResourceConfig,
107149
desired: Partial<T> | null,

src/resource/resource-settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export interface ResourceSettings<T extends StringIndexedObject> {
113113

114114
/**
115115
* Customize the required parameters needed to import this resource. By default, the `requiredParameters` are taken
116-
* from the JSON schema. The `requiredParameters` parameter must be declared if a complex required is declared in
116+
* from the identifyingParameters for allowMultiple. The `requiredParameters` parameter must be declared if a complex required is declared in
117117
* the schema (contains `oneOf`, `anyOf`, `allOf`, `if`, `then`, `else`).
118118
* <br>
119119
* The user will be prompted for the required parameters before the import starts. This is done because for most resources

0 commit comments

Comments
 (0)