-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocutil.go
More file actions
43 lines (34 loc) · 778 Bytes
/
docutil.go
File metadata and controls
43 lines (34 loc) · 778 Bytes
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
package validate
import (
"regexp"
"strings"
)
var (
reHeader = regexp.MustCompile(`^\s@(\w+):\s*$`)
reOption = regexp.MustCompile(`^\s{2,}(\w+):\s*(.+)$`)
)
type Option struct{ K, V string }
func ParseDoc(comments []string) map[string][]Option {
annos := make(map[string][]Option)
var headerName string
for _, comment := range comments {
c := strings.TrimPrefix(comment, "//")
result := reHeader.FindAllStringSubmatch(c, -1)
if len(result) > 0 {
headerName = result[0][1]
continue
}
result = reOption.FindAllStringSubmatch(c, -1)
if len(result) > 0 {
if headerName != "" {
annos[headerName] = append(annos[headerName], Option{
K: result[0][1],
V: result[0][2],
})
}
continue
}
headerName = ""
}
return annos
}