Skip to content

Commit

Permalink
merge: non nullable struct
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Mar 18, 2018
1 parent 763d1f0 commit 056fc7f
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ regenerate:
make -C test/issue322 regenerate
make -C test/issue330 regenerate
make -C test/importcustom-issue389 regenerate
make -C test/merge regenerate
make gofmt

tests:
Expand Down
5 changes: 4 additions & 1 deletion proto/table_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,10 @@ func (mi *mergeInfo) computeMergeInfo() {
case reflect.Struct:
switch {
case !isPointer:
panic(fmt.Sprintf("message field %s without pointer", tf))
mergeInfo := getMergeInfo(tf)
mfi.merge = func(dst, src pointer) {
mergeInfo.merge(dst, src)
}
case isSlice: // E.g., []*pb.T
mergeInfo := getMergeInfo(tf)
mfi.merge = func(dst, src pointer) {
Expand Down
5 changes: 5 additions & 0 deletions test/merge/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
regenerate:
go install github.com/gogo/protobuf/protoc-min-version
go install github.com/gogo/protobuf/protoc-gen-gogo
protoc-min-version --version="3.0.0" --gogo_out=. \
--proto_path=../../../../../:../../protobuf/:. merge.proto
121 changes: 121 additions & 0 deletions test/merge/merge.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/merge/merge.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";
package merge;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

message A {
B B = 1 [(gogoproto.nullable) = false];
}

message B {
int64 c = 1;
}
36 changes: 36 additions & 0 deletions test/merge/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package merge

import (
"testing"

"github.com/gogo/protobuf/proto"
)

func TestClone1(t *testing.T) {
f1 := &A{}
proto.Clone(f1)
}

func TestMerge1(t *testing.T) {
f1 := &A{}
f2 := &A{}
proto.Merge(f1, f2)
}

func TestMerge2(t *testing.T) {
f1 := &A{B: B{C: 1}}
f2 := &A{}
proto.Merge(f1, f2)
if f1.B.C != 1 {
t.Fatalf("got %d want %d", f1.B.C, 1)
}
}

func TestMerge3(t *testing.T) {
f1 := &A{}
f2 := &A{B: B{C: 1}}
proto.Merge(f1, f2)
if f1.B.C != 1 {
t.Fatalf("got %d want %d", f1.B.C, 1)
}
}

0 comments on commit 056fc7f

Please sign in to comment.