Add lists converter
Current State
There are already a few ways how cs helps to convert untyped values to typed ones. For example when loading ini-files or environment variables there are no typing information. For that a user can specify converters, so that he gets already typed values when he access a key.
At the moment configstacker supports
- bools
- dates
- datetimes
Now when a key in such a source has multiple values, one has to specify a custom converter to deal with lists.
import configstacker as cs
# assuming an untyped source
source_data = {'a': '1,2,3'}
# custom converters
def to_list(value):
tokens = value.split(',')
return [int(t) for t in token]
def to_str(value):
return ','.join([str(v) for v in value])
cfg = cs.DictSource(
source_data,
converters=[('a', to_list, to_str)]
)
assert cfg.a == [1, 2, 3]
Proposal
As list conversion is quite predictable it would be nice to also provide a built-in list converter. That way one can focus on converting the individual elements of the list.
import configstacker as cs
# assuming an untyped source
source_data = {'a': '1,2,3'}
cfg = cs.DictSource(
source_data,
converters=[cs.converters.lists('a', int, str)]
)
assert cfg.a == [1, 2, 3]