config-loader: rename top-level source data key to configs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-03-31 12:04:48 +02:00
parent aaee88b0af
commit 83aae42439
12 changed files with 41 additions and 36 deletions
+3 -3
View File
@@ -120,13 +120,13 @@ export async function loadConfig(
const abortController = new AbortController();
options.watch?.stopSignal?.then(() => abortController.abort());
for await (const { data } of source.readConfigData({
for await (const { configs } of source.readConfigData({
signal: abortController.signal,
})) {
if (loaded) {
options.watch?.onChange(data);
options.watch?.onChange(configs);
} else {
resolve({ appConfigs: data });
resolve({ appConfigs: configs });
loaded = true;
if (options.watch) {
@@ -240,14 +240,14 @@ export class ConfigSources {
let config: ObservableConfigProxy | undefined = undefined;
try {
const abortController = new AbortController();
for await (const { data } of source.readConfigData({
for await (const { configs } of source.readConfigData({
signal: abortController.signal,
})) {
if (config) {
config.setConfig(ConfigReader.fromConfigs(data));
config.setConfig(ConfigReader.fromConfigs(configs));
} else {
config = ObservableConfigProxy.create(abortController);
config!.setConfig(ConfigReader.fromConfigs(data));
config!.setConfig(ConfigReader.fromConfigs(configs));
resolve(config);
}
}
@@ -17,7 +17,7 @@
import { AppConfig } from '@backstage/config';
import { assertError } from '@backstage/errors';
import { JsonObject } from '@backstage/types';
import { ConfigSource, ConfigSourceData } from './types';
import { AsyncConfigSourceIterator, ConfigSource } from './types';
export class EnvConfigSource implements ConfigSource {
static create(options: {
@@ -32,9 +32,9 @@ export class EnvConfigSource implements ConfigSource {
private readonly env: { [name: string]: string | undefined },
) {}
async *readConfigData(): AsyncIterableIterator<{ data: ConfigSourceData[] }> {
const data = readEnvConfig(this.env);
yield { data };
async *readConfigData(): AsyncConfigSourceIterator {
const configs = readEnvConfig(this.env);
yield { configs };
return;
}
}
@@ -103,14 +103,14 @@ export class FileConfigSource implements ConfigSource {
watcher.close();
});
yield { data: await readConfigFile() };
yield { configs: await readConfigFile() };
for (;;) {
const event = await this.#waitForEvent(watcher, signal);
if (event === 'abort') {
return;
}
yield { data: await readConfigFile() };
yield { configs: await readConfigFile() };
}
}
@@ -112,22 +112,22 @@ describe('MergedConfigSource', () => {
);
const sourceA: ConfigSource = {
async *readConfigData() {
yield { data: [] };
yield { configs: [] };
},
};
const sourceD: ConfigSource = {
async *readConfigData() {
yield { data: [] };
yield { configs: [] };
},
};
const sourceB: ConfigSource = {
async *readConfigData() {
yield { data: [] };
yield { configs: [] };
},
};
const sourceC: ConfigSource = {
async *readConfigData() {
yield { data: [] };
yield { configs: [] };
},
};
@@ -49,20 +49,23 @@ export class MergedConfigSource implements ConfigSource {
): AsyncConfigSourceIterator {
const its = this.sources.map(source => source.readConfigData(options));
const initialResults = await Promise.all(its.map(it => it.next()));
const data = initialResults.map((result, i) => {
const configs = initialResults.map((result, i) => {
if (result.done) {
throw new Error(
`Config source ${String(this.sources[i])} returned no data`,
);
}
return result.value.data;
return result.value.configs;
});
yield { data: data.flat(1) };
yield { configs: configs.flat(1) };
const results: Array<
| Promise<
readonly [number, IteratorResult<{ data: ConfigSourceData[] }, void>]
readonly [
number,
IteratorResult<{ configs: ConfigSourceData[] }, void>,
]
>
| undefined
> = its.map((it, i) => nextWithIndex(it, i));
@@ -74,8 +77,8 @@ export class MergedConfigSource implements ConfigSource {
results[i] = undefined;
} else {
results[i] = nextWithIndex(its[i], i);
data[i] = result.value.data;
yield { data: data.flat(1) };
configs[i] = result.value.configs;
yield { configs: configs.flat(1) };
}
} catch (error) {
const source = this.sources[error.index];
@@ -107,7 +107,7 @@ describe('MutableConfigSource', () => {
const source = MutableConfigSource.create({ data: { a: 1 } });
const resultsPromise = readAll(source);
for await (const { data } of source.readConfigData()) {
for await (const { configs: data } of source.readConfigData()) {
const a = data[0].data.a as number;
if (a < 3) {
source.setData({ a: a + 1 });
@@ -50,7 +50,7 @@ export class MutableConfigSource implements ConfigSource {
let deferredPromise = this.#deferred.promise;
if (this.#currentData !== undefined) {
yield { data: [{ data: this.#currentData, context: this.#context }] };
yield { configs: [{ data: this.#currentData, context: this.#context }] };
}
for (;;) {
@@ -64,7 +64,9 @@ export class MutableConfigSource implements ConfigSource {
deferredPromise = this.#deferred.promise;
if (this.#currentData !== undefined) {
yield { data: [{ data: this.#currentData, context: this.#context }] };
yield {
configs: [{ data: this.#currentData, context: this.#context }],
};
}
}
}
@@ -66,7 +66,7 @@ export class RemoteConfigSource implements ConfigSource {
): AsyncConfigSourceIterator {
let data = await this.#load();
yield { data: [{ data, context: this.#url }] };
yield { configs: [{ data, context: this.#url }] };
for (;;) {
if (options?.signal?.aborted) {
@@ -78,7 +78,7 @@ export class RemoteConfigSource implements ConfigSource {
const newData = await this.#load(options?.signal);
if (newData && !isEqual(data, newData)) {
data = newData;
yield { data: [{ data, context: this.#url }] };
yield { configs: [{ data, context: this.#url }] };
}
} catch (error) {
console.error(`Failed to read config from ${this.#url}, ${error}`);
@@ -67,7 +67,7 @@ class StaticObservableConfigSource implements ConfigSource {
return;
}
while (queue.length > 0) {
yield { data: [{ data: queue.shift()!, context: this.context }] };
yield { configs: [{ data: queue.shift()!, context: this.context }] };
}
}
}
@@ -87,7 +87,7 @@ export class StaticConfigSource implements ConfigSource {
if (!data) {
return {
async *readConfigData(): AsyncConfigSourceIterator {
yield { data: [] };
yield { configs: [] };
return;
},
};
@@ -104,7 +104,7 @@ export class StaticConfigSource implements ConfigSource {
return {
async *readConfigData(): AsyncConfigSourceIterator {
for await (const value of data) {
yield { data: [{ data: value, context }] };
yield { configs: [{ data: value, context }] };
}
},
};
@@ -119,7 +119,7 @@ export class StaticConfigSource implements ConfigSource {
) {}
async *readConfigData(): AsyncConfigSourceIterator {
yield { data: [{ data: await this.promise, context: this.context }] };
yield { configs: [{ data: await this.promise, context: this.context }] };
return;
}
}
@@ -41,7 +41,7 @@ export async function readAll(
const results: ConfigSourceData[][] = [];
try {
for await (const { data } of source.readConfigData({ signal })) {
for await (const { configs: data } of source.readConfigData({ signal })) {
results.push(data);
}
} catch (error) {
@@ -58,7 +58,7 @@ export function simpleSource(
return {
async *readConfigData() {
for (const d of data) {
yield { data: [{ data: d, context }] };
yield { configs: [{ data: d, context }] };
}
},
};
+3 -3
View File
@@ -43,9 +43,9 @@ export interface ReadConfigDataOptions {
* @public
*/
export interface AsyncConfigSourceIterator
extends AsyncIterator<{ data: ConfigSourceData[] }, void, void> {
extends AsyncIterator<{ configs: ConfigSourceData[] }, void, void> {
[Symbol.asyncIterator](): AsyncIterator<
{ data: ConfigSourceData[] },
{ configs: ConfigSourceData[] },
void,
void
>;
@@ -64,7 +64,7 @@ export interface AsyncConfigSourceIterator
* class MyConfigSource implements ConfigSource {
* async *readConfigData() {
* yield {
* data: [{
* config: [{
* context: 'example',
* data: { backend: { baseUrl: 'http://localhost' } }
* }]