-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstruct.go
109 lines (81 loc) · 1.75 KB
/
struct.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
package meta
import (
"bytes"
"errors"
)
type DescriptorType int
const (
DescriptorType_None DescriptorType = iota
DescriptorType_Enum
DescriptorType_Struct
)
type Descriptor struct {
*CommentGroup
Name string
SrcName string
Type DescriptorType
Fields []*FieldDescriptor
FieldByName map[string]*FieldDescriptor
FieldByTag map[int]*FieldDescriptor
File *FileDescriptor
TagBase int
EnumValueIgnoreType bool
}
func (self *Descriptor) MaxTag() (ret int) {
for _, fd := range self.Fields {
if fd.TagNumber() > ret {
ret = fd.TagNumber()
}
}
return
}
func (self *Descriptor) TypeName() string {
switch self.Type {
case DescriptorType_Enum:
return "enum"
case DescriptorType_Struct:
return "struct"
}
return "none"
}
// c# 要使用的fieldcount
func (self *Descriptor) MaxFieldCount() int {
maxn := len(self.Fields)
lastTag := -1
for _, fd := range self.Fields {
if fd.TagNumber() < lastTag {
panic(errors.New("tag must in ascending order"))
}
if fd.TagNumber() > lastTag+1 {
maxn++
}
lastTag = fd.TagNumber()
}
return maxn
}
func (self *Descriptor) String() string {
var bf bytes.Buffer
bf.WriteString(self.Name)
bf.WriteString(":")
bf.WriteString("\n")
for _, fd := range self.Fields {
bf.WriteString(" ")
bf.WriteString(fd.String())
bf.WriteString("\n")
}
bf.WriteString("\n")
return bf.String()
}
func (self *Descriptor) addField(fd *FieldDescriptor) {
self.Fields = append(self.Fields, fd)
self.FieldByName[fd.Name] = fd
self.FieldByTag[fd.Tag] = fd
}
func newDescriptor(f *FileDescriptor) *Descriptor {
return &Descriptor{
CommentGroup: newCommentGroup(),
File: f,
FieldByName: make(map[string]*FieldDescriptor),
FieldByTag: make(map[int]*FieldDescriptor),
}
}