-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbols.go
156 lines (126 loc) · 3.4 KB
/
symbols.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
package main
import (
"errors"
"strings"
)
// Tag Constants
const (
TagDescription = "description"
TagRoute = "route"
TagModel = "model"
TagReturn = "return"
TagParam = "param"
TagTags = "tags"
TagArgRequired = "required"
TagArgOptional = "optional"
TagArgTransportPrefix = "in:"
GoTypeInt = "int"
SwaggerTypeInt = "integer"
GoTypeString = "string"
SwaggerTypeString = "string"
GoTypeFloat = "float"
SwaggerTypeFloat = "number"
GoTypeBool = "bool"
SwaggerTypeBool = "boolean"
TransportPath = "path"
TransportQuery = "query"
TransportForm = "form"
TransportHeader = "header"
TransportBody = "body"
)
// Tags is a collection of tagName constants
var Tags = []string{
TagDescription,
TagRoute,
TagModel,
TagReturn,
TagParam,
TagTags,
}
// GetSymbols returns a collection of symbol objects based on a symbol string
func GetSymbols(lines []string, symbol string) (symbols []Symbol, err error) {
currentLine := 0
numLines := len(lines)
if numLines == 0 {
err = errors.New("Lines array cannot be empty")
return
}
for {
if currentLine > (numLines - 1) {
break
}
if strings.Contains(lines[currentLine], symbol) {
s := Symbol{
symbol,
currentLine,
lines[currentLine],
[]string{},
map[string][]string{},
"",
}
symbols = append(symbols, s)
}
currentLine = currentLine + 1
}
return
}
// GetCommentBlock parses an entire comment block (comments above a function or struct) and returns them as a string array,
// along with the numeric start and end position of the block
func GetCommentBlock(lines []string, startLine int) (comments []string, blockStart int, blockEnd int) {
currentLine := startLine
// Go backwards
for {
currentLine = currentLine - 1
// TODO support docblock (`*`)
if currentLine < 1 || len(lines[currentLine]) < 3 || lines[currentLine][0:3] != "// " {
// Start of block
blockStart = currentLine + 1
break
}
// prepend
comments = append([]string{lines[currentLine][3:]}, comments...)
}
// Reset currentLine
currentLine = startLine
comments = append(comments, lines[currentLine][3:])
for {
currentLine = currentLine + 1
if len(lines[currentLine]) < 3 || lines[currentLine][0:3] != "// " {
blockEnd = currentLine - 1
break
}
comments = append(comments, lines[currentLine][3:])
}
return
}
func inArray(needle string, haystack []string) bool {
for _, el := range haystack {
if el == needle {
return true
}
}
return false
}
// ParseSymbols looks for comment tags (starts with `@`) and returns what it finds as a multi-dimensional array
func ParseSymbols(lines []string) (tags map[string][]string) {
tags = map[string][]string{}
for _, line := range lines {
if !strings.HasPrefix(line, "@") {
continue
}
lineParts := strings.Fields(line)
// Remove the `@` symbol
tagName := lineParts[0][1:]
if !inArray(tagName, Tags) {
continue
}
if _, ok := tags[tagName]; !ok {
tags[tagName] = []string{}
}
tags[tagName] = append(tags[tagName], strings.Join(lineParts[1:], " "))
}
return
}
// TODO scan entire package for files
// TODO reference other models from within a model
// TODO allow for models (@model) and routes (@route) to be identified by their tags -- allow them to live in the same file.