Commit a7e42939 by Vladislav Lagunov

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

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