|
5 | 5 | "go/ast"
|
6 | 6 | "go/parser"
|
7 | 7 | "go/token"
|
8 |
| - "io/fs" |
9 | 8 | "strconv"
|
10 | 9 | "strings"
|
11 | 10 | "testing"
|
@@ -52,49 +51,47 @@ func TestClusterExtensionReasonRegistration(t *testing.T) {
|
52 | 51 | }
|
53 | 52 | }
|
54 | 53 |
|
55 |
| -// parseConstants parses the values of the top-level constants in the current |
56 |
| -// directory whose names start with the given prefix. When running as part of a |
57 |
| -// test, the current directory is the directory of the file that contains the |
58 |
| -// test in which this function is called. |
| 54 | +// parseConstants parses the values of the top-level constants that start with the given prefix, |
| 55 | +// in the files clusterextension_types.go and common_types.go. |
59 | 56 | func parseConstants(prefix string) ([]string, error) {
|
60 | 57 | fset := token.NewFileSet()
|
61 |
| - // ParseDir returns a map of package name to package ASTs. An AST is a representation of the source code |
62 |
| - // that can be traversed to extract information. The map is keyed by the package name. |
63 |
| - pkgs, err := parser.ParseDir(fset, ".", func(info fs.FileInfo) bool { |
64 |
| - return !strings.HasSuffix(info.Name(), "_test.go") |
65 |
| - }, 0) |
66 |
| - if err != nil { |
67 |
| - return nil, err |
| 58 | + // An AST is a representation of the source code that can be traversed to extract information. |
| 59 | + // Converting files to AST representation to extract information. |
| 60 | + parseFiles, astFiles := []string{"clusterextension_types.go", "common_types.go"}, []*ast.File{} |
| 61 | + for _, file := range parseFiles { |
| 62 | + p, err := parser.ParseFile(fset, file, nil, 0) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + astFiles = append(astFiles, p) |
68 | 67 | }
|
69 | 68 |
|
70 | 69 | var constValues []string
|
71 | 70 |
|
72 |
| - // Iterate all of the top-level declarations in each package's files, |
73 |
| - // looking for constants that start with the prefix. When we find one, |
74 |
| - // add its value to the constValues list. |
75 |
| - for _, pkg := range pkgs { |
76 |
| - for _, f := range pkg.Files { |
77 |
| - for _, d := range f.Decls { |
78 |
| - genDecl, ok := d.(*ast.GenDecl) |
79 |
| - if !ok { |
| 71 | + // Iterate all of the top-level declarations in each file, looking |
| 72 | + // for constants that start with the prefix. When we find one, add |
| 73 | + // its value to the constValues list. |
| 74 | + for _, f := range astFiles { |
| 75 | + for _, d := range f.Decls { |
| 76 | + genDecl, ok := d.(*ast.GenDecl) |
| 77 | + if !ok { |
| 78 | + continue |
| 79 | + } |
| 80 | + for _, s := range genDecl.Specs { |
| 81 | + valueSpec, ok := s.(*ast.ValueSpec) |
| 82 | + if !ok || len(valueSpec.Names) != 1 || valueSpec.Names[0].Obj.Kind != ast.Con || !strings.HasPrefix(valueSpec.Names[0].String(), prefix) { |
80 | 83 | continue
|
81 | 84 | }
|
82 |
| - for _, s := range genDecl.Specs { |
83 |
| - valueSpec, ok := s.(*ast.ValueSpec) |
84 |
| - if !ok || len(valueSpec.Names) != 1 || valueSpec.Names[0].Obj.Kind != ast.Con || !strings.HasPrefix(valueSpec.Names[0].String(), prefix) { |
| 85 | + for _, val := range valueSpec.Values { |
| 86 | + lit, ok := val.(*ast.BasicLit) |
| 87 | + if !ok || lit.Kind != token.STRING { |
85 | 88 | continue
|
86 | 89 | }
|
87 |
| - for _, val := range valueSpec.Values { |
88 |
| - lit, ok := val.(*ast.BasicLit) |
89 |
| - if !ok || lit.Kind != token.STRING { |
90 |
| - continue |
91 |
| - } |
92 |
| - v, err := strconv.Unquote(lit.Value) |
93 |
| - if err != nil { |
94 |
| - return nil, fmt.Errorf("unquote literal string %s: %v", lit.Value, err) |
95 |
| - } |
96 |
| - constValues = append(constValues, v) |
| 90 | + v, err := strconv.Unquote(lit.Value) |
| 91 | + if err != nil { |
| 92 | + return nil, fmt.Errorf("unquote literal string %s: %v", lit.Value, err) |
97 | 93 | }
|
| 94 | + constValues = append(constValues, v) |
98 | 95 | }
|
99 | 96 | }
|
100 | 97 | }
|
|
0 commit comments