https://github.com/golaxo/gofieldselect
Apply field selection pattern to your REST apis.
https://github.com/golaxo/gofieldselect
fields go golang odata query-parameters rest rest-api restful-api select
Last synced: 3 months ago
JSON representation
Apply field selection pattern to your REST apis.
- Host: GitHub
- URL: https://github.com/golaxo/gofieldselect
- Owner: golaxo
- License: mit
- Created: 2025-12-12T13:35:56.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-12-16T13:28:42.000Z (4 months ago)
- Last Synced: 2025-12-18T09:54:45.156Z (4 months ago)
- Topics: fields, go, golang, odata, query-parameters, rest, rest-api, restful-api, select
- Language: Go
- Homepage:
- Size: 32.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Field Select
[](https://img.shields.io/github/v/release/golaxo/gofieldselect)
[](https://github.com/golaxo/gofieldselect/actions/workflows/ci.yml)
[](https://github.com/golaxo/gofieldselect/actions/workflows/pr-checks.yml)
[](https://goreportcard.com/report/github.com/golaxo/gofieldselect)
> [!WARNING]
> GoFieldSelect is under heavy development.
A powerful `select` query parameter implementation based on [Field selection][field-selection] for filtering desired fields in a REST API.
## ⬇️ Getting Started
To start using it:
```bash
go get github.com/golaxo/gofieldselect@latest
```
And then use it either by
### Using Get for each field
```go
n, err := gofieldselect.Parse("name,address(street)")
an, _ := fieldSelection.SelectField("address")
dto := examples.User{
Name: gofieldselect.Get(n, "name", user.Name),
Surname: gofieldselect.Get(n, "surname", user.Surname),
Age: gofieldselect.Get(n, "age", user.Age),
Address: &examples.Address{
Street: gofieldselect.Get(an.Child, "street", user.Address.Street),
Number: gofieldselect.Get(an.Child, "number", user.Address.Number),
},
}
// dto only contains `name` and `address.street` set, the rest of the fields contain the default value.
```
Check [examples/get](./examples/get/main.go) to see it in action.
### Reflection for an existing instance
```go
n, _ := gofieldselect.Parse("name,surname")
src := User{Name: "John", Surname: "Doe", Age: 20}
selected, _ := gofieldselect.GetWithReflection(n, src)
// selected only contains `name` and `surname` set, `age` is set to its default value.
```
Check [examples/getwithreflection](./examples/getwithreflection/main.go) to see it in action.
## 🚀 Features
GoFieldSelect provides a way to return only certain fields. It can be used as a query parameter in your REST endpoints.
Imagine you have the following JSON:
```json
{
"id": 1,
"name": "John",
"address": {
"street": "Example street",
"number": "1"
}
}
```
### Root field selection
JSON selecting some fields: `?fields=id,name`:
```json
{
"id": 1,
"name": "John"
}
```
### Nested field selection
JSON selecting some fields: `?fields=id,name,address(street)`:
```json
{
"id": 1,
"name": "John",
"address": {
"street": "Example street"
}
}
```
[field-selection]: https://learn.microsoft.com/en-us/azure/data-api-builder/keywords/select-rest