Popularity
2.0
Growing
Activity
0.0
Declining
14
3
2

Programming language: Go
Tags: Data Structures    
Latest version: v1.1.1

hide alternatives and similar packages

Based on the "Data Structures" category

Do you think we are missing an alternative of hide or a related project?

Add another 'Data Structures' Package

README

Hide IDs

GoDoc CircleCI Go Report Card

Hide is a simple package to provide an ID type that is marshalled to/from a hash string. This prevents sending technical IDs to clients and converts them on the API layer. Hide uses hashids as its default hash function. But you can provide your own by implementing the Hash interface and configure it using hide.UseHash.

Read our full article on Medium.

Installation

go get github.com/emvi/hide

Example

Consider the following struct:

type User struct {
    Id       int64  `json:"id"`
    Username string `json:"username"`
}

When marshalling 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 marshalled to JSON or stored in database.

View the full demo

Contribute

[See CONTRIBUTING.md](CONTRIBUTING.md)

License

MIT


*Note that all licence references and agreements mentioned in the hide README section above are relevant to that project's source code only.