{"id":13411614,"url":"https://github.com/emvi/hide","last_synced_at":"2025-03-14T17:30:59.317Z","repository":{"id":57495363,"uuid":"166048649","full_name":"emvi/hide","owner":"emvi","description":"A Go type to prevent internal numeric IDs from being exposed to clients using HashIDs and JSON.","archived":false,"fork":false,"pushed_at":"2021-11-09T19:21:48.000Z","size":294,"stargazers_count":68,"open_issues_count":1,"forks_count":6,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-07-31T20:48:16.729Z","etag":null,"topics":["golang","id","json","type"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emvi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-16T13:54:17.000Z","updated_at":"2024-07-28T15:07:17.000Z","dependencies_parsed_at":"2022-08-31T12:52:00.369Z","dependency_job_id":null,"html_url":"https://github.com/emvi/hide","commit_stats":null,"previous_names":["emvicom/hide"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emvi%2Fhide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emvi%2Fhide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emvi%2Fhide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emvi%2Fhide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emvi","download_url":"https://codeload.github.com/emvi/hide/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618638,"owners_count":20320269,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","id","json","type"],"created_at":"2024-07-30T20:01:15.016Z","updated_at":"2025-03-14T17:30:58.923Z","avatar_url":"https://github.com/emvi.png","language":"Go","readme":"# Hide IDs\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/emvi/hide?status.svg)](https://pkg.go.dev/github.com/emvi/hide?status)\n[![Go Report Card](https://goreportcard.com/badge/github.com/emvi/hide)](https://goreportcard.com/report/github.com/emvi/hide)\n\u003ca href=\"https://discord.gg/fAYm4Cz\"\u003e\u003cimg src=\"https://img.shields.io/discord/739184135649886288?logo=discord\" alt=\"Chat on Discord\"\u003e\u003c/a\u003e\n\nHide is a simple package to provide an ID type that is marshaled to/from a hash string.\nThis prevents sending technical IDs to clients and converts them on the API layer.\nHide uses [hashids](https://github.com/speps/go-hashids) as its default hash function.\nBut you can provide your own by implementing the `Hash` interface and configuring it using `hide.UseHash`.\n\n[Read our full article on Medium.](https://medium.com/emvi/golang-transforming-ids-to-a-userfriendly-representation-in-web-applications-85bf2f7d71c5)\n\n## Installation\n\n```\ngo get github.com/emvi/hide/v2\n```\n\n## Example\n\nConsider the following struct:\n\n```\ntype User struct {\n    Id       int64  `json:\"id\"`\n    Username string `json:\"username\"`\n}\n```\n\nWhen marshaling this struct to JSON, the ID will be represented by a number:\n\n```\n{\n    \"id\": 123,\n    \"username\": \"foobar\"\n}\n```\n\nIn this case, you expose the technical user ID to your clients. By changing the type of the ID, you get a better result:\n\n```\ntype User struct {\n    Id       hide.ID `json:\"id\"`\n    Username string  `json:\"username\"`\n}\n```\n\nNotice that the `int64` ID got replaced by the `hide.ID`, which internally is represented as an `int64` as well, but implements the marshal interface.\nThis allows you to cast between them and use `hide.ID` as a replacement. The resulting JSON changes to the following:\n\n```\n{\n  \"id\": \"beJarVNaQM\",\n  \"username\": \"foobar\"\n}\n```\n\nIf 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.\nIt'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.\n\n[View the full demo](https://github.com/emvi/hide-example)\n\n## Contribute\n\n[See CONTRIBUTING.md](CONTRIBUTING.md)\n\n## License\n\nMIT\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"hidegopher.png\" width=\"300px\" /\u003e\n\u003c/p\u003e\n","funding_links":[],"categories":["Data Structures and Algorithms","数据结构","数据结构与算法","Uncategorized","Data Structures","Generators","Data Integration Frameworks","数据结构`go语言实现的数据结构与算法`","Go"],"sub_categories":["Miscellaneous Data Structures and Algorithms","标准 CLI","杂项数据结构和算法","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femvi%2Fhide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femvi%2Fhide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femvi%2Fhide/lists"}