Gin-Gonic Middleware that implements fields selection pattern
At my company we use Go to build internal tools. Recently I worked in a REST API using gin-gonic, that required displaying a lot of data across many endpoints.
One must have feature in this type of scenario, is pagination, but and often overlooked pattern is partial response (a.k.a field selection). Which is also a very nice addition to filter out the amount of data in the responses of your web server.
What’s field selection?
Let us first make it clear what I mean with a field selection. Imagine that you have the following endpoint:
// > GET /api/products
[
{
"id": 1,
"createdAt": "2024-18-11",
"updatedAt": "2024-18-11",
"code": "1",
"price": {
"amount": 100,
"currency": "EUR"
},
"manufacturedBy": "myshop",
"stock": 552,
...
},
...
]
Using partial responses, clients can filter the output with a fields query parameter, e.g.
// > GET /api/products?fields=code,price
[
{
"code": "1",
"price": {
"amount": 100,
"currency": "EUR"
}
},
...
]
Only the fields declared in the query parameter are returned. Reducing the payload size and saving bandwidth.
Introducing Milogo
I could not find any implementation available for this pattern, so I decided to create it myself, and that is how Milogo was born.
Milogo is a Gin middleware that processes API responses, filters out fields specified in the fields query parameter, and return only the requested data.
Some of the main features available:
Support for json objects and json arrays.
So Milogo can filter fields for JSON responses that starts with an array of items or just with one single item.Support for filtering out fields in nested json objects.
Milogo also supports filtering nested JSON objects with the following format e.g. code,price(amount)Support for json wrapped in another json.
Sometimes the JSON responses are wrapped in another JSON object, Milogo support filtering the actual payload (see example wrapped):
// GET /products?fields=code,price(amount)
{
"data": [
{
"code": 1,
"price": {
"amount": 100
}
},
...
],
"_metadata": {
...
}
}
Getting Started
As any gin middleware, Milogo is really easy to use and setup, you can follow the README in the github repository, but basically:
r := gin.Default()
r.Use(Milogo())
is enough to add Milogo middleware to your api.
In the previous example Milogo is applied to every single endpoint, but it is also possible to apply only to a group of endpoints, or just to an specific endpoint, e.g.
group := r.Group("/products", milogo.Milogo())
group.GET("", productsHandler)
In the previous example, only the /products group would have the Milogo’s middleware applied.
Conclusion
Milogo is designed to simplify REST API development and improve client-server interactions. Check out the GitHub repository for examples and documentation.
Feel free to reach out or contribute — let’s make REST APIs efficient together!
Top comments (0)