Skip to content

Commit 2607ad1

Browse files
committed
fix: Fixed defaultRefreshValue not being used if the parameter has a default value
1 parent 62b60a9 commit 2607ad1

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

src/resource/resource-controller.test.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,6 @@ describe('Resource tests', () => {
767767
},
768768
allowMultiple: {
769769
matcher: (desired, current) => {
770-
// console.log('Matcher');
771-
// console.log(desired);
772-
// console.log(current);
773-
774770
if (desired.path) {
775771
return desired.path === current.path;
776772
}
@@ -791,6 +787,47 @@ describe('Resource tests', () => {
791787
const plan = await controller.plan({ type: 'path' }, { path: '$HOME/.bun/bin' }, null, false);
792788

793789
expect(plan.requiresChanges()).to.be.false;
794-
console.log(JSON.stringify(plan, null, 2));
790+
})
791+
792+
it('Can import with the correct default parameters', async () => {
793+
const resource = new class extends TestResource {
794+
getSettings(): ResourceSettings<any> {
795+
return {
796+
id: 'path',
797+
parameterSettings: {
798+
path: { type: 'string', isEqual: 'directory' },
799+
paths: { canModify: true, type: 'array', isElementEqual: 'directory' },
800+
prepend: { default: false, setting: true },
801+
declarationsOnly: { default: false, setting: true },
802+
},
803+
importAndDestroy: {
804+
refreshKeys: ['paths', 'declarationsOnly'],
805+
defaultRefreshValues: {
806+
paths: [],
807+
declarationsOnly: true,
808+
}
809+
},
810+
allowMultiple: {
811+
matcher: (desired, current) => {
812+
if (desired.path) {
813+
return desired.path === current.path;
814+
}
815+
816+
const currentPaths = new Set(current.paths)
817+
return desired.paths?.some((p) => currentPaths.has(p));
818+
}
819+
}
820+
}
821+
}
822+
823+
async refresh(parameters: Partial<TestConfig>): Promise<Partial<TestConfig> | null> {
824+
expect(parameters.declarationsOnly).to.be.true;
825+
826+
return null;
827+
}
828+
}
829+
830+
const controller = new ResourceController(resource);
831+
const plan = await controller.import({ type: 'path' }, {});
795832
})
796833
});

src/resource/resource-controller.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ export class ResourceController<T extends StringIndexedObject> {
281281
),
282282
...this.settings.importAndDestroy?.defaultRefreshValues,
283283
...parameters,
284+
...(Object.fromEntries( // If a default value was used, but it was also declared in the defaultRefreshValues, prefer the defaultRefreshValue instead
285+
Object.entries(parameters).filter(([k, v]) =>
286+
this.parsedSettings.defaultValues[k] !== undefined
287+
&& v === this.parsedSettings.defaultValues[k]
288+
&& context.originalDesiredConfig?.[k] === undefined
289+
&& this.settings.importAndDestroy?.defaultRefreshValues?.[k] !== undefined
290+
).map(([k]) => [k, this.settings.importAndDestroy!.defaultRefreshValues![k]])
291+
))
284292
}
285293
: {
286294
...Object.fromEntries(

0 commit comments

Comments
 (0)