Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ing-bank/gohateoas
Plug-and-play HATEOAS for REST API's written in Go
https://github.com/ing-bank/gohateoas
api go go-hateoas hateoas json level-3 marshal marshalling maturity model rest rest-api richardson
Last synced: 5 days ago
JSON representation
Plug-and-play HATEOAS for REST API's written in Go
- Host: GitHub
- URL: https://github.com/ing-bank/gohateoas
- Owner: ing-bank
- License: mit
- Created: 2022-11-18T10:20:33.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-07T16:12:28.000Z (6 months ago)
- Last Synced: 2024-06-19T17:50:58.135Z (5 months ago)
- Topics: api, go, go-hateoas, hateoas, json, level-3, marshal, marshalling, maturity, model, rest, rest-api, richardson
- Language: Go
- Homepage: https://pkg.go.dev/github.com/ing-bank/gohateoas
- Size: 53.7 KB
- Stars: 28
- Watchers: 9
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🦁 Golang Hateoas
[![Go package](https://github.com/ing-bank/gohateoas/actions/workflows/test.yaml/badge.svg)](https://github.com/ing-bank/gohateoas/actions/workflows/test.yaml)
![GitHub](https://img.shields.io/github/license/ing-bank/gohateoas)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/ing-bank/gohateoas)Hateoas isn't always necessary for a REST API to function, but it's definitely a nice-to-have.
This library provides a simple way to add hateoas links to your API responses, regardless
of the wrapper object you use.## ⬇️ Installation
`go get github.com/ing-bank/gohateoas`
## 📋 Usage
```go
package mainimport (
"fmt"
"github.com/ing-bank/gohateoas"
"encoding/json"
)// APIResponse is a simple struct that will be used as a wrapper for our response.
type APIResponse struct {
Data any `json:"data"`
}// MarshalJSON overrides the usual json.Marshal behaviour to allow us to add links to the response
func (a APIResponse) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Data json.RawMessage `json:"data"`
}{
Data: gohateoas.InjectLinks(gohateoas.DefaultLinkRegistry, a.Data),
})
}type Cupcake struct{}
func main() {
gohateoas.Register(Cupcake{}, gohateoas.Self("/api/v1/cupcakes/{id}", "Get this cupcake"),
gohateoas.Post("/api/v1/cupcakes", "Create a cupcake"),
gohateoas.Patch("/api/v1/cupcakes/{id}", "Partially update a cupcake"),
gohateoas.Delete("/api/v1/cupcakes?id={id}", "Delete this cupcake"))response := APIResponse{Data: Cupcake{}}
jsonBytes, _ := json.Marshal(response)
fmt.Println(string(jsonBytes))
}```
## 🚀 Development
1. Clone the repository
2. Run `make t` to run unit tests
3. Run `make fmt` to format codeYou can run `make` to see a list of useful commands.
## 🔭 Future Plans
- [ ] Add support for custom link-property name