Merge pull request #1364 from spotify/rugvip/optconf

packages/config: add getOptionalConfig and getOptionalConfigArray to mirror other accessors
This commit is contained in:
Patrik Oldsberg
2020-06-18 11:30:45 +02:00
committed by GitHub
3 changed files with 33 additions and 10 deletions
+8 -4
View File
@@ -46,10 +46,11 @@ function expectValidValues(config: ConfigReader) {
expect(config.getStringArray('strings')).toEqual(['string1', 'string2']);
expect(config.getConfig('nested').getNumber('one')).toBe(1);
expect(config.getConfig('nested').getString('string')).toBe('string');
expect(config.getConfig('nested').getStringArray('strings')).toEqual([
'string1',
'string2',
]);
expect(
config.getOptionalConfig('nested')!.getStringArray('strings'),
).toEqual(['string1', 'string2']);
expect(config.getOptionalConfig('missing')).toBe(undefined);
expect(config.getOptionalConfigArray('missing')).toBe(undefined);
expect(config.getNumber('zero')).toBe(0);
expect(config.getBoolean('true')).toBe(true);
expect(config.getString('string')).toBe('string');
@@ -59,6 +60,9 @@ function expectValidValues(config: ConfigReader) {
expect(config1.getBoolean('boolean')).toBe(true);
expect(config2.getString('string')).toBe('string');
expect(config3.getNumber('number')).toBe(42);
expect(
config.getOptionalConfigArray('nestlings')![0].getBoolean('boolean'),
).toBe(true);
}
function expectInvalidValues(config: ConfigReader) {
+23 -6
View File
@@ -79,8 +79,16 @@ export class ConfigReader implements Config {
}
getConfig(key: string): ConfigReader {
const value = this.getOptionalConfig(key);
if (value === undefined) {
throw new Error(errors.missing(this.fullKey(key)));
}
return value;
}
getOptionalConfig(key: string): ConfigReader | undefined {
const value = this.readValue(key);
const fallbackConfig = this.fallback?.getConfig(key);
const fallbackConfig = this.fallback?.getOptionalConfig(key);
const prefix = this.fullKey(key);
if (isObject(value)) {
@@ -91,13 +99,18 @@ export class ConfigReader implements Config {
errors.type(this.fullKey(key), this.context, typeOf(value), 'object'),
);
}
return (
fallbackConfig ??
new ConfigReader(undefined, undefined, undefined, prefix)
);
return fallbackConfig;
}
getConfigArray(key: string): ConfigReader[] {
const value = this.getOptionalConfigArray(key);
if (value === undefined) {
throw new Error(errors.missing(this.fullKey(key)));
}
return value;
}
getOptionalConfigArray(key: string): ConfigReader[] | undefined {
const configs = this.readConfigValue<JsonObject[]>(key, values => {
if (!Array.isArray(values)) {
return { expected: 'object-array' };
@@ -111,7 +124,11 @@ export class ConfigReader implements Config {
return true;
});
return (configs ?? []).map(
if (!configs) {
return undefined;
}
return configs.map(
(obj, index) =>
new ConfigReader(
obj,
+2
View File
@@ -33,8 +33,10 @@ export type Config = {
keys(): string[];
getConfig(key: string): Config;
getOptionalConfig(key: string): Config | undefined;
getConfigArray(key: string): Config[];
getOptionalConfigArray(key: string): Config[] | undefined;
getNumber(key: string): number;
getOptionalNumber(key: string): number | undefined;