Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
npm
common
Commits
4c6c4a99
Commit
4c6c4a99
authored
Jan 15, 2019
by
Vladislav Lagunov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Переименования в decoder/index.ts
- Рефакторинг config-reader/index.ts
parent
974e1d48
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
46 deletions
+158
-46
config-reader/index.ts
+158
-46
decoder/index.ts
+0
-0
No files found.
config-reader/index.ts
View file @
4c6c4a99
import
*
as
t
from
'../decoder'
;
import
*
as
t
from
'../decoder'
;
import
{
camelCase
}
from
'lodash'
;
import
{
camelCase
}
from
'lodash'
;
import
{
absurd
}
from
'../types'
;
import
{
absurd
}
from
'../types'
;
import
{
WithDefault
,
RecordDecoder
,
Decoder
}
from
'../decoder'
;
import
{
Either
}
from
'../either'
;
/**
/**
...
@@ -33,12 +35,12 @@ export function concat(...sources: ConfigSource[]): ConfigSource {
...
@@ -33,12 +35,12 @@ export function concat(...sources: ConfigSource[]): ConfigSource {
}
}
export
function
validate
<
A
>
(
config
:
ConfigSource
,
decoder
:
t
.
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
:
t
.
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
);
if
(
source
.
tag
===
'Cli'
)
{
if
(
source
.
tag
===
'Cli'
)
{
const
value
:
Record
<
string
,
any
>
=
{};
const
value
:
Record
<
string
,
any
>
=
{};
...
@@ -47,8 +49,8 @@ export function merge<A>(decoder: t.RecordDecoder<A>, ...srcs: ConfigSource[]):
...
@@ -47,8 +49,8 @@ export function merge<A>(decoder: t.RecordDecoder<A>, ...srcs: ConfigSource[]):
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
[
k
,
v
]
=
rest
.
indexOf
(
'='
)
===
-
1
?
[
rest
,
source
.
args
[
++
i
]
||
''
]
:
source
.
args
[
i
].
split
(
'='
);
// Проверка того что это известно
// Проверка того что это известно
if
(
!
decoder
.
description
.
hasOwnProperty
(
k
))
continue
;
if
(
!
decoder
.
_
description
.
hasOwnProperty
(
k
))
continue
;
value
[
k
]
=
decoderFromString
(
decoder
.
description
[
k
],
v
);
value
[
k
]
=
fromString
(
decoder
.
_
description
[
k
],
v
);
}
}
return
value
;
return
value
;
}
}
...
@@ -57,8 +59,8 @@ export function merge<A>(decoder: t.RecordDecoder<A>, ...srcs: ConfigSource[]):
...
@@ -57,8 +59,8 @@ export function merge<A>(decoder: t.RecordDecoder<A>, ...srcs: ConfigSource[]):
const
substring
=
source
.
value
[
0
]
===
'?'
?
source
.
value
.
substr
(
1
)
:
source
.
value
;
const
substring
=
source
.
value
[
0
]
===
'?'
?
source
.
value
.
substr
(
1
)
:
source
.
value
;
const
value
=
substring
.
split
(
'&'
).
map
(
x
=>
x
.
split
(
'='
)).
reduce
((
acc
,
[
k
,
v
]:
any
)
=>
{
const
value
=
substring
.
split
(
'&'
).
map
(
x
=>
x
.
split
(
'='
)).
reduce
((
acc
,
[
k
,
v
]:
any
)
=>
{
const
key
=
source
.
transformKey
(
decodeURIComponent
(
k
));
const
key
=
source
.
transformKey
(
decodeURIComponent
(
k
));
if
(
!
decoder
.
description
.
hasOwnProperty
(
key
))
return
acc
;
if
(
!
decoder
.
_
description
.
hasOwnProperty
(
key
))
return
acc
;
acc
[
key
]
=
decoderFromString
(
decoder
.
description
[
key
],
decodeURIComponent
(
v
));
acc
[
key
]
=
fromString
(
decoder
.
_
description
[
key
],
decodeURIComponent
(
v
));
return
acc
;
return
acc
;
},
{});
},
{});
return
value
;
return
value
;
...
@@ -77,16 +79,21 @@ export function merge<A>(decoder: t.RecordDecoder<A>, ...srcs: ConfigSource[]):
...
@@ -77,16 +79,21 @@ export function merge<A>(decoder: t.RecordDecoder<A>, ...srcs: ConfigSource[]):
}
}
export
function
decoderFromString
(
decoder
:
t
.
Decoder
<
any
>
,
value
:
string
):
any
{
export
function
fromString
(
d
:
Decoder
<
any
>
,
value
:
string
):
unknown
{
if
(
d
ecoder
instanceof
t
.
Custom
Decoder
)
{
if
(
d
instanceof
t
.
Array
Decoder
)
{
throw
new
Error
(
'[config-reader] CustomDecoder is not supported'
);
return
value
.
split
(
', '
).
map
(
x
=>
fromString
(
d
.
_decoder
,
x
)
);
}
}
if
(
decoder
instanceof
t
.
ArrayDecoder
)
{
// if (d instanceof t.Tuple) {
return
value
.
split
(
', '
).
map
(
x
=>
decoderFromString
(
decoder
.
decoder
,
x
));
// return value.split(', ').map((x, idx) => fromString(d._tuple[idx], x));
}
// }
// if (d instanceof t.Literals) {
// // TODO: проверять значение исходя из типа rep._literals
// return value;
// }
if
(
d
ecoder
instanceof
t
.
DictionaryDecoder
)
{
if
(
d
instanceof
t
.
Dict
)
{
try
{
try
{
return
JSON
.
parse
(
value
);
return
JSON
.
parse
(
value
);
}
catch
(
e
)
{
}
catch
(
e
)
{
...
@@ -94,61 +101,166 @@ export function decoderFromString(decoder: t.Decoder<any>, value: string): any {
...
@@ -94,61 +101,166 @@ export function decoderFromString(decoder: t.Decoder<any>, value: string): any {
}
}
}
}
if
(
d
ecoder
instanceof
t
.
RecordDecoder
)
{
if
(
d
instanceof
t
.
RecordDecoder
)
{
try
{
try
{
return
JSON
.
parse
(
value
);
return
JSON
.
parse
(
value
);
}
catch
(
e
)
{
}
catch
(
e
)
{
return
value
;
return
value
;
}
}
}
}
if
(
d
instanceof
t
.
Primitive
)
{
if
(
d
.
_type
===
'string'
)
return
value
;
if
(
d
.
_type
===
'boolean'
)
return
!
(
value
===
'false'
||
value
===
'0'
);
if
(
d
.
_type
===
'undefined'
)
return
undefined
;
if
(
d
.
_type
===
'null'
)
return
null
;
if
(
d
.
_type
===
'any'
)
return
value
;
if
(
d
.
_type
===
'nat'
)
return
Number
(
value
);
if
(
d
.
_type
===
'int'
)
return
Number
(
value
);
if
(
d
.
_type
===
'float'
)
return
Number
(
value
);
return
absurd
(
d
.
_type
);
}
if
(
d
instanceof
t
.
OneOf
)
{
throw
new
Error
(
'[config-reader] OneOf is not supported'
);
}
if
(
d
ecoder
instanceof
t
.
AtDecoder
)
{
if
(
d
instanceof
t
.
Discriminate
)
{
try
{
try
{
return
JSON
.
parse
(
value
);
return
JSON
.
parse
(
value
);
}
catch
(
e
)
{
}
catch
(
e
)
{
return
value
;
return
value
;
}
}
}
}
if
(
decoder
instanceof
t
.
PrimitiveDecoder
)
{
if
(
decoder
.
primitive
===
'string'
)
return
value
;
if
(
decoder
.
primitive
===
'boolean'
)
return
!
(
value
===
'false'
||
value
===
'0'
);
if
(
decoder
.
primitive
===
'undefined'
)
return
undefined
;
if
(
decoder
.
primitive
===
'null'
)
return
null
;
if
(
decoder
.
primitive
===
'any'
)
return
value
;
if
(
decoder
.
primitive
===
'nat'
)
return
Number
(
value
);
if
(
decoder
.
primitive
===
'int'
)
return
Number
(
value
);
if
(
decoder
.
primitive
===
'float'
)
return
Number
(
value
);
return
absurd
(
decoder
.
primitive
);
}
if
(
d
ecoder
instanceof
t
.
PureDecoder
)
{
if
(
d
instanceof
t
.
Pure
)
{
return
value
;
return
value
;
}
}
if
(
decoder
instanceof
t
.
ChainDecoder
)
{
if
(
d
instanceof
t
.
ToDecoder
)
{
throw
new
Error
(
'[config-reader] ChainDecoder is not supported'
);
if
(
d
instanceof
WithDefault
)
{
return
fromString
(
d
.
_decoder
,
value
);
}
return
fromString
(
d
.
toDecoder
(),
value
);
}
}
if
(
d
ecoder
instanceof
t
.
OneOfDecoder
)
{
if
(
d
instanceof
t
.
Custom
)
{
throw
new
Error
(
'[config-reader]
OneOf
Decoder is not supported'
);
throw
new
Error
(
'[config-reader]
: Custom
Decoder is not supported'
);
}
}
if
(
d
ecoder
instanceof
t
.
Discriminated
Decoder
)
{
if
(
d
instanceof
t
.
At
Decoder
)
{
t
ry
{
t
hrow
new
Error
(
'[config-reader]: AtDecoder is not supported'
);
return
JSON
.
parse
(
value
);
}
}
catch
(
e
)
{
return
value
;
if
(
d
instanceof
t
.
Chain
)
{
}
throw
new
Error
(
'[config-reader]: ChainDecoder is not supported'
);
}
}
if
(
decoder
instanceof
t
.
HasDecoder
)
{
return
absurd
(
d
);
if
(
decoder
instanceof
t
.
WithDefaultDecoder
)
{
}
return
decoderFromString
(
decoder
.
decoder
,
value
);
export
class
ConfigDescription
<
A
>
extends
t
.
ToDecoder
<
A
>
{
constructor
(
readonly
_decoder
:
Decoder
<
A
>
,
readonly
_description
:
string
,
)
{
super
();
}
toDecoder
()
{
return
this
.
_decoder
;
}
}
// Информация о 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
),
{});
function
go
(
d
:
Decoder
<
any
>
):
InfoItem
{
if
(
d
instanceof
t
.
ArrayDecoder
)
{
const
{
type
}
=
go
(
d
.
_decoder
);
return
{
type
:
'Array<'
+
type
+
'>'
};
}
if
(
d
instanceof
t
.
Dict
)
{
const
{
type
}
=
go
(
d
.
_decoder
);
return
{
type
:
'Record<string, '
+
type
+
'>'
};
}
if
(
d
instanceof
t
.
RecordDecoder
)
{
// TODO: печать полей
return
{
type
:
'Record<string, unknown>'
};
}
if
(
d
instanceof
t
.
Primitive
)
{
if
(
d
.
_type
===
'string'
)
return
{
type
:
'string'
};
if
(
d
.
_type
===
'boolean'
)
return
{
type
:
'boolean'
};
if
(
d
.
_type
===
'undefined'
)
return
{
type
:
'undefined'
};
if
(
d
.
_type
===
'null'
)
return
{
type
:
'null'
};
if
(
d
.
_type
===
'any'
)
return
{
type
:
'any'
};
if
(
d
.
_type
===
'nat'
)
return
{
type
:
'number'
};
if
(
d
.
_type
===
'int'
)
return
{
type
:
'number'
};
if
(
d
.
_type
===
'float'
)
return
{
type
:
'number'
};
return
absurd
(
d
.
_type
);
}
if
(
d
instanceof
t
.
OneOf
)
{
return
{
type
:
d
.
_alternatives
.
map
(
go
).
map
(
x
=>
x
.
type
).
join
(
'|'
)
};
}
}
return
decoderFromString
(
decoder
.
toDecoder
(),
value
);
if
(
d
instanceof
t
.
Discriminate
)
{
// TODO: печать дискриминаторов
return
{
type
:
Object
.
keys
(
d
.
_alternatives
).
map
(
k
=>
go
(
d
.
_alternatives
[
k
]).
type
).
join
(
'|'
)
};
}
if
(
d
instanceof
t
.
Pure
)
{
return
{
type
:
JSON
.
stringify
(
d
.
_value
)
};
}
if
(
d
instanceof
t
.
ToDecoder
)
{
if
(
d
instanceof
WithDefault
)
{
const
info
=
go
(
d
.
_decoder
);
info
.
default
=
d
.
_default
;
return
info
;
}
const
info
=
go
(
d
.
toDecoder
());
if
(
d
instanceof
ConfigDescription
)
{
info
.
description
=
d
.
_description
;
}
return
info
;
}
if
(
d
instanceof
t
.
Custom
)
{
throw
new
Error
(
'[config-reader]: CustomDecoder is not supported'
);
}
if
(
d
instanceof
t
.
AtDecoder
)
{
throw
new
Error
(
'[config-reader]: AtDecoder is not supported'
);
}
if
(
d
instanceof
t
.
Chain
)
{
throw
new
Error
(
'[config-reader]: ChainDecoder is not supported'
);
}
return
absurd
(
d
);
}
}
}
return
absurd
(
decoder
);
declare
module
"../decoder"
{
interface
DecoderBase
<
A
>
{
withDescription
(
description
:
string
):
Decoder
<
A
>
;
validateConfigs
(...
sources
:
ConfigSource
[]):
Either
<
Problem
,
A
>
;
}
}
}
t
.
DecoderBase
.
prototype
.
withDescription
=
function
(
description
)
{
return
new
ConfigDescription
(
this
as
any
,
description
);
};
t
.
DecoderBase
.
prototype
.
validateConfigs
=
function
(...
sources
)
{
return
this
.
validate
(
merge
(
this
as
any
,
...
sources
));
};
decoder/index.ts
View file @
4c6c4a99
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment