Skip to content

Commit

Permalink
Add mergeOptions method
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Jan 11, 2019
1 parent 8b27e81 commit 939c04c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
33 changes: 29 additions & 4 deletions lib/api/goal/GoalWithFulfillment.ts
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { configurationValue } from "@atomist/automation-client";
import * as _ from "lodash";
import { LogSuppressor } from "../../api-helper/log/logInterpreters";
import { AbstractSoftwareDeliveryMachine } from "../../api-helper/machine/AbstractSoftwareDeliveryMachine";
import { InterpretLog } from "../../spi/log/InterpretedLog";
Expand Down Expand Up @@ -169,10 +171,10 @@ export abstract class FulfillableGoal extends GoalWithPrecondition implements Re

public withExecutionListener(listener: GoalExecutionListener): this {
const wrappedListener = async gi => {
if (gi.goalEvent.uniqueName === this.uniqueName) {
return listener(gi);
}
};
if (gi.goalEvent.uniqueName === this.uniqueName) {
return listener(gi);
}
};
if (this.sdm) {
this.sdm.addGoalExecutionListener(wrappedListener);
}
Expand Down Expand Up @@ -312,6 +314,12 @@ export function goal(details: FulfillableGoalDetails = {},
return g;
}

/**
* Construct a PredicatedGoalDefinition from the provided goalDetails
* @param goalDetails
* @param uniqueName
* @param definition
*/
// tslint:disable:cyclomatic-complexity
export function getGoalDefinitionFrom(goalDetails: FulfillableGoalDetails | string,
uniqueName: string,
Expand Down Expand Up @@ -352,6 +360,23 @@ export function getGoalDefinitionFrom(goalDetails: FulfillableGoalDetails | stri
}
}

/**
* Merge Goal configuration options into a final options object.
* Starts off by merging the explicitly provided options over the provided defaults; finally merges the configuration
* values at the given configuration path (prefixed with sdm.) over the previous merge.
* @param defaults
* @param explicit
* @param configurationPath
*/
export function mergeOptions<OPTIONS>(defaults: OPTIONS, explicit: OPTIONS, configurationPath?: string): OPTIONS {
const options: OPTIONS = _.merge(defaults, explicit || {});
if (!!configurationPath) {
const configurationOptions = configurationValue<OPTIONS>(`sdm.${configurationPath}`, {} as any);
return _.merge(options, configurationOptions);
}
return options;
}

function getEnvironment(details?: { environment?: string | GoalEnvironment }): GoalEnvironment {
if (details && details.environment && typeof details.environment === "string") {
switch (details.environment) {
Expand Down
19 changes: 18 additions & 1 deletion test/api/goal/GoalWithFulfillment.test.ts
Expand Up @@ -15,7 +15,10 @@
*/

import * as assert from "power-assert";
import { goal } from "../../../lib/api/goal/GoalWithFulfillment";
import {
goal,
mergeOptions,
} from "../../../lib/api/goal/GoalWithFulfillment";
import { IndependentOfEnvironment } from "../../../lib/api/goal/support/environment";

describe("GoalWithFulfillment", () => {
Expand All @@ -31,4 +34,18 @@ describe("GoalWithFulfillment", () => {

});

describe("mergeOptions", () => {

it("should merge defaults with explicit values", () => {
const opts = mergeOptions({ foo: { bar: 1 } }, { foo: { bar: 2, bla: 3 } });
assert.deepStrictEqual(opts, { foo: { bar: 2, bla: 3 } });
});

it("should merge defaults with explicit values with partial overwrite", () => {
const opts = mergeOptions({ foo: { bar: 1 } }, { foo: { bla: 3 } });
assert.deepStrictEqual(opts, { foo: { bar: 1, bla: 3 } });
});

});

});

0 comments on commit 939c04c

Please sign in to comment.