maskmerge

package module
v0.0.0-...-9c2f789 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 6 Imported by: 0

README

Merge utility for Protobuf using FieldMask

MaskMerge implements merging of two protocol buffer messages as specified by the FieldMask documentation.

Installation

go get github.com/marcusirgens/maskmerge

Usage

Feed MaskMerge with a proto.Message to update, a proto.Message to read values from, and a fieldmaskpb.FieldMask. See example_test.go for a full example.

maskmerge.MaskMerge(dst, src, mask)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Overview

Package maskmerge deals with merging protocol buffer messages using field masks.

See google.golang.org/protobuf/types/known/fieldmaskpb for more information on how field masks are interpreted.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaskMerge

func MaskMerge(dst, src proto.Message, mask *fieldmaskpb.FieldMask) error

MaskMerge merges the fields in mask in src into dst. Following the specification field mask specification, lists and messages are merged, not replaced.

Example
package main

import (
	"fmt"
	"log"

	"github.com/marcusirgens/maskmerge"
	"github.com/marcusirgens/maskmerge/internal/prototest"
	"google.golang.org/protobuf/types/known/fieldmaskpb"
)

func main() {
	// Original data to update
	stored := &prototest.B1{D: 12, X: 15}
	// New data from the user
	userData := &prototest.B1{X: 42}
	// Only update path x
	mask, err := fieldmaskpb.New(userData, "x")
	if err != nil {
		log.Fatalf("invalid field mask: %v", err)
	}

	if err := maskmerge.MaskMerge(stored, userData, mask); err != nil {
		log.Fatalf("merging messages: %v", err)
	}

	// not using prototext because the output is not stable.
	fmt.Printf("d:%d x:%d", stored.D, stored.X)
}
Output:

d:12 x:42

Types

This section is empty.

Directories

Path Synopsis
internal
prototest
Package prototest contains test files for maskmerge.
Package prototest contains test files for maskmerge.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL