package
0.0.0-20240417025418-8e18a74854d4
Repository: https://github.com/echogroot/kratos-examples.git
Documentation: pkg.go.dev

# README

Protobuf Field Mask utils for Go

Build Status Coverage Status

Features:

  • Copy from any Go struct to any compatible Go struct with a field mask applied
  • Copy from any Go struct to a map[string]interface{} with a field mask applied
  • Extensible masks (e.g. inverse mask: copy all except those mentioned, etc.)
  • Supports Protobuf Any message types.

If you're looking for a simple FieldMask library to work with protobuf messages only (not arbitrary structs) consider this tiny repo: https://github.com/mennanov/fmutils

Examples

Copy from a protobuf message to a protobuf message:

// testproto/test.proto

message UpdateUserRequest {
    User user = 1;
    google.protobuf.FieldMask field_mask = 2;
}
import "github.com/golang/protobuf/protoc-gen-go/generator"

var request UpdateUserRequest
userDst := &testproto.User{} // a struct to copy to
mask, err := fieldmask_utils.MaskFromPaths(request.FieldMask.Paths, generator.CamelCase)
// handle err...
fieldmask_utils.StructToStruct(mask, request.User, userDst)
// Only the fields mentioned in the field mask will be copied to userDst, other fields are left intact

Copy from a protobuf message to a map[string]interface{}:

import "github.com/golang/protobuf/protoc-gen-go/generator"

var request UpdateUserRequest
userDst := make(map[string]interface{}) // a map to copy to
mask, err := fieldmask_utils.MaskFromProtoFieldMask(request.FieldMask, generator.CamelCase)
// handle err...
err := fieldmask_utils.StructToMap(mask, request.User, userDst)
// handle err..
// Only the fields mentioned in the field mask will be copied to userDst, other fields are left intact

Copy with an inverse mask:

import "github.com/golang/protobuf/protoc-gen-go/generator"

var request UpdateUserRequest
userDst := &testproto.User{} // a struct to copy to
mask := fieldmask_utils.MaskInverse{"Id": nil, "Friends": fieldmask_utils.MaskInverse{"Username": nil}}
fieldmask_utils.StructToStruct(mask, request.User, userDst)
// Only the fields that are not mentioned in the field mask will be copied to userDst, other fields are left intact.

Limitations

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

    field mask strings "a", "a.b", "a.b.c" will result in a mask a{b{c}}, which is the same as "a.b.c".

  2. Masks inside a protobuf Map are not supported.

  3. When copying from a struct to struct the destination struct must have the same fields (or a subset) as the source struct. Either of source or destination fields can be a pointer as long as it is a pointer to the type of the corresponding field.

  4. oneof fields are represented differently in fieldmaskpb.FieldMask compared to fieldmask_util.Mask. In FieldMask the fields are represented using their property name, in this library they are prefixed with the oneof name matching how Go generated code is laid out. This can lead to issues when converting between the two, for example when using MaskFromPaths or MaskFromProtoFieldMask.

# Packages

No description provided by the author

# Functions

FieldFilterFromPaths creates a new FieldFilter from the given paths.
FieldFilterFromString creates a new FieldFilterContainer from string.
MaskFromPaths creates a new Mask from the given paths.
MaskFromProtoFieldMask creates a Mask from the given FieldMask.
MaskFromString creates a new Mask instance from a given string.
MaskInverseFromPaths creates a new MaskInverse from the given paths.
MaskInverseFromProtoFieldMask creates a MaskInverse from the given FieldMask.
MaskInverseFromString creates a new MaskInverse instance from a given string.
StructToMap copies `src` struct to the `dst` map.
StructToStruct copies `src` struct to `dst` struct using the given FieldFilter.
WithTag sets the destination field name.

# Interfaces

FieldFilter is an interface used by the copying function to filter fields that are needed to be copied.
FieldFilterContainer is a FieldFilter with additional methods Get and Set.

# Type aliases

Mask is a tree-based implementation of a FieldFilter.
MaskInverse is an inversed version of a Mask (will copy all the fields except those mentioned in the mask).
Option function modifies the given options.