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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoder/decoder for percent-encoding (URL/URI encoding) #1529

Closed
hhromic opened this issue Jan 19, 2023 · 5 comments
Closed

Encoder/decoder for percent-encoding (URL/URI encoding) #1529

hhromic opened this issue Jan 19, 2023 · 5 comments

Comments

@hhromic
Copy link

hhromic commented Jan 19, 2023

Please describe your feature request.
(( First of all, thank you very much for developing yq. This is really a great swiss army knife for YAML! 馃挴 ))

It would be quite handy for yq to support percent-encoding (aka URL/URI encoding) as an encoder/decoder, similar to the existing @base64 encoder/decoder pair. For instance, jq supports this as a string formatter:

$ jq -n '{key:@uri "uri=\("some/value")"}'
{
  "key": "uri=some%2Fvalue"
}

This is specially handy when constructing URLs from dynamic values. A hypothetical example:

$ DB_PASSWORD='super/secret' yq -n '.dbconfig.dsn="mysql://user:" + (env(DB_PASSWORD) | @uri) + "@host:port/dbname"'
dbconfig:
  dsn: mysql://user:super%2Fsecret@host:port/dbname

Describe alternatives you've considered
Within yq, it doesn't seem to be possible to perform percent-encoding. Using other tools, jq supports encoding like shown above, therefore the current workaround is to use jq to encode the value and then pass it to yq to insert into a YAML document.

@hhromic
Copy link
Author

hhromic commented Jan 19, 2023

Looks like Go has built-in batteries to encode/decode percent-encoding in net/url :)

@mikefarah
Copy link
Owner

Nice find - should be pretty easy to add that in 馃憤馃徏

mikefarah added a commit that referenced this issue Jan 23, 2023
@hhromic
Copy link
Author

hhromic commented Jan 23, 2023

Wow, nice you already implemented this! 馃挴
Thanks a lot! I will give it a try as soon as this addition is released!

@mikefarah
Copy link
Owner

Released in v4.31.1 :)

@hhromic
Copy link
Author

hhromic commented Feb 20, 2023

Just tested the released binary:

$ DB_PASSWORD='super/secret' yq -n '.dbconfig.dsn="mysql://user:" + (env(DB_PASSWORD) | @uri) + "@host:port/dbname"'
dbconfig:
  dsn: mysql://user:super%2Fsecret@host:port/dbname

Works exactly like proposed! :)
Thanks!

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

No branches or pull requests

2 participants