Skip to content

Commit

Permalink
omit undefined values from cli (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro authored Jan 10, 2023
1 parent c9900be commit 9808c46
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
39 changes: 38 additions & 1 deletion src/common/utils/object/object.mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { expect } from "chai";
import { fromEntries, mapValues, omitFalsyValues } from "./object";
import { fromEntries, mapValues, omitFalsyValues, pickValues } from "./object";

describe("Object utils", () => {
describe("omitFalsyValues", () => {
Expand Down Expand Up @@ -73,6 +73,43 @@ describe("Object utils", () => {
});
});

describe("pickValues", () => {

const greaterThan10 = (n: number) => n > 10;

it("should left only values that pass predicate", () => {
const input: any = {
a: 9,
b: 10,
c: 11
};

const expected = {
c: 11
};

expect(pickValues(input, greaterThan10)).to.be.deep.equal(expected);
});

it("should handle empty object", () => {
expect(pickValues({}, greaterThan10)).to.be.deep.equal({});
});

it("should not modify input object", () => {
const input: any = {
a: 9,
b: 10,
c: 11
};

const inputCopy = Object.assign({}, input);

pickValues(input, greaterThan10);

expect(input).to.deep.equal(inputCopy);
});
});

describe("mapValues", () => {
function addAndStringify(num: number): string {
return String(num + 1);
Expand Down
19 changes: 12 additions & 7 deletions src/common/utils/object/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { assoc, Unary } from "../functional/functional";
import { assoc, Predicate, Unary } from "../functional/functional";
import { isTruthy } from "../general/general";

export function extend(source: any, target: any): any {
Expand All @@ -27,12 +27,7 @@ export function extend(source: any, target: any): any {
}

export function omitFalsyValues<T>(obj: T): Partial<T> {
return Object.keys(obj).reduce<Partial<T>>((res, key: keyof T & string) => {
if (isTruthy(obj[key])) {
res[key] = obj[key];
}
return res;
}, {});
return pickValues(obj, isTruthy);
}

type Key = string;
Expand All @@ -44,6 +39,16 @@ export function mapValues<K extends Key, S, T>(obj: Record<K, S>, fn: Unary<S, T
}, {} as Record<K, T>);
}

export function pickValues<T, K extends keyof T>(obj: T, predicate: Predicate<T[K]>): Partial<T> {
return (Object.keys(obj) as K[]).reduce((result: Partial<T>, key: K) => {
const value = obj[key];
if (predicate(value)) {
result[key] = value;
}
return result;
}, {} as Partial<T>);
}

export function fromEntries<K extends Key, T>(entries: Array<[K, T]>): Record<K, T> {
return entries.reduce((result: Record<K, T>, [key, value]: [K, T]) =>
assoc(result, key, value), {} as Record<K, T>);
Expand Down
6 changes: 5 additions & 1 deletion src/server/cli/build-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import { ClusterAuthJS } from "../../common/models/cluster-auth/cluster-auth";
import { fromConfig as clusterFromConfig } from "../../common/models/cluster/cluster";
import { fromConfig as dataCubeFromConfig } from "../../common/models/data-cube/data-cube";
import { fromConfig as sourcesFromConfig, Sources, SourcesJS } from "../../common/models/sources/sources";
import { complement } from "../../common/utils/functional/functional";
import { isNil } from "../../common/utils/general/general";
import { pickValues } from "../../common/utils/object/object";
import { LoggerFormat, ServerSettings, ServerSettingsJS } from "../models/server-settings/server-settings";
import { TurniloSettings } from "./run-turnilo";

Expand All @@ -45,10 +47,12 @@ function overrideClustersAuth(config: SourcesJS, auth: ClusterAuthJS): SourcesJS
}

export default function buildSettings(config: object, options: ServerOptions, auth?: ClusterAuthJS): TurniloSettings {
const definedOptions = pickValues(options, complement(isNil));
const serverSettingsJS: ServerSettingsJS = {
...config,
...options
...definedOptions
};

const serverSettings = ServerSettings.fromJS(serverSettingsJS);
const logger = getLogger(serverSettings.loggerFormat);
const appSettings = appSettingsFromConfig(config, logger);
Expand Down

0 comments on commit 9808c46

Please sign in to comment.