Skip to content

Commit

Permalink
New extension to generate ProtoSize methods instead of Size. Fixes go…
Browse files Browse the repository at this point in the history
…go#56.

Regenerate files.
  • Loading branch information
dennwc committed Jan 30, 2016
1 parent e8904f5 commit dab86ea
Show file tree
Hide file tree
Showing 89 changed files with 37,780 additions and 22,650 deletions.
20 changes: 18 additions & 2 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ type Codec interface {

type marshaler interface {
MarshalTo(data []byte) (n int, err error)
Size() (n int)
}

func getSize(v interface{}) (int, bool) {
if sz, ok := v.(interface {
Size() (n int)
}); ok {
return sz.Size(), true
} else if sz, ok := v.(interface {
ProtoSize() (n int)
}); ok {
return sz.ProtoSize(), true
} else {
return 0, false
}
}

type codec struct {
Expand All @@ -55,7 +68,10 @@ func New(size int) Codec {

func (this *codec) Marshal(v interface{}) ([]byte, error) {
if m, ok := v.(marshaler); ok {
n := m.Size()
n, ok := getSize(v)
if !ok {
return proto.Marshal(v.(proto.Message))
}
if n > len(this.buf) {
this.buf = make([]byte, n)
}
Expand Down
18 changes: 18 additions & 0 deletions gogoproto/gogo.pb.go

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

4 changes: 4 additions & 0 deletions gogoproto/gogo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ extend google.protobuf.FileOptions {
optional bool goproto_extensions_map_all = 63025;
optional bool goproto_unrecognized_all = 63026;
optional bool gogoproto_import = 63027;

optional bool protosizer_all = 63028;
}

extend google.protobuf.MessageOptions {
Expand All @@ -93,6 +95,8 @@ extend google.protobuf.MessageOptions {

optional bool goproto_extensions_map = 64025;
optional bool goproto_unrecognized = 64026;

optional bool protosizer = 64028;
}

extend google.protobuf.FieldOptions {
Expand Down
4 changes: 4 additions & 0 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ func IsSizer(file *google_protobuf.FileDescriptorProto, message *google_protobuf
return proto.GetBoolExtension(message.Options, E_Sizer, proto.GetBoolExtension(file.Options, E_SizerAll, false))
}

func IsProtoSizer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool {
return proto.GetBoolExtension(message.Options, E_Protosizer, proto.GetBoolExtension(file.Options, E_ProtosizerAll, false))
}

func IsGoEnumStringer(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool {
return proto.GetBoolExtension(enum.Options, E_GoprotoEnumStringer, proto.GetBoolExtension(file.Options, E_GoprotoEnumStringerAll, true))
}
Expand Down
8 changes: 7 additions & 1 deletion io/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ type fullWriter struct {
func (this *fullWriter) WriteMsg(msg proto.Message) (err error) {
var data []byte
if m, ok := msg.(marshaler); ok {
n := m.Size()
n, ok := getSize(m)
if !ok {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
if n >= len(this.buffer) {
this.buffer = make([]byte, n)
}
Expand Down
15 changes: 14 additions & 1 deletion io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,18 @@ type ReadCloser interface {

type marshaler interface {
MarshalTo(data []byte) (n int, err error)
Size() (n int)
}

func getSize(v interface{}) (int, bool) {
if sz, ok := v.(interface {
Size() (n int)
}); ok {
return sz.Size(), true
} else if sz, ok := v.(interface {
ProtoSize() (n int)
}); ok {
return sz.ProtoSize(), true
} else {
return 0, false
}
}
8 changes: 7 additions & 1 deletion io/uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ type uint32Writer struct {
func (this *uint32Writer) WriteMsg(msg proto.Message) (err error) {
var data []byte
if m, ok := msg.(marshaler); ok {
n := m.Size()
n, ok := getSize(m)
if !ok {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
if n >= len(this.buffer) {
this.buffer = make([]byte, n)
}
Expand Down
8 changes: 7 additions & 1 deletion io/varint.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ type varintWriter struct {
func (this *varintWriter) WriteMsg(msg proto.Message) (err error) {
var data []byte
if m, ok := msg.(marshaler); ok {
n := m.Size()
n, ok := getSize(m)
if !ok {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
if n >= len(this.buffer) {
this.buffer = make([]byte, n)
}
Expand Down
3 changes: 2 additions & 1 deletion plugin/embedcheck/embedcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ var overwriters []map[string]gogoproto.EnableFunc = []map[string]gogoproto.Enabl
"verboseequal": gogoproto.HasVerboseEqual,
},
{
"size": gogoproto.IsSizer,
"size": gogoproto.IsSizer,
"protosizer": gogoproto.IsProtoSizer,
},
{
"unmarshaler": gogoproto.IsUnmarshaler,
Expand Down
6 changes: 3 additions & 3 deletions plugin/equal/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (p *plugin) generateMsgNullAndTypeCheck(ccTypeName string, verbose bool) {

func (p *plugin) generateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto, verbose bool) {
proto3 := gogoproto.IsProto3(file.FileDescriptorProto)
fieldname := p.GetOneOfFieldName(message, field)
fieldname := p.GetOneOfFieldName(file, message, field)
repeated := field.IsRepeated()
ctype := gogoproto.IsCustomType(field)
nullable := gogoproto.IsNullable(field)
Expand Down Expand Up @@ -451,7 +451,7 @@ func (p *plugin) generateMessage(file *generator.FileDescriptor, message *genera
for _, field := range message.Field {
oneof := field.OneofIndex != nil
if oneof {
fieldname := p.GetFieldName(message, field)
fieldname := p.GetFieldName(file, message, field)
if _, ok := oneofs[fieldname]; ok {
continue
} else {
Expand Down Expand Up @@ -575,7 +575,7 @@ func (p *plugin) generateMessage(file *generator.FileDescriptor, message *genera
if !oneof {
continue
}
ccTypeName := p.OneOfTypeName(message, field)
ccTypeName := p.OneOfTypeName(file, message, field)
if verbose {
p.P(`func (this *`, ccTypeName, `) VerboseEqual(that interface{}) error {`)
} else {
Expand Down
6 changes: 3 additions & 3 deletions plugin/face/face.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (p *plugin) Generate(file *generator.FileDescriptor) {
p.In()
p.P(`Proto() `, protoPkg.Use(), `.Message`)
for _, field := range message.Field {
fieldname := p.GetFieldName(message, field)
fieldname := p.GetFieldName(file, message, field)
goTyp, _ := p.GoType(message, field)
if p.IsMap(field) {
m := p.GoMapType(nil, field)
Expand All @@ -198,7 +198,7 @@ func (p *plugin) Generate(file *generator.FileDescriptor) {
p.P(`}`)
p.P(``)
for _, field := range message.Field {
fieldname := p.GetFieldName(message, field)
fieldname := p.GetFieldName(file, message, field)
goTyp, _ := p.GoType(message, field)
if generator.IsMap(file.FileDescriptorProto, field) {
m := p.GoMapType(nil, field)
Expand All @@ -216,7 +216,7 @@ func (p *plugin) Generate(file *generator.FileDescriptor) {
p.In()
p.P(`this := &`, ccTypeName, `{}`)
for _, field := range message.Field {
fieldname := p.GetFieldName(message, field)
fieldname := p.GetFieldName(file, message, field)
p.P(`this.`, fieldname, ` = that.Get`, fieldname, `()`)
}
p.P(`return this`)
Expand Down
6 changes: 3 additions & 3 deletions plugin/gostring/gostring.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (p *gostring) Generate(file *generator.FileDescriptor) {
for _, field := range message.Field {
nullable := gogoproto.IsNullable(field)
repeated := field.IsRepeated()
fieldname := p.GetFieldName(message, field)
fieldname := p.GetFieldName(file, message, field)
oneof := field.OneofIndex != nil
if oneof {
if _, ok := oneofs[fieldname]; ok {
Expand Down Expand Up @@ -285,7 +285,7 @@ func (p *gostring) Generate(file *generator.FileDescriptor) {
if !oneof {
continue
}
ccTypeName := p.OneOfTypeName(message, field)
ccTypeName := p.OneOfTypeName(file, message, field)
p.P(`func (this *`, ccTypeName, `) GoString() string {`)
p.In()
p.P(`if this == nil {`)
Expand All @@ -294,7 +294,7 @@ func (p *gostring) Generate(file *generator.FileDescriptor) {
p.Out()
p.P(`}`)
outFlds := []string{}
fieldname := p.GetOneOfFieldName(message, field)
fieldname := p.GetOneOfFieldName(file, message, field)
if field.IsMessage() || p.IsGroup(field) {
tmp := strings.Join([]string{"`", fieldname, ":` + "}, "")
tmp += strings.Join([]string{fmtPkg.Use(), `.Sprintf("%#v", this.`, fieldname, `)`}, "")
Expand Down
Loading

0 comments on commit dab86ea

Please sign in to comment.