stackbuilders / dotenv-hs

Load environment variables from dotenv files for Haskell
https://hackage.haskell.org/package/dotenv
MIT License
65 stars 14 forks source link

Parse content that starts with curly brackets #179

Closed CristhianMotoche closed 11 months ago

CristhianMotoche commented 1 year ago

In #169 was reported that JSON content cannot be parsed. The workaround was to warp the content in quotes but that causes some inconveniences since we need to escape inner quotes. Also, due to #178 we cannot do variable substitution. Therefore, we should be able to parse content that starts with curly brackets. For example:

Given:

$ cat .env
JSON={"a":[1,2,3], "b": "FOO BAR BAZ"}

Should not produce:

dotenv: .env
   |
2 |  JSON={"a":[1,2,3], "b": "FOO BAR BAZ"}
   |

unexpected '"'
expecting ${, ...
CristhianMotoche commented 1 year ago

After some review, I noticed the issue is not related to the starting curly bracket { but due to the double quote " after it. It seems dotenv parser allows only escaped quotes after the first character. The following will be parsed correctly:

# .env
JSON={\"a\":[1,2,3],\"b\":\"FOO\ BAR\ BAZ\"}

NOTE that it also needs to escape spaces. I need to review a little bit more the reasons of this initial decision before updating the parser.

CristhianMotoche commented 11 months ago

After some thought, I think I prefer to keep the behaviour of dotenv since it seems the correct one. If a use wants to set a JSON value in an env variable they can wrap the value in quotes like it is mentioned in the README. Therefore, I'm closing this issue now.