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 a product with the fields, code, price, description, manufacturedBy
:
/products/1
{
"code": "1",
"price": "200",
"description": "Very nice product",
"manufacturedBy": "company"
}
We can call the endpoint and, with the query parameter fields, filter out the fields that we are interested:
/products/1?fields=code,price
{
"code": "1",
"price": "200"
}
- 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/manuel?fields=name,surname
{
"name": "Manuel",
"surname": "Example"
}
/users?fields=name
[
{
"name": "Manuel"
}
]
/users/manuel?fields=name,surname,address(street,zipcode)
{
"name": "Manuel",
"surname": "Example",
"address": {
"street": "mystreet",
"zipcode": "myzipcode"
}
}
/users/manuel?fields=name
{
"data": {
"name": "Manuel"
}
}
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.