Skip to content

Commit 9bf8cad

Browse files
authored
Add lang.ReferencesInExpr (#180)
1 parent 2d14952 commit 9bf8cad

23 files changed

+1662
-5
lines changed

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ module github.com/terraform-linters/tflint-plugin-sdk
33
go 1.19
44

55
require (
6+
github.com/go-test/deep v1.0.3
67
github.com/google/go-cmp v0.5.8
78
github.com/hashicorp/go-hclog v1.2.2
89
github.com/hashicorp/go-plugin v1.4.4
910
github.com/hashicorp/hcl/v2 v2.13.0
1011
github.com/zclconf/go-cty v1.10.0
12+
golang.org/x/tools v0.1.11
1113
google.golang.org/grpc v1.48.0
1214
google.golang.org/protobuf v1.28.0
1315
)
@@ -25,9 +27,10 @@ require (
2527
github.com/oklog/run v1.0.0 // indirect
2628
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
2729
github.com/vmihailenco/tagparser v0.1.1 // indirect
28-
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
30+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
31+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
2932
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
30-
golang.org/x/text v0.3.6 // indirect
33+
golang.org/x/text v0.3.7 // indirect
3134
google.golang.org/appengine v1.6.5 // indirect
3235
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
3336
)

go.sum

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
2828
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
2929
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
3030
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
31+
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
3132
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
3233
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
3334
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -107,6 +108,8 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
107108
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
108109
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
109110
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
111+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
112+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
110113
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
111114
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
112115
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -116,8 +119,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
116119
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
117120
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
118121
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
119-
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
120122
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
123+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY=
124+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
121125
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
122126
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
123127
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -140,13 +144,15 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
140144
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
141145
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
142146
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
143-
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
144-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
147+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
148+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
145149
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
146150
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
147151
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
148152
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
149153
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
154+
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
155+
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
150156
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
151157
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
152158
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=

terraform/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Forked Terraform packages
2+
3+
This directory contains a subset of code from Terraform's internal packages. However, the implementation is not exactly the same, it is just a fork, and simplifications and changes have been made according to our project.
4+
5+
This package provides functionality for static analysis of Terraform Language.

terraform/addrs/count_attr.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package addrs
2+
3+
// CountAttr is the address of an attribute of the "count" object in
4+
// the interpolation scope, like "count.index".
5+
type CountAttr struct {
6+
referenceable
7+
Name string
8+
}
9+
10+
func (ca CountAttr) String() string {
11+
return "count." + ca.Name
12+
}

terraform/addrs/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Package addrs is a fork of Terraform's internal/addrs package.
2+
//
3+
// This package contains reference addresses in the Terraform Language
4+
// and implementations that parse hcl.Traversal to produce references.
5+
package addrs

terraform/addrs/for_each_attr.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package addrs
2+
3+
// ForEachAttr is the address of an attribute referencing the current "for_each" object in
4+
// the interpolation scope, addressed using the "each" keyword, ex. "each.key" and "each.value"
5+
type ForEachAttr struct {
6+
referenceable
7+
Name string
8+
}
9+
10+
func (f ForEachAttr) String() string {
11+
return "each." + f.Name
12+
}

terraform/addrs/input_variable.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package addrs
2+
3+
// InputVariable is the address of an input variable.
4+
type InputVariable struct {
5+
referenceable
6+
Name string
7+
}
8+
9+
func (v InputVariable) String() string {
10+
return "var." + v.Name
11+
}

terraform/addrs/instance_key.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package addrs
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/zclconf/go-cty/cty"
7+
"github.com/zclconf/go-cty/cty/gocty"
8+
)
9+
10+
// InstanceKey represents the key of an instance within an object that
11+
// contains multiple instances due to using "count" or "for_each" arguments
12+
// in configuration.
13+
//
14+
// IntKey and StringKey are the two implementations of this type. No other
15+
// implementations are allowed. The single instance of an object that _isn't_
16+
// using "count" or "for_each" is represented by NoKey, which is a nil
17+
// InstanceKey.
18+
type InstanceKey interface {
19+
instanceKeySigil()
20+
String() string
21+
22+
// Value returns the cty.Value of the appropriate type for the InstanceKey
23+
// value.
24+
Value() cty.Value
25+
}
26+
27+
// ParseInstanceKey returns the instance key corresponding to the given value,
28+
// which must be known and non-null.
29+
//
30+
// If an unknown or null value is provided then this function will panic. This
31+
// function is intended to deal with the values that would naturally be found
32+
// in a hcl.TraverseIndex, which (when parsed from source, at least) can never
33+
// contain unknown or null values.
34+
func ParseInstanceKey(key cty.Value) (InstanceKey, error) {
35+
switch key.Type() {
36+
case cty.String:
37+
return StringKey(key.AsString()), nil
38+
case cty.Number:
39+
var idx int
40+
err := gocty.FromCtyValue(key, &idx)
41+
return IntKey(idx), err
42+
default:
43+
return NoKey, fmt.Errorf("either a string or an integer is required")
44+
}
45+
}
46+
47+
// NoKey represents the absense of an InstanceKey, for the single instance
48+
// of a configuration object that does not use "count" or "for_each" at all.
49+
var NoKey InstanceKey
50+
51+
// IntKey is the InstanceKey representation representing integer indices, as
52+
// used when the "count" argument is specified or if for_each is used with
53+
// a sequence type.
54+
type IntKey int
55+
56+
func (k IntKey) instanceKeySigil() {
57+
}
58+
59+
func (k IntKey) String() string {
60+
return fmt.Sprintf("[%d]", int(k))
61+
}
62+
63+
// Value returns a cty.Value of type Number
64+
func (k IntKey) Value() cty.Value {
65+
return cty.NumberIntVal(int64(k))
66+
}
67+
68+
// StringKey is the InstanceKey representation representing string indices, as
69+
// used when the "for_each" argument is specified with a map or object type.
70+
type StringKey string
71+
72+
func (k StringKey) instanceKeySigil() {
73+
}
74+
75+
func (k StringKey) String() string {
76+
// FIXME: This isn't _quite_ right because Go's quoted string syntax is
77+
// slightly different than HCL's, but we'll accept it for now.
78+
return fmt.Sprintf("[%q]", string(k))
79+
}
80+
81+
// Value returns a cty.Value of type String
82+
func (k StringKey) Value() cty.Value {
83+
return cty.StringVal(string(k))
84+
}
85+
86+
// InstanceKeyType represents the different types of instance key that are
87+
// supported. Usually it is sufficient to simply type-assert an InstanceKey
88+
// value to either IntKey or StringKey, but this type and its values can be
89+
// used to represent the types themselves, rather than specific values
90+
// of those types.
91+
type InstanceKeyType rune
92+
93+
const (
94+
// NoKeyType is a type of NoKey instance key
95+
NoKeyType InstanceKeyType = 0
96+
// IntKeyType is a type of IntKey instance key
97+
IntKeyType InstanceKeyType = 'I'
98+
// StringKeyType is a type of StringKey instance key
99+
StringKeyType InstanceKeyType = 'S'
100+
)

