Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configuration key handling #3855

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@ import { Provider } from 'nconf';
/**
* Configuration implements the [IConfiguration](xref:botbuilder-dialogs-adaptive-runtime-core.IConfiguration)
* interface and adds helper methods for setting values, layering sources, and getting type checked values.
*
* @internal
*/
export class Configuration implements CoreConfiguration {
private prefix: string[] = [];
private provider = new Provider().use('memory');

/**
* Create a configuration instance
*
* @param initialValues Optional set of default values to provide
*/
constructor(initialValues?: Record<string, unknown>) {
if (initialValues) {
Object.entries(initialValues).forEach(([key, value]) => this.provider.set(key, value));
}
}

/**
* Bind a path to a Configuration instance such that calls to get or set will
* automatically include the bound path as a prefix.
Expand Down Expand Up @@ -41,7 +54,9 @@ export class Configuration implements CoreConfiguration {
* @returns the value, or undefined
*/
get<T = unknown>(path: string[] = []): T | undefined {
return this.provider.get(this.key(path));
// Note: `|| undefined` ensures that empty string is coerced to undefined
// which ensures nconf returns the entire merged configuration.
return this.provider.get(this.key(path) || undefined);
}

/**
Expand All @@ -51,6 +66,10 @@ export class Configuration implements CoreConfiguration {
* @param value value to set
*/
set(path: string[], value: unknown): void {
if (!path.length) {
throw new Error('`path` must be non-empty');
}

this.provider.set(this.key(path), value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,53 @@ describe('Configuration', function () {
assert.strictEqual(layered.get(['root', 'key']), 'layer');
});
});

describe('keys', function () {
const configuration = new Configuration({
key: 'value',
one: {
key: 'value-one',
two: {
key: 'value-two',
},
},
});

describe('non-prefixed', function () {
it('yields a value for a key', function () {
assert.strictEqual(configuration.get(['key']), 'value');
assert.strictEqual(configuration.get(['one', 'key']), 'value-one');
});

it('yields all values for a key', function () {
assert.deepStrictEqual(configuration.get(['one']), { key: 'value-one', two: { key: 'value-two' } });
});

it('yields all values for no key', function () {
assert.deepStrictEqual(configuration.get(), {
key: 'value',
one: { key: 'value-one', two: { key: 'value-two' } },
});
});
});

describe('prefixed', function () {
it('yields a value for a key', function () {
assert.strictEqual(configuration.bind(['one']).get(['key']), 'value-one');
});

it('yields all values for a key', function () {
assert.deepStrictEqual(configuration.bind(['one']).get(['two']), {
key: 'value-two',
});
});

it('yields all values for no key', function () {
assert.deepStrictEqual(configuration.bind(['one']).get(), {
key: 'value-one',
two: { key: 'value-two' },
});
});
});
});
});