protoc-gen-go-msgclone
Command protoc-gen-go-msgclone is a protoc plugin that generates code to clone messages without reflection.
Features
See also examples/addressbook.
// Code generated by protoc-gen-go-msgclone. DO NOT EDIT.
// source: examples/addressbook/addressbook.proto
package addressbook
func (x *Person) ShallowClone() *Person {
return &Person{
Name: x.GetName(),
Id: x.GetId(),
Email: x.GetEmail(),
Phones: x.GetPhones(),
LastUpdated: x.GetLastUpdated(),
}
}
func (x *Person_PhoneNumber) ShallowClone() *Person_PhoneNumber {
return &Person_PhoneNumber{
Number: x.GetNumber(),
Type: x.GetType(),
}
}
func (x *AddressBook) ShallowClone() *AddressBook {
return &AddressBook{
People: x.GetPeople(),
}
}
ShallowClone
Generated method, for all messages types (including nested), which will instance a new message of the same type,
performing a shallow copy of all fields from the receiver, using the getter methods generated by the official go plugin.
The method name may be overridden via the shallow_clone_name
option.
Usage
Generates *.pb.msgclone.go
files, alongside the usual *.pb.go
files, for all proto files with at least one message.
Standard protoc
plugin usage, with several options common to the go
and go-grpc
plugins.
Code is generated using the protoc
command, as usual, by providing at least one additional argument, like
--go-msgclone_out=OUT_DIR
. Plugin options may also be provided in the usual manner, like
--go-msgclone_opt=OPTION[=VALUE]
.
# install the plugin
go install github.com/joeycumines/protoc-gen-go-msgclone@latest
# example protoc usage based on https://developers.google.com/protocol-buffers/docs/reference/go-generated
protoc --proto_path=src --go_out=out --go_opt=paths=source_relative --go-msgclone_out=out --go-msgclone_opt=paths=source_relative foo.proto bar/baz.proto