terraform/addrs/local_value.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package addrs
2+
3+
// LocalValue is the address of a local value.
4+
type LocalValue struct {
5+
referenceable
6+
Name string
7+
}
8+
9+
func (v LocalValue) String() string {
10+
return "local." + v.Name
11+
}

terraform/addrs/module_call.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package addrs
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// ModuleCall is the address of a call from the current module to a child
8+
// module.
9+
type ModuleCall struct {
10+
referenceable
11+
Name string
12+
}
13+
14+
func (c ModuleCall) String() string {
15+
return "module." + c.Name
16+
}
17+
18+
// ModuleCallInstance is the address of one instance of a module created from
19+
// a module call, which might create multiple instances using "count" or
20+
// "for_each" arguments.
21+
type ModuleCallInstance struct {
22+
referenceable
23+
Call ModuleCall
24+
Key InstanceKey
25+
}
26+
27+
func (c ModuleCallInstance) String() string {
28+
if c.Key == NoKey {
29+
return c.Call.String()
30+
}
31+
return fmt.Sprintf("module.%s%s", c.Call.Name, c.Key)
32+
}
33+
34+
// ModuleCallInstanceOutput is the address of a particular named output produced by
35+
// an instance of a module call.
36+
type ModuleCallInstanceOutput struct {
37+
referenceable
38+
Call ModuleCallInstance
39+
Name string
40+
}
41+
42+
func (co ModuleCallInstanceOutput) String() string {
43+
return fmt.Sprintf("%s.%s", co.Call.String(), co.Name)
44+
}

0 commit comments

Comments
 (0)