-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_enum.go
39 lines (34 loc) · 1.07 KB
/
proto_enum.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
package protokit
import "github.com/jhump/protoreflect/desc"
type ProtoEnumField struct {
Name string
Number int32
}
type ProtoEnum struct {
dotFullyQualifiedTypeName string
ed *desc.EnumDescriptor
Fields []*ProtoEnumField
ProtoFile *ProtoFile
Name string // proto enum name
}
func NewProtoEnum(pf *ProtoFile, ed *desc.EnumDescriptor) *ProtoEnum {
v := &ProtoEnum{
ed: ed,
ProtoFile: pf,
Name: ed.GetName(),
}
for _, field := range v.ed.AsEnumDescriptorProto().Value {
v.Fields = append(v.Fields, &ProtoEnumField{
Name: field.GetName(),
Number: field.GetNumber(),
})
}
return v
}
func (pe *ProtoEnum) DotFullyQualifiedTypeName() string { return pe.dotFullyQualifiedTypeName }
func (pe *ProtoEnum) AsEnumDescriptor() *desc.EnumDescriptor { return pe.ed }
func (p *Parser) BuildProtoEnum(pf *ProtoFile, ed *desc.EnumDescriptor) *ProtoEnum {
pe := NewProtoEnum(pf, ed)
pe.dotFullyQualifiedTypeName = p.descriptor2DotFullyQualifiedTypeName[pe.ed]
return pe
}