Skip to content

Commit

Permalink
Tutorial on how to convert a protobuf message and filter certain fiel…
Browse files Browse the repository at this point in the history
…ds to a resulting map. (#42)

* Add test case using a PascalCase generator

Bump go version to 1.21
Add example in README
Add disclaimer about potential solutions for the naming function

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>

* Improve text

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>

* Fix format issue

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>

* Improve pipelines

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>

* Fix dependencies

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>

* Revert version change

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>

* Remove dependency by deleting test case

* Tidy dependencies

* Add final note in README

* Revert changes on go.mod and go.sum

* Tidy dependencies

---------

Signed-off-by: Marcos Huck <marcoshuck@ekumenlabs.com>
  • Loading branch information
marcoshuck authored Feb 15, 2024
1 parent 2de6178 commit 7cfb817
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true

- name: Run tests
run: go test -v ./...
42 changes: 42 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,47 @@ 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](https://github.com/gojaguar/jaguar/blob/main/strings/pascal_case.go), 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)
}
```

This will result in a map that contains the fields that need to be updated with their respective values.


### Limitations

1. Larger scope field masks have no effect and are not considered invalid:
Expand Down

0 comments on commit 7cfb817

Please sign in to comment.