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

Non-numeric values are parsed as numbers #27

Closed
luontola opened this issue Jun 27, 2019 · 8 comments
Closed

Non-numeric values are parsed as numbers #27

luontola opened this issue Jun 27, 2019 · 8 comments

Comments

@luontola
Copy link

Given an environment variable with the value 7 Nov 22:44:53 2015, then cprop.source/read-system-env accidentally parses it as the number 7 instead of keeping it as a string.

@tolitius
Copy link
Owner

tolitius commented Jun 27, 2019

ah... interesting. thanks for reporting it.

this is due to the non "EDN formatted thing":

=> (require '[clojure.edn :as edn])

=> (edn/read-string "7 Nov 22:44:53 2015")
7

also:

boot.user=> (edn/read-string "7Nov 22:44:53 2015")
java.lang.NumberFormatException: Invalid number: 7Nov

I was wondering for some time now whether to have an option param where it would treat/read/overwrite all ENV vars as strings for cases like this.

If you have other ideas, let's talk :)

@tolitius
Copy link
Owner

other things to consider is to:

  • optionally take in custom regex and a function to parse it
  • take in custom *data-readers*

@luontola
Copy link
Author

luontola commented Jun 27, 2019

I was able to work around this by changing the parameters to be formatted 2015-12-27T17:32:00Z, which makes edn/read-string throw an exception but cprop.source/str->value has a catch for that, so the value is returned unmodified.

I suppose wrapping the value in double quotes ("7 Nov 22:44:53 2015") would also work by making it valid EDN, but since my variables are in a YAML file, it'll need double double quotes: SOME_YAML_VAR: "\"7 Nov 22:44:53 2015\""

@luontola
Copy link
Author

An option for disabling EDN parsing would be nice. I don't use that feature anyways.

@tolitius
Copy link
Owner

I added :as-is? optional param to source (sysprops, propfile and env related) functions which will read props as is (i.e. as strings):

$ export FOO='"4242"'
$ export BAR=4242
$ export DATE='7 Nov 22:44:53 2015'
$ export VEC='[1 2 3 4]'
[cprop]$ boot dev
=> (require '[cprop.source :as s])

=> (:foo (s/from-env))
"4242"
=> (:bar (s/from-env))
4242
=> (:date (s/from-env))
7
=> (:vec (s/from-env))
[1 2 3 4]

but

=> (:foo (s/from-env {:as-is? true}))
"\"4242\""
=> (:bar (s/from-env {:as-is? true}))
"4242"
=> (:date (s/from-env {:as-is? true}))
"7 Nov 22:44:53 2015"
=> (:vec (s/from-env {:as-is? true}))
"[1 2 3 4]"

it should be in cprop-0.1.14-SNAPSHOT. let me know if it works for you.

@tolitius
Copy link
Owner

tolitius commented Jun 27, 2019

also can be used at the top level: (load-config :as-is? true)

@luontola
Copy link
Author

luontola commented Jul 2, 2019

Thanks. :as-is? works well.

@tolitius
Copy link
Owner

tolitius commented Jul 3, 2019

great, thanks for the feedback. it is in cprop-0.1.14 (release version).

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

No branches or pull requests

2 participants