diff --git a/packages/config/src/reader.ts b/packages/config/src/reader.ts index bc8a860c85..0947f8c8ab 100644 --- a/packages/config/src/reader.ts +++ b/packages/config/src/reader.ts @@ -17,6 +17,7 @@ import { JsonValue, JsonObject } from '@backstage/types'; import { AppConfig, Config } from './types'; import cloneDeep from 'lodash/cloneDeep'; +import merge from 'lodash/merge'; import mergeWith from 'lodash/mergeWith'; // Update the same pattern in config-loader package if this is changed @@ -118,6 +119,28 @@ export class ConfigReader implements Config { return value as T; } + getMap() { + const map: Record = {}; + + const flatten = (data: JsonValue, path = '') => { + if (isObject(data)) { + Object.entries(data).forEach(([key, value]: [string, any]) => + flatten(value, `${path}${path ? '.' : ''}${key}`), + ); + } else if (Array.isArray(data)) { + data.forEach((value, key) => { + flatten(value, `${path}[${key}]`); + }); + } else { + map[path] = data; + } + }; + + flatten(merge({}, this.fallback?.data, this.data)); + + return map; + } + getOptional(key?: string): T | undefined { const value = this.readValue(key); const fallbackValue = this.fallback?.getOptional(key); diff --git a/packages/config/src/types.ts b/packages/config/src/types.ts index a543233277..628178379e 100644 --- a/packages/config/src/types.ts +++ b/packages/config/src/types.ts @@ -65,6 +65,12 @@ export type Config = { */ keys(): string[]; + /** + * Returns a flattened map of the config with the full path to the keys and + * the config value as the value. + */ + getMap(): Record; + /** * Same as `getOptional`, but will throw an error if there's no value for the given key. */