forked from pseudomuto/protoc-gen-doc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.go
80 lines (74 loc) · 1.85 KB
/
content.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
package gendoc
import (
"strings"
)
// LinkedMessageField - Tree type object the maps graph between fields of proto messages
type LinkedMessageField struct {
Self *MessageField
Children []*LinkedMessageField
}
func printLinkedMessageField(f *LinkedMessageField) {
println(f.Self.Name)
if f.Children != nil {
for _, child := range f.Children {
print(child)
}
}
}
// GetContent parses file data and returns tree structured list of LinkedMessageField
func GetContent(files []*File, baseMessage string) []*LinkedMessageField {
var linkedFields []*LinkedMessageField
for _, file := range files {
for _, message := range file.Messages {
if strings.ToLower(message.LongName) == strings.ToLower(baseMessage) {
for _, field := range message.Fields {
linkedField := &LinkedMessageField{Self: field}
if !isScalarType(field.LongType) {
getChildField(files, linkedField)
}
linkedFields = append(linkedFields, linkedField)
}
}
}
}
return linkedFields
}
func getChildField(files []*File, parentLinkedField *LinkedMessageField) {
for _, file := range files {
for _, message := range file.Messages {
if message.FullName == parentLinkedField.Self.FullType {
for _, field := range message.Fields {
linkedField := &LinkedMessageField{Self: field}
if !isScalarType(field.LongType) {
getChildField(files, linkedField)
}
if parentLinkedField.Children == nil {
parentLinkedField.Children = []*LinkedMessageField{}
}
parentLinkedField.Children = append(parentLinkedField.Children, linkedField)
}
}
}
}
}
func isScalarType(fieldType string) bool {
switch strings.ToLower(fieldType) {
case
"double",
"float",
"int32",
"int64",
"uint32",
"uint64",
"sint32",
"sint64",
"fixed32",
"sfixed32",
"sfixed64",
"bool",
"string",
"bytes":
return true
}
return false
}