Skip to content

Commit

Permalink
compare plugin. update compare for oneof types to check this and that…
Browse files Browse the repository at this point in the history
… types and either call Compare if they are the same or return 1/-1. (#600)
  • Loading branch information
jmarais authored Aug 18, 2019
1 parent 28a6bbf commit 4c00d2f
Show file tree
Hide file tree
Showing 45 changed files with 12,515 additions and 3,483 deletions.
56 changes: 55 additions & 1 deletion plugin/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,65 @@ func (p *plugin) generateMessage(file *generator.FileDescriptor, message *genera
p.In()
p.P(`return -1`)
p.Out()
p.P(`} else if c := this.`, fieldname, `.Compare(that1.`, fieldname, `); c != 0 {`)
p.P(`} else {`)
p.In()

// Generate two type switches in order to compare the
// types of the oneofs. If they are of the same type
// call Compare, otherwise return 1 or -1.
p.P(`thisType := -1`)
p.P(`switch this.`, fieldname, `.(type) {`)
for i, subfield := range message.Field {
if *subfield.OneofIndex == *field.OneofIndex {
ccTypeName := p.OneOfTypeName(message, subfield)
p.P(`case *`, ccTypeName, `:`)
p.In()
p.P(`thisType = `, i)
p.Out()
}
}
p.P(`default:`)
p.In()
p.P(`panic(fmt.Sprintf("compare: unexpected type %T in oneof", this.`, fieldname, `))`)
p.Out()
p.P(`}`)

p.P(`that1Type := -1`)
p.P(`switch that1.`, fieldname, `.(type) {`)
for i, subfield := range message.Field {
if *subfield.OneofIndex == *field.OneofIndex {
ccTypeName := p.OneOfTypeName(message, subfield)
p.P(`case *`, ccTypeName, `:`)
p.In()
p.P(`that1Type = `, i)
p.Out()
}
}
p.P(`default:`)
p.In()
p.P(`panic(fmt.Sprintf("compare: unexpected type %T in oneof", that1.`, fieldname, `))`)
p.Out()
p.P(`}`)

p.P(`if thisType == that1Type {`)
p.In()
p.P(`if c := this.`, fieldname, `.Compare(that1.`, fieldname, `); c != 0 {`)
p.In()
p.P(`return c`)
p.Out()
p.P(`}`)
p.Out()
p.P(`} else if thisType < that1Type {`)
p.In()
p.P(`return -1`)
p.Out()
p.P(`} else if thisType > that1Type {`)
p.In()
p.P(`return 1`)
p.Out()
p.P(`}`)
p.Out()
p.P(`}`)
} else {
p.generateField(file, message, field)
}
Expand Down
3 changes: 3 additions & 0 deletions protoc-gen-gogo/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3005,6 +3005,9 @@ func (g *Generator) generateOneofDecls(mc *msgCtx, topLevelFields []topLevelFiel
if gogoproto.IsProtoSizer(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
g.P(`ProtoSize() int`)
}
if gogoproto.HasCompare(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
g.P(`Compare(interface{}) int`)
}
g.Out()
g.P("}")
}
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-gogotypes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
if strings.HasSuffix(file.GetName(), "struct.proto") {
// TODO struct can also get a compare method when
// https://github.com/gogo/protobuf/issues/221 is fixed
continue
//continue
}
vanity.TurnOnCompareAll(file)
}
Expand Down
103 changes: 99 additions & 4 deletions test/issue322/issue322.pb.go

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

1 change: 1 addition & 0 deletions test/issue322/issue322.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ option (gogoproto.gostring_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.compare_all) = true;

message OneofTest {
oneof union {
Expand Down
24 changes: 24 additions & 0 deletions test/issue322/issue322pb_test.go

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

Loading

0 comments on commit 4c00d2f

Please sign in to comment.