Skip to content

✂️ Cut your JSON Rest API responses using a field query parameter (JSON Partial response plugin for Gin).

License

Notifications You must be signed in to change notification settings

manuelarte/milogo

Repository files navigation

✂️ Milogo ✂️

Go coverage Go Report Card version

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.

logo

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"
}

📝 How To Install It And Use It

  • 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

✨ Features

/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.

🤝 Contributing

Feel free to create a PR or suggest improvements or ideas.

About

✂️ Cut your JSON Rest API responses using a field query parameter (JSON Partial response plugin for Gin).

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors 2

  •  
  •