-
Notifications
You must be signed in to change notification settings - Fork 15
/
which_oneof.go
54 lines (47 loc) · 1.48 KB
/
which_oneof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package fastreflection
import (
"github.com/cosmos/cosmos-proto/generator"
"google.golang.org/protobuf/compiler/protogen"
)
type whichOneofGen struct {
*generator.GeneratedFile
typeName string
message *protogen.Message
}
func (g *whichOneofGen) generate() {
g.genComment()
g.genFunc()
}
func (g *whichOneofGen) genComment() {
g.P("// WhichOneof reports which field within the oneof is populated,")
g.P("// returning nil if none are populated.")
g.P("// It panics if the oneof descriptor does not belong to this message.")
}
func (g *whichOneofGen) genFunc() {
g.P("func (x *", g.typeName, ") WhichOneof(d ", protoreflectPkg.Ident("OneofDescriptor"), ") ", protoreflectPkg.Ident("FieldDescriptor"), " {")
g.P("switch d.FullName() {")
for _, oneof := range g.message.Oneofs {
g.P("case \"", oneof.Desc.FullName(), "\": ")
g.genOneof(oneof)
}
g.P("default: ")
g.P("panic(", fmtPkg.Ident("Errorf"), "(\"%s is not a oneof field in ", g.message.Desc.FullName(), "\", d.FullName()))")
g.P("}")
// this part is unreachable
g.P("panic(\"unreachable\")")
g.P("}")
g.P()
}
func (g *whichOneofGen) genOneof(oneof *protogen.Oneof) {
// if none is populated then return nil
g.P("if x.", oneof.GoName, " == nil {")
g.P("return nil")
g.P("}")
// switch the type
g.P("switch x.", oneof.GoName, ".(type) {")
for _, field := range oneof.Fields {
g.P("case *", g.QualifiedGoIdent(field.GoIdent), ":")
g.P("return x.Descriptor().Fields().ByName(\"", field.Desc.Name(), "\")")
}
g.P("}")
}