https://github.com/manuelarte/milogo
JSON Partial response (aka field selection) plugin for Gin.
https://github.com/manuelarte/milogo
field-selection gin-gonic go golang middleware partial-responses rest-api
Last synced: about 2 months ago
JSON representation
JSON Partial response (aka field selection) plugin for Gin.
- Host: GitHub
- URL: https://github.com/manuelarte/milogo
- Owner: manuelarte
- License: mit
- Created: 2024-11-10T23:51:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-11T09:38:21.000Z (over 1 year ago)
- Last Synced: 2025-01-11T10:31:08.546Z (over 1 year ago)
- Topics: field-selection, gin-gonic, go, golang, middleware, partial-responses, rest-api
- Language: Go
- Homepage:
- Size: 48.8 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# ✂️ Milogo ✂️
[](https://github.com/manuelarte/milogo/actions/workflows/go.yml)

[](https://goreportcard.com/report/github.com/manuelarte/milogo)

Rest Partial Response (aka Field Selection) Pattern middleware for [Gin](https://gin-gonic.com/).
This gin middleware allows you to select a subset of fields to be returned from your endpoints.

e.g., Imagine that you have the following rest endpoint that returns a user with the fields, `id, name, surname, age, address`:
> /users/1
```json
{
"id": 1,
"name": "John",
"surname": "Doe",
"age": 18,
"address": {
"street": "mystreet",
"city": "mycity",
"country": "mycountry",
"zipcode": "1111"
}
}
```
We can call the endpoint and, with the query parameter fields, filter out the fields that we are interested:
> /users/1?**fields=name,surname**
```json
{
"name": "John",
"surname": "Doe"
}
```
## 📝 How To Install It And Use It
- Run the command:
> go get -u -d github.com/manuelarte/milogo
- Add milogo middleware
```go
r := gin.Default()
r.Use(Milogo())
```
- Call your endpoints adding the query parameter `fields` with the fields you want to filter:
> /users/1?**fields=name,surname**
## ✨ Features
- [Support for multiple fields filtering](./examples/simple).
> /users/1?fields=name,surname
```json
{
"name": "John",
"surname": "Doe"
}
```
- [Support for arrays](./examples/simple-array)
> /users?fields=name
```json
[
{
"name": "John"
}
]
```
- [Support for nested jsons](./examples/nested).
> /users/1?fields=name,surname,address(street,zipcode)
```json
{
"name": "John",
"surname": "Doe",
"address": {
"street": "mystreet",
"zipcode": "myzipcode"
}
}
```
- [Support for wrapped json](./examples/wrapped).
> /users/1?fields=name
```json
{
"data": {
"name": "John"
}
}
```
- [Middleware applied to route groups with different configuration](./example/routeGroups)
Milogo middleware, as any other gin middleware, can be applied to different route groups with different configurations.
## 🤝 Contributing
Feel free to create a PR or suggest improvements or ideas.