Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial on how to convert a protobuf message and filter certain fields to a resulting map. #42

Merged
merged 13 commits into from
Feb 15, 2024
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go 1.20
- name: Setup Go 1.21
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- name: Run tests with coverage
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
- uses: codecov/codecov-action@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go 1.20
- name: Setup Go 1.21
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- name: Go vet
run: go vet ./...
9 changes: 6 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.14', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20' ]
go-version: [ '1.14', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20', '1.21' ]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true

- name: Run tests
run: go test -v ./...
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import fieldmask_utils "github.com/mennanov/fieldmask-utils"

// A function that maps field mask field names to the names used in Go structs.
// It has to be implemented according to your needs.
// Scroll down for a reference on how to apply field masks to your gRPC services.
func naming(s string) string {
if s == "foo" {
return "Foo"
Expand Down Expand Up @@ -80,6 +81,45 @@ func main() {
}
```

#### Naming function

For developers that are looking for a mechanism to apply a mask field in their update endpoints using gRPC services,
there are multiple options for the naming function described above:

- Using the `CamelCase` function provided in
the [original protobuf repository](https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L2648).
This repository has been deprecated and it will potentially trigger lint errors.
- You can copy-paste the `CamelCase` function to your own project or,
- You can use an [Open Source alternative](https://github.com/gojaguar/jaguar) that provides the same functionality,
already took care of copying the code, and also added tests.

```go
func main() {
mask := &fieldmaskpb.FieldMask{Paths: []string{"username"}}
mask.Normalize()
req := &UpdateUserRequest{
User: &User{
Id: 1234,
Username: "Test",
},
}
if !mask.IsValid(req) {
return
}
protoMask, err := fieldmask_utils.MaskFromProtoFieldMask(mask, strings.PascalCase)
if err != nil {
return
}
m := make(map[string]any)
err = fieldmask_utils.StructToMap(protoMask, req, m)
if err != nil {
return
}
fmt.Println("Resulting map:", m)
}
```


### Limitations

1. Larger scope field masks have no effect and are not considered invalid:
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/mennanov/fieldmask-utils
go 1.16

require (
github.com/kr/text v0.2.0 // indirect
github.com/gojaguar/jaguar v0.6.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
google.golang.org/genproto v0.0.0-20220531173845-685668d2de03
google.golang.org/protobuf v1.28.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
github.com/stretchr/testify v1.8.4
google.golang.org/genproto v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/protobuf v1.32.0
)
Loading
Loading