Skip to content

json marshal / unmarshal for hashid#29

Merged
stereosteve merged 3 commits intomainfrom
trashid_json_marshal
Apr 18, 2025
Merged

json marshal / unmarshal for hashid#29
stereosteve merged 3 commits intomainfrom
trashid_json_marshal

Conversation

@stereosteve
Copy link
Copy Markdown
Contributor

Yesterday @schottra had mentioned using custom JSON marshal / unmarshal functions for playlist library... and it occurred to me we could experiment with that for hash ID encoding.

Might make sense to view the commits in series:

First one does developer apps which shows that for the vanilla case we don't have to define Full struct variants to override an ID column. In developer apps case, the min response is identical to full response, so we can also skip that step.

The second one uses trashid.TrashId for id field on users... while keeping user_id an int in the response (for your debugging pleasure!). This is a bit trickier as it gets into how sqlc.yaml field mapping works... but not too bad.

My main concern with this is that it is all up in the intersection of sqlc + golang + json in ways that might be confusing / surprising to devs. Like if we define a struct type in jsonb_types.go for a jsonb column... if you try to use trashid.TrashId in that struct it'll go badly because the JSON coming from the database will be a number but trashid.TrashId will expect to decode a hashid... and it'll fail.

We could probably define other type variants that just have MarshalJSON and not UnmarshalJSON. Or we could add some code to try to handle this case in UnmarshalJSON.

Maybe the move here is to expand this branch to use this custom type everywhere and see if it's overall good or bad.

@stereosteve stereosteve changed the title experiment: json marshal / unmarshal for hashid json marshal / unmarshal for hashid Apr 18, 2025
@stereosteve stereosteve marked this pull request as ready for review April 18, 2025 20:38
@stereosteve stereosteve force-pushed the trashid_json_marshal branch from 50159c6 to f6c7496 Compare April 18, 2025 20:42
@stereosteve stereosteve merged commit 91a3103 into main Apr 18, 2025
2 checks passed
@stereosteve stereosteve deleted the trashid_json_marshal branch April 18, 2025 20:43
schottra added a commit that referenced this pull request Apr 18, 2025
* origin/main:
  feed: honor client's limit param.
  json marshal / unmarshal for hashid (#29)
  Explicitly proxy some ambigious routes. (#30)
  Fix trending time param (#28)
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

Successfully merging this pull request may close these issues.

1 participant