Skip to content

Commit

Permalink
Use ProtoSize() in MarshalTo when enabled for oneof fields (#618)
Browse files Browse the repository at this point in the history
Fixes #617
Adds test which generates a marshalTo for a oneof message. This will fail to compile if the wrong size method is generated in the marshalTo method.
  • Loading branch information
gaffneyc authored and jmarais committed Sep 4, 2019
1 parent 1d64e23 commit 23325ce
Show file tree
Hide file tree
Showing 13 changed files with 909 additions and 136 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ regenerate:
make -C test/issue498 regenerate
make -C test/issue503 regenerate
make -C test/issue530 regenerate
make -C test/issue617 regenerate

make gofmt

Expand Down
7 changes: 6 additions & 1 deletion plugin/marshalto/marshalto.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,12 @@ func (p *marshalto) Generate(file *generator.FileDescriptor) {
ccTypeName := p.OneOfTypeName(message, field)
p.P(`func (m *`, ccTypeName, `) MarshalTo(dAtA []byte) (int, error) {`)
p.In()
p.P(`return m.MarshalToSizedBuffer(dAtA[:m.Size()])`)
if gogoproto.IsProtoSizer(file.FileDescriptorProto, message.DescriptorProto) {
p.P(`size := m.ProtoSize()`)
} else {
p.P(`size := m.Size()`)
}
p.P(`return m.MarshalToSizedBuffer(dAtA[:size])`)
p.Out()
p.P(`}`)
p.P(``)
Expand Down
3 changes: 2 additions & 1 deletion test/issue322/issue322.pb.go

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

32 changes: 32 additions & 0 deletions test/issue617/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Protocol Buffers for Go with Gadgets
#
# Copyright (c) 2019, The GoGo Authors. All rights reserved.
# http://github.com/gogo/protobuf
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

regenerate:
go install github.com/gogo/protobuf/protoc-gen-gogo
go install github.com/gogo/protobuf/protoc-min-version
protoc-min-version --version="3.0.0" --gogo_out=. --proto_path=../../../../../:../../protobuf/:. *.proto
Loading

0 comments on commit 23325ce

Please sign in to comment.