Merge pull request #1350 from spotify/rugvip/confkeys

packages/config: added .keys()
This commit is contained in:
Patrik Oldsberg
2020-06-18 10:36:14 +02:00
committed by GitHub
3 changed files with 21 additions and 0 deletions
+13
View File
@@ -37,6 +37,7 @@ const DATA = {
};
function expectValidValues(config: ConfigReader) {
expect(config.keys()).toEqual(Object.keys(DATA));
expect(config.getNumber('zero')).toBe(0);
expect(config.getNumber('one')).toBe(1);
expect(config.getBoolean('true')).toBe(true);
@@ -111,6 +112,7 @@ function expectInvalidValues(config: ConfigReader) {
describe('ConfigReader', () => {
it('should read empty config with valid keys', () => {
const config = new ConfigReader({});
expect(config.keys()).toEqual([]);
expect(config.getOptionalString('x')).toBeUndefined();
expect(config.getOptionalString('x_x')).toBeUndefined();
expect(config.getOptionalString('x-X')).toBeUndefined();
@@ -208,6 +210,17 @@ describe('ConfigReader with fallback', () => {
const config = new ConfigReader(a, new ConfigReader(b));
expect(config.keys()).toEqual(['merged']);
expect(config.getConfig('merged').keys()).toEqual([
'x',
'z',
'arr',
'config',
'configs',
'y',
]);
expect(config.getConfig('merged.config').keys()).toEqual(['d', 'e']);
expect(config.getString('merged.x')).toBe('x');
expect(config.getString('merged.y')).toBe('y');
expect(config.getString('merged.z')).toBe('z1');
+6
View File
@@ -68,6 +68,12 @@ export class ConfigReader implements Config {
private readonly prefix: string = '',
) {}
keys(): string[] {
const localKeys = this.data ? Object.keys(this.data) : [];
const fallbackKeys = this.fallback?.keys() ?? [];
return [...new Set([...localKeys, ...fallbackKeys])];
}
getConfig(key: string): ConfigReader {
const value = this.readValue(key);
const fallbackConfig = this.fallback?.getConfig(key);
+2
View File
@@ -27,6 +27,8 @@ export type JsonValue =
export type AppConfig = JsonObject;
export type Config = {
keys(): string[];
getConfig(key: string): Config;
getConfigArray(key: string): Config[];