-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathannotations.go
159 lines (123 loc) · 3.25 KB
/
annotations.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
157
158
159
package docs
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
goFileExt = ".go"
)
type configAnnotation struct {
getWD getWorkingDirFn
}
type (
getWorkingDirFn func() (dir string, err error)
pathWalkerFn func(path string, walker walkerFn) (files []string, err error)
walkerFn func(root string, walkFn filepath.WalkFunc) error
)
// MapAnnotationsInPath scanIn is relevant from initiator calling it.
//
// It accepts the path in which to scan for annotations within Go files.
func (oas *OAS) MapAnnotationsInPath(scanIn string, conf ...configAnnotation) error {
filesInPath, err := scanForChangesInPath(scanIn, getWDFn(conf), walkFilepath)
if err != nil {
return fmt.Errorf(" :%w", err)
}
for _, file := range filesInPath {
err = oas.mapDocAnnotations(file)
if err != nil {
return fmt.Errorf(" :%w", err)
}
}
return nil
}
// scanForChangesInPath scans for annotations changes on handlers in passed path,
// which is relative to the caller's point of view.
func scanForChangesInPath(handlersPath string, getWD getWorkingDirFn, walker pathWalkerFn) (files []string, err error) {
currentPath, err := getWD()
if err != nil {
return files, fmt.Errorf("failed getting current working directory: %w", err)
}
files, err = walker(filepath.Join(currentPath, handlersPath), filepath.Walk)
if err != nil {
return files, fmt.Errorf("failed walking tree of the given path: %w", err)
}
return files, nil
}
func (ca configAnnotation) getCurrentDirFetcher() getWorkingDirFn {
if ca.getWD != nil {
return ca.getWD
}
return os.Getwd
}
func getWDFn(configs []configAnnotation) getWorkingDirFn {
if len(configs) != 0 {
return configs[0].getCurrentDirFetcher()
}
return os.Getwd
}
func walkFilepath(pathToTraverse string, walker walkerFn) ([]string, error) {
var files []string
walkFn := func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != goFileExt {
return nil
}
files = append(files, path)
return nil
}
err := walker(pathToTraverse, walkFn)
if err != nil {
return files, err
}
return files, nil
}
func (oas *OAS) mapDocAnnotations(path string) error {
if oas == nil {
return errors.New("pointer to OASHandlers can not be nil")
}
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file in path %s :%w", path, err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
line := 1
for scanner.Scan() {
mapIfLineContainsOASTag(scanner.Text(), oas)
line++
}
err = scanner.Err()
if err != nil {
return fmt.Errorf("scanner failure :%w", err)
}
return nil
}
func mapIfLineContainsOASTag(lineText string, o *OAS) {
if strings.Contains(lineText, oasAnnotationInit) {
annotations := oasAnnotations(strings.Fields(lineText))
var newRoute Path
newRoute.HandlerFuncName = annotations.getHandlerFuncName()
newRoute.Route = annotations.getRoute()
newRoute.HTTPMethod = annotations.getHTTPMethod()
o.Paths = append(o.Paths, newRoute)
}
}
type oasAnnotations []string
func (oa oasAnnotations) getHandlerFuncName() string {
return oa[2]
}
func (oa oasAnnotations) getRoute() string {
return oa[3]
}
func (oa oasAnnotations) getHTTPMethod() string {
return oa[4]
}