From 7cfb81701a2097dbe799155bb478691d9c6dfb2d Mon Sep 17 00:00:00 2001 From: Marcos Huck Date: Thu, 15 Feb 2024 03:34:40 -0300 Subject: [PATCH] Tutorial on how to convert a protobuf message and filter certain fields 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 * Improve text Signed-off-by: Marcos Huck * Fix format issue Signed-off-by: Marcos Huck * Improve pipelines Signed-off-by: Marcos Huck * Fix dependencies Signed-off-by: Marcos Huck * Revert version change Signed-off-by: Marcos Huck * 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 --- .github/workflows/tests.yml | 2 ++ README.md | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0568533..0db151c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 ./... \ No newline at end of file diff --git a/README.md b/README.md index 5cfb661..bd245a9 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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: