Skip to content

propertechnologies/fieldmask-utils

 
 

Repository files navigation

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.)

Examples

Copy from a protobuf message to a protobuf message:

// test.proto

message UpdateUserRequest {
    User user = 1;
    google.protobuf.FieldMask field_mask = 2;
}
var request UpdateUserRequest
userDst := &testproto.User{} // a struct to copy to
mask, err := fieldmask_utils.MaskFromProtoFieldMask(request.FieldMask)
// 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{}:

var request UpdateUserRequest
userDst := make(map[string]interface{}) // a map to copy to
mask, err := fieldmask_utils.MaskFromProtoFieldMask(request.FieldMask)
// 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:

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 Any and 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. Pointers must also be coherent: if a field is a pointer in the source struct, then it also must be a pointer (not a value field) in the destination struct.

Releases

No releases published

Packages

No packages published

Languages

  • Go 98.6%
  • Makefile 1.4%