Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to 'automatically' convert values to types not native to YAML #8

Closed
akaIDIOT opened this issue Dec 3, 2017 · 4 comments
Closed
Labels

Comments

@akaIDIOT
Copy link
Member

akaIDIOT commented Dec 3, 2017

Either at construction time or at value retrieval time, some values may need conversion. Examples include date(time)s and binary blobs. Converters are easy to do (strptime / b64decode?), the way to attach / register them and where to apply them requires some thinking.

@akaIDIOT
Copy link
Member Author

akaIDIOT commented Dec 9, 2017

This also goes for values read from environment variables, these can only be strings. Can we determine a 'safe' way to auto-convert values like 'true' to a bool and '12345' to an int?

@akaIDIOT
Copy link
Member Author

TOML (config language, used in proposed Pipfile thingy) seems to be using pattern matching, if a string matches an RFC 3339 date, it'll be a datetime instance. We could adopt a similar approach, or use YAML typing, which puts a strain on the user to explicitly type it as such.

@akaIDIOT
Copy link
Member Author

akaIDIOT commented Feb 1, 2018

Interestingly, pyyaml seems to do this partially already:

# date / datetime (seems to ignore tz hints, sadly)
yaml.load('test: 2001-01-02')
{'test': datetime.date(2001, 1, 2)}
yaml.load('test: 2001-01-02 12:34:00')
{'test': datetime.datetime(2001, 1, 2, 12, 34)}
yaml.load('test: !!timestamp 2001-01-02 12:34:00 +1')

# binary data is a tad more involved
yaml.load('test: YmluYXJ5IGRhdGE=')
{'test': 'YmluYXJ5IGRhdGE='}
yaml.load('test: !!binary YmluYXJ5IGRhdGE=')
{'test': b'binary data'}

http://yaml.org/type/ lists a number of builtin tags that might be quite useful for this.

yaml/pyyaml#113 proposes support for timezone-aware timestamps.

@akaIDIOT
Copy link
Member Author

Issue above has been merged in the meantime 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant