diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_vars_gen.go')
-rw-r--r-- | vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_vars_gen.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_vars_gen.go b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_vars_gen.go new file mode 100644 index 0000000..88f1980 --- /dev/null +++ b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_vars_gen.go | |||
@@ -0,0 +1,99 @@ | |||
1 | // This is a 'go generate'-oriented program for producing the "Variables" | ||
2 | // method on every Expression implementation found within this package. | ||
3 | // All expressions share the same implementation for this method, which | ||
4 | // just wraps the package-level function "Variables" and uses an AST walk | ||
5 | // to do its work. | ||
6 | |||
7 | // +build ignore | ||
8 | |||
9 | package main | ||
10 | |||
11 | import ( | ||
12 | "fmt" | ||
13 | "go/ast" | ||
14 | "go/parser" | ||
15 | "go/token" | ||
16 | "os" | ||
17 | "sort" | ||
18 | ) | ||
19 | |||
20 | func main() { | ||
21 | fs := token.NewFileSet() | ||
22 | pkgs, err := parser.ParseDir(fs, ".", nil, 0) | ||
23 | if err != nil { | ||
24 | fmt.Fprintf(os.Stderr, "error while parsing: %s\n", err) | ||
25 | os.Exit(1) | ||
26 | } | ||
27 | pkg := pkgs["hclsyntax"] | ||
28 | |||
29 | // Walk all the files and collect the receivers of any "Value" methods | ||
30 | // that look like they are trying to implement Expression. | ||
31 | var recvs []string | ||
32 | for _, f := range pkg.Files { | ||
33 | for _, decl := range f.Decls { | ||
34 | fd, ok := decl.(*ast.FuncDecl) | ||
35 | if !ok { | ||
36 | continue | ||
37 | } | ||
38 | if fd.Name.Name != "Value" { | ||
39 | continue | ||
40 | } | ||
41 | results := fd.Type.Results.List | ||
42 | if len(results) != 2 { | ||
43 | continue | ||
44 | } | ||
45 | valResult := fd.Type.Results.List[0].Type.(*ast.SelectorExpr).X.(*ast.Ident) | ||
46 | diagsResult := fd.Type.Results.List[1].Type.(*ast.SelectorExpr).X.(*ast.Ident) | ||
47 | |||
48 | if valResult.Name != "cty" && diagsResult.Name != "hcl" { | ||
49 | continue | ||
50 | } | ||
51 | |||
52 | // If we have a method called Value and it returns something in | ||
53 | // "cty" followed by something in "hcl" then that's specific enough | ||
54 | // for now, even though this is not 100% exact as a correct | ||
55 | // implementation of Value. | ||
56 | |||
57 | recvTy := fd.Recv.List[0].Type | ||
58 | |||
59 | switch rtt := recvTy.(type) { | ||
60 | case *ast.StarExpr: | ||
61 | name := rtt.X.(*ast.Ident).Name | ||
62 | recvs = append(recvs, fmt.Sprintf("*%s", name)) | ||
63 | default: | ||
64 | fmt.Fprintf(os.Stderr, "don't know what to do with a %T receiver\n", recvTy) | ||
65 | } | ||
66 | |||
67 | } | ||
68 | } | ||
69 | |||
70 | sort.Strings(recvs) | ||
71 | |||
72 | of, err := os.OpenFile("expression_vars.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) | ||
73 | if err != nil { | ||
74 | fmt.Fprintf(os.Stderr, "failed to open output file: %s\n", err) | ||
75 | os.Exit(1) | ||
76 | } | ||
77 | |||
78 | fmt.Fprint(of, outputPreamble) | ||
79 | for _, recv := range recvs { | ||
80 | fmt.Fprintf(of, outputMethodFmt, recv) | ||
81 | } | ||
82 | fmt.Fprint(of, "\n") | ||
83 | |||
84 | } | ||
85 | |||
86 | const outputPreamble = `package hclsyntax | ||
87 | |||
88 | // Generated by expression_vars_get.go. DO NOT EDIT. | ||
89 | // Run 'go generate' on this package to update the set of functions here. | ||
90 | |||
91 | import ( | ||
92 | "github.com/hashicorp/hcl2/hcl" | ||
93 | )` | ||
94 | |||
95 | const outputMethodFmt = ` | ||
96 | |||
97 | func (e %s) Variables() []hcl.Traversal { | ||
98 | return Variables(e) | ||
99 | }` | ||