https://github.com/olexnzarov/protomask
A package that makes protobuf messages and field masks just work together
https://github.com/olexnzarov/protomask
assign fieldmask go grpc protobuf update
Last synced: about 1 month ago
JSON representation
A package that makes protobuf messages and field masks just work together
- Host: GitHub
- URL: https://github.com/olexnzarov/protomask
- Owner: olexnzarov
- License: mit
- Created: 2023-09-18T13:15:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-20T16:07:10.000Z (over 2 years ago)
- Last Synced: 2025-03-26T16:12:41.372Z (about 1 year ago)
- Topics: assign, fieldmask, go, grpc, protobuf, update
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protomask [](https://pkg.go.dev/github.com/olexnzarov/protomask) [](https://github.com/olexnzarov/protomask/releases) [](https://github.com/olexnzarov/protomask/actions/workflows/tests.yml)
protomask is a package that lets you update protobuf messages with a help of field masks.
**Features**
- Assign values from one message to another based on a field mask.
- Create a mask of all populated fields of a message.
- Supports nested fields, and handles their parent being nil.
- Supports [fieldmaskpb](https://google.golang.org/protobuf/types/known/), including `oneof` properties.
## Installation
```sh
go get github.com/olexnzarov/protomask
```
## Usage
```go
func (s *BookServer) UpdateBookPrice(id int64, priceCents int64) error {
update := &pb.Book{
Id: id,
Price: &pb.Price{
Cents: priceCents,
},
}
mask, _ := fieldmaskpb.New(update, "price.cents")
return s.UpdateBook(update, mask)
}
func (s *BookServer) UpdateBook(update *pb.Book, updateMask protomask.FieldMask) error {
book, err := s.bookStorage.GetById(update.Id)
if err != nil {
return err
}
err = protomask.Update(book, update, updateMask)
if err != nil {
return err
}
return s.bookStorage.Save(book)
}
```
## Examples
See [protomask_test.go](./protomask_test.go) for more examples on how to use the package. Also, you can check out [gofu](https://github.com/olexnzarov/gofu) for a real-life example of how it can be used.
## License
This code is available under the MIT license, allowing for free use, modification, and distribution.