-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_field.go
151 lines (133 loc) · 5.21 KB
/
proto_field.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package protokit
import (
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/types/descriptorpb"
)
type Label = string
const (
LabelOptional Label = "optional"
LabelSlice Label = "repeated"
LabelMap Label = "map"
)
type ProtoFieldTypeName = string
const (
ProtoFieldTypeNameDouble ProtoFieldTypeName = "double"
ProtoFieldTypeNameFloat = "float"
ProtoFieldTypeNameInt64 = "int64"
ProtoFieldTypeNameUInt64 = "uint64"
ProtoFieldTypeNameInt32 = "int32"
ProtoFieldTypeNameFixed64 = "fixed64"
ProtoFieldTypeNameFixed32 = "fixed32"
ProtoFieldTypeNameBool = "bool"
ProtoFieldTypeNameString = "string"
ProtoFieldTypeNameBytes = "bytes"
ProtoFieldTypeNameUInt32 = "uint32"
ProtoFieldTypeNameSFixed32 = "sfixed32"
ProtoFieldTypeNameSFixed64 = "sfixed64"
ProtoFieldTypeNameSInt32 = "sint32"
ProtoFieldTypeNameSInt64 = "sint64"
)
type GolangFieldTypeName = string
const (
GolangFieldTypeNameFloat32 GolangFieldTypeName = "float32"
GolangFieldTypeNameFloat64 = "float64"
GolangFieldTypeNameInt32 = "int32"
GolangFieldTypeNameInt64 = "int64"
GolangFieldTypeNameUInt32 = "uint32"
GolangFieldTypeNameUInt64 = "uint64"
GolangFieldTypeNameBool = "bool"
GolangFieldTypeNameString = "string"
GolangFieldTypeNameBytes = "[]byte"
)
var protoFieldTypeNameToGolangFieldTypeNameMapping = map[ProtoFieldTypeName]GolangFieldTypeName{
ProtoFieldTypeNameDouble: GolangFieldTypeNameFloat64,
ProtoFieldTypeNameFloat: GolangFieldTypeNameFloat32,
ProtoFieldTypeNameInt64: GolangFieldTypeNameInt64,
ProtoFieldTypeNameUInt64: GolangFieldTypeNameUInt64,
ProtoFieldTypeNameInt32: GolangFieldTypeNameInt32,
ProtoFieldTypeNameFixed64: GolangFieldTypeNameUInt64,
ProtoFieldTypeNameFixed32: GolangFieldTypeNameUInt32,
ProtoFieldTypeNameBool: GolangFieldTypeNameBool,
ProtoFieldTypeNameString: GolangFieldTypeNameString,
ProtoFieldTypeNameBytes: GolangFieldTypeNameBytes,
ProtoFieldTypeNameUInt32: GolangFieldTypeNameUInt32,
ProtoFieldTypeNameSFixed32: GolangFieldTypeNameInt32,
ProtoFieldTypeNameSFixed64: GolangFieldTypeNameInt64,
ProtoFieldTypeNameSInt32: GolangFieldTypeNameInt32,
ProtoFieldTypeNameSInt64: GolangFieldTypeNameInt64,
}
var protoFieldTypeNameMapping = map[descriptorpb.FieldDescriptorProto_Type]ProtoFieldTypeName{
1: ProtoFieldTypeNameDouble,
2: ProtoFieldTypeNameFloat,
3: ProtoFieldTypeNameInt64,
4: ProtoFieldTypeNameUInt64,
5: ProtoFieldTypeNameInt32,
6: ProtoFieldTypeNameFixed64,
7: ProtoFieldTypeNameFixed32,
8: ProtoFieldTypeNameBool,
9: ProtoFieldTypeNameString,
//10: "TYPE_GROUP",
//11: "TYPE_MESSAGE",
12: ProtoFieldTypeNameBytes,
13: ProtoFieldTypeNameUInt32,
//14: "TYPE_ENUM",
15: ProtoFieldTypeNameSFixed32,
16: ProtoFieldTypeNameSFixed64,
17: ProtoFieldTypeNameSInt32,
18: ProtoFieldTypeNameSInt64,
}
type ProtoField struct {
fd *desc.FieldDescriptor
RawName string
Name string // proto field name
Comment *Comment // 注释
Label Label // Label类型
KeyTypeName string // key的proto type name
ValueTypeName string // value的proto type name(如果是map的话)
}
func NewProtoField(pf *ProtoFile, fd *desc.FieldDescriptor) *ProtoField {
return &ProtoField{
RawName: fd.GetName(),
Name: GoFieldName(fd.GetName()),
fd: fd,
}
}
func (p *Parser) BuildProtoField(pf *ProtoFile, fd *desc.FieldDescriptor) *ProtoField {
f := NewProtoField(pf, fd)
f.Comment = p.comments[fd.AsFieldDescriptorProto()]
switch fd.GetLabel() {
case descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL, descriptorpb.FieldDescriptorProto_LABEL_REQUIRED:
f.Label = LabelOptional
f.KeyTypeName = p.MustGetFieldTypeName(fd)
case descriptorpb.FieldDescriptorProto_LABEL_REPEATED:
if !fd.IsMap() {
f.Label = LabelSlice
f.KeyTypeName = p.MustGetFieldTypeName(fd)
} else {
f.Label = LabelMap
f.KeyTypeName = p.MustGetFieldTypeName(fd.GetMapKeyType())
f.ValueTypeName = p.MustGetFieldTypeName(fd.GetMapValueType())
}
}
return f
}
func (pf *ProtoField) AsFieldDescriptor() *desc.FieldDescriptor { return pf.fd }
func ProtoFieldTypeNameToGolangFieldTypeName(protoFieldTypeName string) string {
s, ok := protoFieldTypeNameToGolangFieldTypeNameMapping[protoFieldTypeName]
if ok {
return s
}
return protoFieldTypeName
}
func FieldDescriptorProtoTypeToProtoFieldTypeName(t descriptorpb.FieldDescriptorProto_Type) string {
return protoFieldTypeNameMapping[t]
}
func FieldDescriptorProtoTypeToGolangFieldTypeName(t descriptorpb.FieldDescriptorProto_Type) string {
return ProtoFieldTypeNameToGolangFieldTypeName(protoFieldTypeNameMapping[t])
}
func (pf *ProtoField) KeyGoTypeName() string {
return ProtoFieldTypeNameToGolangFieldTypeName(pf.KeyTypeName)
}
func (pf *ProtoField) ValueGoTypeName() string {
return ProtoFieldTypeNameToGolangFieldTypeName(pf.ValueTypeName)
}