Url <-> struct conversion

I’m looking for a library or pair of libraries that handles conversion to and from some kind of URL & human friendly encoding for simple structs. Specifically I have query parameters I want to submit to an API and I don’t want to have to build the translation from struct to URL and URL back to struct by hand for each bit of data query config. Currently I’m sending URL-escaped JSON which works well enough for programmatically generated requests but is a pain if you want to build URLs by hand.

I’d like to make it easier without having to support multiple encoding formats.

I’m familiar with schema and the recently released form which handle processing path-based URL encoded data and storing it into a struct. I’ve also seen urlvalues that was put together to generate schema-compatible URLs from a struct but each of these are problematic or incomplete solutions.

  • schema doesn’t support nested slices as far as I can tell (bug) and doesn’t have any built-in approach to encoding structs into a format it can consume (feature request)
  • urlvalues is the basis for the solution for #44 above but based on my initial tests doesn’t handle structs or slices out of the box
  • form does great on the decode side but there doesn’t seem to be an encoding solution

If the answer is “write the features you need” that’s fine I just don’t want to duplicate that work if there is already a good solution or set of solutions out there and would appreciate any pointers.

cheers!

I was with you up until

where it starts sounding like I wouldn’t want to see these structs encoded as query parameters and consider it human readable. The original JSON idea sounds better to me. :slight_smile:

Hey @falun, nice to hear from you again

So you are just looking at traversing the Struct and generating the url.Values?

I was planning to add this to the form library at some point anyways, and it isn’t that bad, just reverse of the decode. It will take me a week or so as I have to update another library first if you can wait.

So you are just looking at traversing the Struct and generating the url.Values?

Exactly.

…and it isn’t that bad, just reverse of the decode. It will take me a week or so as I have to update another library first if you can wait.

Ah, neat. Yea, it’s not that the bit of code is difficult it’s more of a not wanting to reinvent the wheel situation. Depending on how much I actually need it we can race to a PR that adds it :stuck_out_tongue:

Sounds good to me :slight_smile:

OK, I WIN! @falun beat you to it :smile:

form - now has an Encoder as well.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.