-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessage.go
74 lines (60 loc) · 1.61 KB
/
message.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
package protostub
import (
"fmt"
"github.com/emicklei/proto"
)
// MessageVisitor is created by the main visitor. It contains a Message with all
// the type data - see types.go.
type MessageVisitor struct {
ProtoData
message *Message
}
// NewMessageVisitor creates a new message visitor. `extend` indicates if this
// is an extension, and comment contains the comment attached to this message.
func NewMessageVisitor(name string, extend bool, comment *proto.Comment) *MessageVisitor {
cv := &MessageVisitor{
ProtoData: ProtoData{},
message: &Message{
name: name,
Types: make([]ProtoType, 0),
IsExtend: extend,
Comment: comment.Lines,
},
}
return cv
}
func (mv *MessageVisitor) addMember(m Member) {
mv.message.Members = append(mv.message.Members, m)
}
// VisitNormalField adds the field to the list. Also makes sure primitive types
// are properly translated.
func (mv *MessageVisitor) VisitNormalField(n *proto.NormalField) {
name := n.Name
var typename string
// repeated = it's a list
if !n.Repeated {
typename = TranslateType(n.Type)
} else {
typename = fmt.Sprintf("List[%s]", TranslateType(n.Type))
}
if n.Comment == nil {
n.Comment = &proto.Comment{Lines: make([]string, 0)}
}
mv.addMember(Member{
name: name,
typename: typename,
Comment: n.Comment.Lines,
})
}
// VisitOneof visits a OneOf
func (mv *MessageVisitor) VisitOneof(o *proto.Oneof) {
for _, i := range o.Elements {
i.Accept(mv)
}
}
// VisitOneofField visits a field in a OneOf
func (mv *MessageVisitor) VisitOneofField(o *proto.OneOfField) {
mv.VisitNormalField(&proto.NormalField{
Field: o.Field,
})
}