All Projects → mennanov → fieldmask-utils

mennanov / fieldmask-utils

Licence: MIT License
Protobuf Field Mask Go utils

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to fieldmask-utils

goverter
Generate type-safe Go converters by simply defining an interface
Stars: ✭ 100 (-21.26%)
Mutual labels:  copy, struct
Protoc Gen Gotag
Add custom struct tags to protobuf generated structs
Stars: ✭ 97 (-23.62%)
Mutual labels:  protobuf, struct
fmutils
Golang protobuf FieldMask missing utils
Stars: ✭ 64 (-49.61%)
Mutual labels:  protobuf, fieldmask
ocaml-grpc-envoy
Using OCaml + gRPC via Envoy
Stars: ✭ 41 (-67.72%)
Mutual labels:  protobuf
javascript-serialization-benchmark
Comparison and benchmark of JavaScript serialization libraries (Protocol Buffer, Avro, BSON, etc.)
Stars: ✭ 54 (-57.48%)
Mutual labels:  protobuf
grpcoin
API-driven cryptocurrency paper trading game. Write a bot and play!
Stars: ✭ 53 (-58.27%)
Mutual labels:  protobuf
vtprotobuf
A Protocol Buffers compiler that generates optimized marshaling & unmarshaling Go code for ProtoBuf APIv2
Stars: ✭ 418 (+229.13%)
Mutual labels:  protobuf
protobuf-d
Protocol Buffers Compiler Plugin and Support Library for D
Stars: ✭ 32 (-74.8%)
Mutual labels:  protobuf
nameko-grpc
GRPC Extensions for Nameko
Stars: ✭ 51 (-59.84%)
Mutual labels:  protobuf
protopatch
protoc-gen-go patch utility
Stars: ✭ 58 (-54.33%)
Mutual labels:  protobuf
mnemosyne
Session management service with RPC API based on protobuf.
Stars: ✭ 15 (-88.19%)
Mutual labels:  protobuf
rpc
☎️ RPC: Type Driven Parser Generator
Stars: ✭ 16 (-87.4%)
Mutual labels:  protobuf
regenny
A reverse engineering tool to interactively reconstruct structures and generate header files
Stars: ✭ 58 (-54.33%)
Mutual labels:  struct
envy
envy: Deserialize environment variables into type-safe structs
Stars: ✭ 64 (-49.61%)
Mutual labels:  struct
zmq-protobuf
communication between python and cpp using zeromq and protobuf
Stars: ✭ 25 (-80.31%)
Mutual labels:  protobuf
argum
Parse incoming arguments in to structure
Stars: ✭ 18 (-85.83%)
Mutual labels:  struct
CoffeeChat
opensource im with server(go) and client(flutter+swift)
Stars: ✭ 111 (-12.6%)
Mutual labels:  protobuf
akka-http-scalapb
akka-http protobuf binary and json marshalling/unmarshalling for ScalaPB messages
Stars: ✭ 15 (-88.19%)
Mutual labels:  protobuf
liftbridge-api
Protobuf definitions for the Liftbridge gRPC API. https://github.com/liftbridge-io/liftbridge
Stars: ✭ 15 (-88.19%)
Mutual labels:  protobuf
CppSerialization
Performance comparison of the most popular C++ serialization protocols such as Cap'n'Proto, FastBinaryEncoding, Flatbuffers, Protobuf, JSON
Stars: ✭ 89 (-29.92%)
Mutual labels:  protobuf

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;
}
package main

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.
func naming(s string) string {
	if s == "foo" {
		return "Foo"
	}
	return s
}

func main () {
	var request UpdateUserRequest
	userDst := &testproto.User{} // a struct to copy to
	mask, _ := fieldmask_utils.MaskFromPaths(request.FieldMask.Paths, naming)
	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{}:

package main

import fieldmask_utils "github.com/mennanov/fieldmask-utils"

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

Copy with an inverse mask:

package main

import fieldmask_utils "github.com/mennanov/fieldmask-utils"

func main() {
	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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].