Skip to content

Commit

Permalink
support clone and merge of non-nullable fields
Browse files Browse the repository at this point in the history
Cloning or merging is broken if a message uses a repeated non-nullable
struct field (segmentation fault). This change implements the handling
of a non-pointer slice and adds clone and merge tests for non-nullable
fields.

This is the change proposed in gogo#529.
  • Loading branch information
RaduBerinde committed Jan 2, 2019
1 parent 4cbf7e3 commit d26e6ac
Show file tree
Hide file tree
Showing 4 changed files with 619 additions and 83 deletions.
21 changes: 21 additions & 0 deletions proto/table_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,27 @@ func (mi *mergeInfo) computeMergeInfo() {
}
case reflect.Struct:
switch {
case !isPointer && isSlice:
// This case is for the gogoproto nullable extension, specifically when
// we have a repeated struct with nullable=false.
mergeInfo := getMergeInfo(tf)
mfi.merge = func(dst, src pointer) {
dstSlice := dst.getSlice(tf)
srcSlice := src.getSlice(tf)

numExisting := dstSlice.Len()
numNew := srcSlice.Len()
newSlice := reflect.MakeSlice(reflect.SliceOf(tf), numExisting+numNew, numExisting+numNew)
reflect.Copy(newSlice, dstSlice)
// Clone each element from src.
for i := 0; i < numNew; i++ {
mergeInfo.merge(
valToPointer(newSlice.Index(numExisting+i).Addr()),
valToPointer(srcSlice.Index(i).Addr()),
)
}
dstSlice.Set(newSlice)
}
case !isPointer:
mergeInfo := getMergeInfo(tf)
mfi.merge = func(dst, src pointer) {
Expand Down
Loading

0 comments on commit d26e6ac

Please sign in to comment.