packages/config: allow config readers to be backed by undefined data

This commit is contained in:
Patrik Oldsberg
2020-06-17 17:28:26 +02:00
parent 3dcef70e3c
commit c9ee24b8bb
2 changed files with 18 additions and 7 deletions
+6
View File
@@ -118,6 +118,8 @@ describe('ConfigReader', () => {
expect(config.getOptionalString('X-x2')).toBeUndefined();
expect(config.getOptionalString('x0_x0')).toBeUndefined();
expect(config.getOptionalString('x_x-x_x')).toBeUndefined();
expect(new ConfigReader(undefined).getOptionalString('x')).toBeUndefined();
});
it('should throw on invalid keys', () => {
@@ -138,6 +140,10 @@ describe('ConfigReader', () => {
expect(() => config.getString('a.a.a.a.')).toThrow(/^Invalid config key/);
expect(() => config.getString('a._')).toThrow(/^Invalid config key/);
expect(() => config.getString('a.-.a')).toThrow(/^Invalid config key/);
expect(() => new ConfigReader(undefined).getString('.')).toThrow(
/^Invalid config key/,
);
});
it('should read valid values', () => {
+12 -7
View File
@@ -40,11 +40,9 @@ function typeOf(value: JsonValue | undefined): string {
}
export class ConfigReader implements Config {
private static readonly nullReader = new ConfigReader({});
static fromConfigs(configs: AppConfig[]): ConfigReader {
if (configs.length === 0) {
return new ConfigReader({});
return new ConfigReader(undefined);
}
// Merge together all configs info a single config with recursive fallback
@@ -55,13 +53,14 @@ export class ConfigReader implements Config {
}
constructor(
private readonly data: JsonObject,
private readonly data: JsonObject | undefined,
private readonly fallback?: ConfigReader,
) {}
getConfig(key: string): ConfigReader {
const value = this.readValue(key);
const fallbackConfig = this.fallback?.getConfig(key);
if (isObject(value)) {
return new ConfigReader(value, fallbackConfig);
}
@@ -72,7 +71,7 @@ export class ConfigReader implements Config {
)}, wanted object`,
);
}
return fallbackConfig ?? ConfigReader.nullReader;
return fallbackConfig ?? new ConfigReader(undefined, undefined);
}
getConfigArray(key: string): ConfigReader[] {
@@ -191,12 +190,18 @@ export class ConfigReader implements Config {
private readValue(key: string): JsonValue | undefined {
const parts = key.split('.');
let value: JsonValue | undefined = this.data;
for (const part of parts) {
if (!CONFIG_KEY_PART_PATTERN.test(part)) {
throw new TypeError(`Invalid config key '${key}'`);
}
}
if (this.data === undefined) {
return undefined;
}
let value: JsonValue | undefined = this.data;
for (const part of parts) {
if (isObject(value)) {
value = value[part];
} else {