https://github.com/emvi/hide
A Go type to prevent internal numeric IDs from being exposed to clients using HashIDs and JSON.
https://github.com/emvi/hide
golang id json type
Last synced: 12 months ago
JSON representation
A Go type to prevent internal numeric IDs from being exposed to clients using HashIDs and JSON.
- Host: GitHub
- URL: https://github.com/emvi/hide
- Owner: emvi
- License: mit
- Created: 2019-01-16T13:54:17.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-09T19:21:48.000Z (over 4 years ago)
- Last Synced: 2024-07-31T20:48:16.729Z (over 1 year ago)
- Topics: golang, id, json, type
- Language: Go
- Homepage:
- Size: 287 KB
- Stars: 68
- Watchers: 5
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go-cn - hide
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures / Advanced Console UIs)
- fucking-awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures / Advanced Console UIs)
- go-awesome-with-star-updatetime - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures / Advanced Console UIs)
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go-extra - hide - 01-16T13:54:17Z|2021-11-09T19:21:48Z| (Generators / Miscellaneous Data Structures and Algorithms)
- awesome-go-cn - hide
- awesome-go - hide - | - | - | (Data Structures / Advanced Console UIs)
- awesome-go-plus - hide - ID type with marshalling to/from hash to prevent sending IDs to clients.  (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
- awesome-go-with-stars - hide - 11-09 | (Data Integration Frameworks / Miscellaneous Data Structures and Algorithms)
- awesome-Char - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures / Advanced Console UIs)
- awesome-ccamel - emvi/hide - A Go type to prevent internal numeric IDs from being exposed to clients using HashIDs and JSON. (Go)
- awesome-go-cn - hide
- awesome-go-info - hide
- awesome-go - hide - ID type with marshalling to/from hash to prevent sending IDs to clients. (Data Structures and Algorithms / Miscellaneous Data Structures and Algorithms)
README
# Hide IDs
[](https://pkg.go.dev/github.com/emvi/hide?status)
[](https://goreportcard.com/report/github.com/emvi/hide)

Hide is a simple package to provide an ID type that is marshaled to/from a hash string.
This prevents sending technical IDs to clients and converts them on the API layer.
Hide uses [hashids](https://github.com/speps/go-hashids) as its default hash function.
But you can provide your own by implementing the `Hash` interface and configuring it using `hide.UseHash`.
[Read our full article on Medium.](https://medium.com/emvi/golang-transforming-ids-to-a-userfriendly-representation-in-web-applications-85bf2f7d71c5)
## Installation
```
go get github.com/emvi/hide/v2
```
## Example
Consider the following struct:
```
type User struct {
Id int64 `json:"id"`
Username string `json:"username"`
}
```
When marshaling this struct to JSON, the ID will be represented by a number:
```
{
"id": 123,
"username": "foobar"
}
```
In this case, you expose the technical user ID to your clients. By changing the type of the ID, you get a better result:
```
type User struct {
Id hide.ID `json:"id"`
Username string `json:"username"`
}
```
Notice that the `int64` ID got replaced by the `hide.ID`, which internally is represented as an `int64` as well, but implements the marshal interface.
This allows you to cast between them and use `hide.ID` as a replacement. The resulting JSON changes to the following:
```
{
"id": "beJarVNaQM",
"username": "foobar"
}
```
If you send the new ID (which is a string now) back to the server and unmarshal it into the `hide.ID` type, you'll get the original technical ID back.
It's also worth mentioning that a value of 0 is translated to `null` when an ID is marshaled to JSON or stored in a database.
[View the full demo](https://github.com/emvi/hide-example)
## Contribute
[See CONTRIBUTING.md](CONTRIBUTING.md)
## License
MIT