Rest Partial Response (aka Field Selection) Pattern middleware for Gin. 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 an user with the fields, id, name, surname, age, address
:
/users/1
{
"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
{
"name": "John",
"surname": "Doe"
}
- Run the command:
go get -u -d github.com/manuelarte/milogo
- Add milogo middleware
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
/users/1?fields=name,surname
{
"name": "John",
"surname": "Doe"
}
/users?fields=name
[
{
"name": "John"
}
]
/users/1?fields=name,surname,address(street,zipcode)
{
"name": "John",
"surname": "Doe",
"address": {
"street": "mystreet",
"zipcode": "myzipcode"
}
}
/users/1?fields=name
{
"data": {
"name": "John"
}
}
Milogo middleware, as any other gin middleware, can be applied to different route groups with different configurations.
Feel free to create a PR or suggest improvements or ideas.