-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Auto-parse booleans (true / false), nil, and numbers? #10
Comments
Here are some examples of conventions used in other libraries. yogthos/config takes the following approach for coercing environment variables:
tolitius/cprop does something similar for environment variables and system properties:
|
Thanks. I've looked into @yogthos's (defn str->value
"ENV vars and system properties are strings. str->value will convert:
the numbers to longs, the alphanumeric values to strings, and will use Clojure reader for the rest
in case reader can't read OR it reads a symbol, the value will be returned as is (a string)"
[v]
(cond
(re-matches #"[0-9]+" v) (parse-number v)
(re-matches #"^(true|false)$" v) (Boolean/parseBoolean v)
(re-matches #"\w+" v) v
:else
(try
(let [parsed (edn/read-string v)]
(if (symbol? parsed) v parsed))
(catch Throwable _ v)))) I think this could just be simplified as: always a read as in, but when the result is a symbol, return the original string and in case of an error too? |
Although:
I think in this case one would like to have |
I've put in place some automatic coercions now. Documented here: https://github.com/babashka/cli/blob/main/API.md#auto-coerce |
It could be reasonable behaviour to auto-parse
"false"
asfalse
and"nil"
asnil
. This would in some cases reduce the amount of:coerce
config to zero.Furthermore it could also be reasonable behavior to auto-parse
123
as a number.Anything else is still parsed as a string.
We should be able to disable the auto-parsing.
and/or in the future:
Could there be a case where you want to have a new
1foo
? This seems very unlikely. We could also revert to the string in case ofedn/read-string
failure.Would you have want to have
false
,true
? This seems unlikely and if so, you could coerce them to strings, if that is desired.The text was updated successfully, but these errors were encountered: