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: