Skip to content

Commit 97de6a0

Browse files
committed
feat: Used the reverse transformation to alter the import output to match a user's config + added tests
1 parent 72e69fc commit 97de6a0

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
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.133",
3+
"version": "1.0.135",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/resource/parsed-resource-settings.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { StatefulParameterController } from '../stateful-parameter/stateful-para
55
import {
66
ArrayParameterSetting,
77
DefaultParameterSetting,
8+
InputTransformation,
89
ParameterSetting,
910
resolveElementEqualsFn,
1011
resolveEqualsFn,
@@ -136,7 +137,7 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
136137
});
137138
}
138139

139-
get inputTransformations(): Partial<Record<keyof T, (a: unknown, parameter: ParameterSetting) => unknown>> {
140+
get inputTransformations(): Partial<Record<keyof T, InputTransformation>> {
140141
return this.getFromCacheOrCreate('inputTransformations', () => {
141142
if (!this.settings.parameterSettings) {
142143
return {};
@@ -145,8 +146,8 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
145146
return Object.fromEntries(
146147
Object.entries(this.settings.parameterSettings)
147148
.filter(([_, v]) => resolveParameterTransformFn(v!) !== undefined)
148-
.map(([k, v]) => [k, resolveParameterTransformFn(v!)!.to] as const)
149-
) as Record<keyof T, (a: unknown) => unknown>;
149+
.map(([k, v]) => [k, resolveParameterTransformFn(v!)] as const)
150+
) as Record<keyof T, InputTransformation>;
150151
});
151152
}
152153

src/resource/resource-controller.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,88 @@ describe('Resource tests', () => {
535535
expect(parameter1.modify.calledOnce).to.be.true;
536536
expect(parameter2.addItem.calledOnce).to.be.true;
537537
});
538+
539+
it('Applies reverse input transformations for imports', async () => {
540+
const resource = new class extends TestResource {
541+
getSettings(): ResourceSettings<TestConfig> {
542+
return {
543+
id: 'resourceType',
544+
parameterSettings: {
545+
propD: {
546+
type: 'array',
547+
inputTransformation: {
548+
to: (hosts: Record<string, unknown>[]) => hosts.map((h) => Object.fromEntries(
549+
Object.entries(h)
550+
.map(([k, v]) => [
551+
k,
552+
typeof v === 'boolean'
553+
? (v ? 'yes' : 'no') // The file takes 'yes' or 'no' instead of booleans
554+
: v,
555+
])
556+
)
557+
),
558+
from: (hosts: Record<string, unknown>[]) => hosts.map((h) => Object.fromEntries(
559+
Object.entries(h)
560+
.map(([k, v]) => [
561+
k,
562+
v === 'yes' || v === 'no'
563+
? (v === 'yes')
564+
: v,
565+
])
566+
))
567+
}
568+
}
569+
}
570+
}
571+
}
572+
573+
async refresh(parameters: Partial<TestConfig>): Promise<Partial<TestConfig> | null> {
574+
return {
575+
propD: [
576+
{
577+
Host: 'new.com',
578+
AddKeysToAgent: true,
579+
IdentityFile: 'id_ed25519'
580+
},
581+
{
582+
Host: 'github.com',
583+
AddKeysToAgent: true,
584+
UseKeychain: true,
585+
},
586+
{
587+
Match: 'User bob,joe,phil',
588+
PasswordAuthentication: true,
589+
}
590+
],
591+
}
592+
}
593+
}
594+
595+
const controller = new ResourceController(resource);
596+
const plan = await controller.import({ type: 'resourceType' }, {});
597+
598+
expect(plan![0]).toMatchObject({
599+
'core': {
600+
'type': 'resourceType'
601+
},
602+
'parameters': {
603+
'propD': [
604+
{
605+
'Host': 'new.com',
606+
'AddKeysToAgent': true,
607+
'IdentityFile': 'id_ed25519'
608+
},
609+
{
610+
'Host': 'github.com',
611+
'AddKeysToAgent': true,
612+
'UseKeychain': true
613+
},
614+
{
615+
'Match': 'User bob,joe,phil',
616+
'PasswordAuthentication': true
617+
}
618+
]
619+
}
620+
})
621+
})
538622
});

src/resource/resource-controller.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ export class ResourceController<T extends StringIndexedObject> {
254254
}
255255

256256
const statefulCurrentParameters = await this.refreshStatefulParameters(allStatefulParameters, parametersToRefresh);
257-
return [{ core, parameters: { ...currentParametersArray[0], ...statefulCurrentParameters } }];
257+
const resultParameters = { ...currentParametersArray[0], ...statefulCurrentParameters };
258+
259+
await this.applyTransformParameters(resultParameters, true);
260+
return [{ core, parameters: resultParameters }];
258261
}
259262

260263
private async applyCreate(plan: Plan<T>): Promise<void> {
@@ -333,7 +336,7 @@ ${JSON.stringify(refresh, null, 2)}
333336
}
334337
}
335338

336-
private async applyTransformParameters(config: Partial<T> | null): Promise<void> {
339+
private async applyTransformParameters(config: Partial<T> | null, reverse = false): Promise<void> {
337340
if (!config) {
338341
return;
339342
}
@@ -343,7 +346,9 @@ ${JSON.stringify(refresh, null, 2)}
343346
continue;
344347
}
345348

346-
(config as Record<string, unknown>)[key] = await inputTransformation(config[key], this.settings.parameterSettings![key]!);
349+
(config as Record<string, unknown>)[key] = reverse
350+
? await inputTransformation.from(config[key])
351+
: await inputTransformation.to(config[key]);
347352
}
348353

349354
if (this.settings.inputTransformation) {

0 commit comments

Comments
 (0)