Skip to content

Commit

Permalink
Implement hidden property for Cfgu declaration with ExportCommand `…
Browse files Browse the repository at this point in the history
…filter` callback (#379)
  • Loading branch information
RonConfigu committed Mar 10, 2024
1 parent 7844cda commit 02ce5e2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
12 changes: 11 additions & 1 deletion ts/packages/ts/src/commands/ExportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ExportCommandReturn = {
export type ExportCommandParameters = {
pipe: EvalCommandReturn;
keys?: (key: string) => string;
filter?: ({ context, result }: EvalCommandReturn['string']) => boolean;
};

export class ExportCommand extends Command<ExportCommandReturn> {
Expand Down Expand Up @@ -37,9 +38,18 @@ export class ExportCommand extends Command<ExportCommandReturn> {
});
}

private filterPipe(pipe: EvalCommandReturn) {
const { filter } = this.parameters;
if (!filter) {
return _.pickBy(pipe, ({ context }) => !context.cfgu.hidden);
}
return _.pickBy(pipe, filter);
}

async run() {
const { pipe } = this.parameters;
const configDict = _.mapValues(pipe, (current) => current.result.value);
const filteredPipe = this.filterPipe(pipe);
const configDict = _.mapValues(filteredPipe, (current) => current.result.value);
const keyMutatedConfigDict = this.mutateKeys(configDict);
return keyMutatedConfigDict;
}
Expand Down
41 changes: 33 additions & 8 deletions ts/packages/ts/src/commands/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,24 +377,49 @@ describe(`commands`, () => {
});
});
describe(`ExportCommand`, () => {
const getEvalResult = async () => {
const getEvalResult = async (schema?: ConfigSchema) => {
return new EvalCommand({
store: store1,
set: set1,
schema: new ConfigSchema('mutate', {
schema:
schema ||
new ConfigSchema('mutate', {
KEY0: {
type: 'String',
},
KEY1: {
type: 'String',
},
}),
configs: {
KEY0: 'KEY0',
KEY1: 'KEY1',
},
}).run();
};
test('Export with filter', async () => {
const evalResult = await getEvalResult();
const exportedConfigs = await new ExportCommand({
pipe: evalResult,
filter: ({ context, result }) => result.value !== 'KEY0',
}).run();
expect(exportedConfigs).toStrictEqual({ KEY1: 'KEY1' });
});
test('Export with hidden configs', async () => {
const evalResult = await getEvalResult(
new ConfigSchema('mutate', {
KEY0: {
type: 'String',
},
KEY1: {
type: 'String',
hidden: true,
},
}),
configs: {
KEY0: 'KEY0',
KEY1: 'KEY1',
},
}).run();
};
);
const exportedConfigs = await new ExportCommand({ pipe: evalResult }).run();
expect(exportedConfigs).toStrictEqual({ KEY0: 'KEY0' });
});
describe(`Keys Mutation Callback`, () => {
test('Export without keys mutation callback', async () => {
const evalResult = await getEvalResult();
Expand Down
4 changes: 4 additions & 0 deletions ts/packages/ts/src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Cfgu {
default?: string;
depends?: string[];
description?: string;
hidden?: boolean;
labels?: string[];
lazy?: boolean;
options?: string[];
Expand Down Expand Up @@ -63,6 +64,7 @@ export interface ConfigSchemaContents {
default?: string;
depends?: string[];
description?: string;
hidden?: boolean;
labels?: string[];
lazy?: boolean;
options?: string[];
Expand Down Expand Up @@ -351,6 +353,7 @@ const typeMap: any = {
{ json: "default", js: "default", typ: u(undefined, "") },
{ json: "depends", js: "depends", typ: u(undefined, a("")) },
{ json: "description", js: "description", typ: u(undefined, "") },
{ json: "hidden", js: "hidden", typ: u(undefined, true) },
{ json: "labels", js: "labels", typ: u(undefined, a("")) },
{ json: "lazy", js: "lazy", typ: u(undefined, true) },
{ json: "options", js: "options", typ: u(undefined, a("")) },
Expand All @@ -373,6 +376,7 @@ const typeMap: any = {
{ json: "default", js: "default", typ: u(undefined, "") },
{ json: "depends", js: "depends", typ: u(undefined, a("")) },
{ json: "description", js: "description", typ: u(undefined, "") },
{ json: "hidden", js: "hidden", typ: u(undefined, true) },
{ json: "labels", js: "labels", typ: u(undefined, a("")) },
{ json: "lazy", js: "lazy", typ: u(undefined, true) },
{ json: "options", js: "options", typ: u(undefined, a("")) },
Expand Down
1 change: 1 addition & 0 deletions types/Cfgu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface Cfgu {
options?: string[];
lazy?: boolean;
labels?: string[];
hidden?: boolean;
}

0 comments on commit 02ce5e2

Please sign in to comment.