-
Notifications
You must be signed in to change notification settings - Fork 6
/
deduplicate.go
125 lines (115 loc) · 2.81 KB
/
deduplicate.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
package main
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
func FindRedeclaredInPkg(packagePath string, exclude string) (result []string, err error) {
if _, err = os.Stat(packagePath); os.IsNotExist(err) {
err = os.MkdirAll(packagePath, 0755)
if err != nil {
return nil, err
}
}
// Get all '*_validate.pb.go' files under the package
filePaths, err := getGoFilesInPackage(packagePath)
if err != nil {
return nil, err
}
for _, filePath := range filePaths {
if strings.Contains(filePath, exclude) {
continue
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil {
println("err", err.Error())
return nil, err
}
ast.Inspect(f, func(n ast.Node) bool {
switch decl := n.(type) {
case *ast.GenDecl:
// process declaration statements (variables or constants)
for _, spec := range decl.Specs {
if vSpec, ok := spec.(*ast.ValueSpec); ok {
for _, ident := range vSpec.Names {
name := ident.Name
switch name {
case "_uuidPattern":
result = append(result, "_uuidPattern")
}
}
}
}
case *ast.FuncDecl:
// process function declaration
name := decl.Name.Name
switch name {
case "_validateUuid":
result = append(result, "_validateUuid")
case "_validateHostname":
result = append(result, "_validateHostname")
case "_validateEmail":
result = append(result, "_validateEmail")
}
case *ast.TypeSpec:
// process type declaration
// check if it is a structure declaration
if _, ok := decl.Type.(*ast.StructType); ok {
// Get the name of the structure
structName := decl.Name.Name
if structName == "ValidationError" {
result = append(result, "ValidationError")
return false
}
}
}
return true
})
}
return result, nil
}
func getGoFilesInPackage(packagePath string) ([]string, error) {
var filePaths []string
absPath, err := filepath.Abs(packagePath)
if err != nil {
return nil, err
}
var curDir string
err = filepath.Walk(absPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dir := filepath.Dir(path)
if curDir != "" && curDir != dir {
return nil
}
if strings.HasSuffix(path, "_validate.pb.go") {
curDir = filepath.Dir(path)
filePaths = append(filePaths, path)
}
return nil
})
if err != nil {
return nil, err
}
return filePaths, nil
}
func isRelativePath(parameter string) bool {
var isRelative bool
for _, param := range strings.Split(parameter, ",") {
var value string
if i := strings.Index(param, "="); i >= 0 {
value = param[i+1:]
param = param[0:i]
}
if param == "paths" && value == "source_relative" {
isRelative = true
return isRelative
}
}
return isRelative
}