-
Notifications
You must be signed in to change notification settings - Fork 30
/
field.go
167 lines (125 loc) · 2.74 KB
/
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package meta
import (
"fmt"
"strconv"
)
type FieldDescriptor struct {
*CommentGroup
Name string
Type FieldType
Tag int
AutoTag int
Repeatd bool
MainIndex *FieldDescriptor
Complex *Descriptor
Struct *Descriptor
}
func (self *FieldDescriptor) IsExtendType() bool {
switch self.Type {
case FieldType_Float32,
FieldType_Float64:
return true
}
return false
}
func (self *FieldDescriptor) ExtendTypePrecision() int {
var precision int = 1000
if precisionStr, ok := self.CommentGroup.MatchTag("ExtendPrecision"); ok {
if pcs, err := strconv.ParseInt(precisionStr, 10, 64); err == nil {
precision = int(pcs)
}
}
switch self.Type {
case FieldType_Float32,
FieldType_Float64:
return precision
}
return 0
}
func (self *FieldDescriptor) TagNumber() int {
var tag int
if self.AutoTag == -1 {
tag = self.Tag
} else {
tag = self.AutoTag
}
if tag != 0 {
return self.Struct.TagBase + tag
}
return tag
}
func (self *FieldDescriptor) TypeString() string {
return self.typeStr(false)
}
func (self *FieldDescriptor) CompatibleTypeString() string {
return self.typeStr(true)
}
func (self *FieldDescriptor) typeStr(compatible bool) (ret string) {
if self.Repeatd {
if compatible {
ret = "*"
} else {
ret = "[]"
}
}
if compatible {
ret += self.CompatibleTypeName()
} else {
ret += self.TypeName()
}
if self.MainIndex != nil {
ret += fmt.Sprintf("(%s)", self.MainIndex.Name)
}
return
}
func (self *FieldDescriptor) String() string {
return fmt.Sprintf("%s %d : %s", self.Name, self.TagNumber(), self.TypeString())
}
func (self *FieldDescriptor) Kind() string {
return self.Type.String()
}
func (self *FieldDescriptor) CompatibleTypeName() string {
switch self.Type {
case FieldType_Struct:
return self.Complex.Name
case FieldType_Int32,
FieldType_Int64,
FieldType_UInt32,
FieldType_UInt64,
FieldType_Float32,
FieldType_Float64,
FieldType_Enum:
return FieldType_Integer.String()
case FieldType_Bytes:
return FieldType_String.String()
default:
return self.Type.String()
}
}
func (self *FieldDescriptor) TypeName() string {
switch self.Type {
case FieldType_Bool:
return "bool"
case FieldType_Struct, FieldType_Enum:
return self.Complex.Name
default:
return self.Type.String()
}
}
func (self *FieldDescriptor) parseType(name string) (ft FieldType, structType *Descriptor) {
ft = ParseFieldType(name)
if ft != FieldType_None {
return ft, nil
}
if ft, structType = self.Struct.File.FileSet.parseType(name); ft != FieldType_None {
return ft, structType
}
return FieldType_None, nil
}
func newFieldDescriptor(d *Descriptor) *FieldDescriptor {
return &FieldDescriptor{
CommentGroup: newCommentGroup(),
Struct: d,
AutoTag: -1,
}
}