Commit a7e42939 by Vladislav Lagunov

Исправлен парсинг конфига из командной строки

parent 351874db
......@@ -4,7 +4,6 @@ import { absurd } from '../types';
import { WithDefault, RecordDecoder, Decoder } from '../decoder';
import { Either } from '../either';
/**
* Источник данных для чтения конфигов
*/
......@@ -14,45 +13,40 @@ export type ConfigSource =
| { tag: 'JSON', value: object }
| { tag: 'Concat', sources: ConfigSource[] }
export function cli(args: string[], prefix: string = ''): ConfigSource {
return { tag: 'Cli', args, prefix };
}
export function queryString(value: string, transformKey = camelCase): ConfigSource {
return { tag: 'Location/search', value, transformKey };
}
export function json(value: object): ConfigSource {
return { tag: 'JSON', value };
}
export function concat(...sources: ConfigSource[]): ConfigSource {
return { tag: 'Concat', sources };
}
export function validate<A>(config: ConfigSource, decoder: RecordDecoder<A>) {
return decoder.validate(merge(decoder, config));
}
export function merge<A>(decoder: RecordDecoder<A>, ...srcs: ConfigSource[]): object {
const source = srcs.length === 1 ? srcs[0] : concat(...srcs);
debugger;
if (source.tag === 'Cli') {
const value: Record<string, any> = {};
const output: Record<string, any> = {};
for (let i = 0; i < source.args.length; i++) {
const prefixLen = '--'.length + source.prefix.length;
if (!source.args[i].startsWith('--' + source.prefix)) continue;
const rest = source.args[i].substr(prefixLen);
const [k, v] = rest.indexOf('=') === -1 ? [rest, source.args[++i] || ''] : source.args[i].split('=');
// Проверка того что это известно
if (!decoder._description.hasOwnProperty(k)) continue;
value[k] = fromString(decoder._description[k], v);
const [key, value] = rest.indexOf('=') === -1 ? [rest, source.args[++i] || ''] : rest.split('=');
if (!decoder._description.hasOwnProperty(key)) continue;
output[key] = fromString(decoder._description[key], value);
}
return value;
return output;
}
if (source.tag === 'Location/search') {
......@@ -153,7 +147,6 @@ export function fromString(d: Decoder<any>, value: string): unknown {
return absurd(d);
}
export class ConfigDescription<A> extends t.ToDecoder<A> {
constructor(
readonly _decoder: Decoder<A>,
......@@ -168,7 +161,6 @@ export class ConfigDescription<A> extends t.ToDecoder<A> {
// Информация о cli параметрe
export type InfoItem = { type: string; default?: unknown; description?: string };
export function configInfo(decoder: RecordDecoder<any>, prefix=''): Record<string, InfoItem> {
return Object.keys(decoder._description).reduce((acc, k) => (acc[k] = go(decoder._description[k]), acc), {});
......@@ -247,7 +239,6 @@ export function configInfo(decoder: RecordDecoder<any>, prefix=''): Record<strin
}
}
declare module "../decoder" {
interface DecoderBase<A> {
withDescription(description: string): Decoder<A>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment