Skip to content

Commit 5e0eda4

Browse files
committed
protoc-gen-go: generate deprecation comments for oneof fields
1 parent 3ed0fc6 commit 5e0eda4

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

protoc-gen-go/generator/generator.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,7 @@ type oneofSubField struct {
18851885
fieldNumber int // Actual field number, as defined in proto, e.g. 12
18861886
getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName"
18871887
protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5"
1888+
deprecated string // Deprecation comment, if any.
18881889
}
18891890

18901891
// wireTypeName returns a textual wire type, needed for oneof sub fields in generated code.
@@ -2154,6 +2155,9 @@ func (f *oneofField) getter(g *Generator, mc *msgCtx) {
21542155
g.P()
21552156
// Getters for each oneof
21562157
for _, sf := range f.subFields {
2158+
if sf.deprecated != "" {
2159+
g.P(sf.deprecated)
2160+
}
21572161
g.P("func (m *", mc.goName, ") ", Annotate(mc.message.file, sf.fullPath, sf.getterName), "() "+sf.goType+" {")
21582162
g.P("if x, ok := m.", f.getterName, "().(*", sf.oneofTypeName, "); ok {")
21592163
g.P("return x.", sf.goName)
@@ -2583,6 +2587,11 @@ func (g *Generator) generateMessage(message *Descriptor) {
25832587
}
25842588
}
25852589

2590+
fieldDeprecated := ""
2591+
if field.GetOptions().GetDeprecated() {
2592+
fieldDeprecated = deprecationComment
2593+
}
2594+
25862595
dvalue := g.getterDefault(field, goTypeName)
25872596
if oneof {
25882597
tname := goTypeName + "_" + fieldName
@@ -2626,17 +2635,13 @@ func (g *Generator) generateMessage(message *Descriptor) {
26262635
getterDef: dvalue,
26272636
protoDef: field.GetDefaultValue(),
26282637
oneofTypeName: tname,
2638+
deprecated: fieldDeprecated,
26292639
}
26302640
oneofField.subFields = append(oneofField.subFields, &sf)
26312641
g.RecordTypeUse(field.GetTypeName())
26322642
continue
26332643
}
26342644

2635-
fieldDeprecated := ""
2636-
if field.GetOptions().GetDeprecated() {
2637-
fieldDeprecated = deprecationComment
2638-
}
2639-
26402645
fieldFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageFieldPath, i)
26412646
c, ok := g.makeComments(fieldFullPath)
26422647
if ok {

protoc-gen-go/testdata/deprecated/deprecated.pb.go

Lines changed: 1 addition & 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)