Skip to content

Commit 4c00d2f

Browse files
authored
compare plugin. update compare for oneof types to check this and that types and either call Compare if they are the same or return 1/-1. (#600)
1 parent 28a6bbf commit 4c00d2f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+12515
-3483
lines changed

plugin/compare/compare.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,65 @@ func (p *plugin) generateMessage(file *generator.FileDescriptor, message *genera
425425
p.In()
426426
p.P(`return -1`)
427427
p.Out()
428-
p.P(`} else if c := this.`, fieldname, `.Compare(that1.`, fieldname, `); c != 0 {`)
428+
p.P(`} else {`)
429+
p.In()
430+
431+
// Generate two type switches in order to compare the
432+
// types of the oneofs. If they are of the same type
433+
// call Compare, otherwise return 1 or -1.
434+
p.P(`thisType := -1`)
435+
p.P(`switch this.`, fieldname, `.(type) {`)
436+
for i, subfield := range message.Field {
437+
if *subfield.OneofIndex == *field.OneofIndex {
438+
ccTypeName := p.OneOfTypeName(message, subfield)
439+
p.P(`case *`, ccTypeName, `:`)
440+
p.In()
441+
p.P(`thisType = `, i)
442+
p.Out()
443+
}
444+
}
445+
p.P(`default:`)
446+
p.In()
447+
p.P(`panic(fmt.Sprintf("compare: unexpected type %T in oneof", this.`, fieldname, `))`)
448+
p.Out()
449+
p.P(`}`)
450+
451+
p.P(`that1Type := -1`)
452+
p.P(`switch that1.`, fieldname, `.(type) {`)
453+
for i, subfield := range message.Field {
454+
if *subfield.OneofIndex == *field.OneofIndex {
455+
ccTypeName := p.OneOfTypeName(message, subfield)
456+
p.P(`case *`, ccTypeName, `:`)
457+
p.In()
458+
p.P(`that1Type = `, i)
459+
p.Out()
460+
}
461+
}
462+
p.P(`default:`)
463+
p.In()
464+
p.P(`panic(fmt.Sprintf("compare: unexpected type %T in oneof", that1.`, fieldname, `))`)
465+
p.Out()
466+
p.P(`}`)
467+
468+
p.P(`if thisType == that1Type {`)
469+
p.In()
470+
p.P(`if c := this.`, fieldname, `.Compare(that1.`, fieldname, `); c != 0 {`)
429471
p.In()
430472
p.P(`return c`)
431473
p.Out()
432474
p.P(`}`)
475+
p.Out()
476+
p.P(`} else if thisType < that1Type {`)
477+
p.In()
478+
p.P(`return -1`)
479+
p.Out()
480+
p.P(`} else if thisType > that1Type {`)
481+
p.In()
482+
p.P(`return 1`)
483+
p.Out()
484+
p.P(`}`)
485+
p.Out()
486+
p.P(`}`)
433487
} else {
434488
p.generateField(file, message, field)
435489
}

protoc-gen-gogo/generator/generator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,9 @@ func (g *Generator) generateOneofDecls(mc *msgCtx, topLevelFields []topLevelFiel
30053005
if gogoproto.IsProtoSizer(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
30063006
g.P(`ProtoSize() int`)
30073007
}
3008+
if gogoproto.HasCompare(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
3009+
g.P(`Compare(interface{}) int`)
3010+
}
30083011
g.Out()
30093012
g.P("}")
30103013
}

protoc-gen-gogotypes/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func main() {
5757
if strings.HasSuffix(file.GetName(), "struct.proto") {
5858
// TODO struct can also get a compare method when
5959
// https://github.com/gogo/protobuf/issues/221 is fixed
60-
continue
60+
//continue
6161
}
6262
vanity.TurnOnCompareAll(file)
6363
}

test/issue322/issue322.pb.go

Lines changed: 99 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/issue322/issue322.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ option (gogoproto.gostring_all) = true;
77
option (gogoproto.populate_all) = true;
88
option (gogoproto.equal_all) = true;
99
option (gogoproto.testgen_all) = true;
10+
option (gogoproto.compare_all) = true;
1011

1112
message OneofTest {
1213
oneof union {

test/issue322/issue322pb_test.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)