@@ -20,6 +20,8 @@ var typeObj = map[string]cty.Type{
2020 "nestedTypeLabel" : cty .String ,
2121}
2222
23+ var varFunctions , outputFunctions map [string ]function.Function
24+
2325func nestedTypeFunc (tfType types.TerraformType ) function.Function {
2426 return function .New (& function.Spec {
2527 Params : []function.Parameter {
@@ -78,8 +80,8 @@ func complexTypeFunc(tfType types.TerraformType) function.Function {
7880 })
7981}
8082
81- func getComplexType (expr hcl.Expression ) (entities.Type , error ) {
82- got , exprDiags := expr .Value (getEvalContextForExpr (expr ))
83+ func getComplexType (expr hcl.Expression , ctxFunctions map [ string ]function. Function ) (entities.Type , error ) {
84+ got , exprDiags := expr .Value (getEvalContextForExpr (expr , ctxFunctions ))
8385 if exprDiags .HasErrors () {
8486 return entities.Type {}, fmt .Errorf ("getting expression value: %v" , exprDiags .Errs ())
8587 }
@@ -115,7 +117,7 @@ func getComplexType(expr hcl.Expression) (entities.Type, error) {
115117 }, nil
116118}
117119
118- func getTypeFromExpression (expr hcl.Expression ) (entities.Type , error ) {
120+ func getTypeFromExpression (expr hcl.Expression , ctxFunctions map [ string ]function. Function ) (entities.Type , error ) {
119121 kw := hcl .ExprAsKeyword (expr )
120122
121123 switch kw {
@@ -134,39 +136,51 @@ func getTypeFromExpression(expr hcl.Expression) (entities.Type, error) {
134136 return entities.Type {}, fmt .Errorf ("type %q is invalid" , kw )
135137 }
136138
137- return getComplexType (expr )
139+ return getComplexType (expr , ctxFunctions )
138140}
139141
140142// this function exists to make it possible to parse `type` attribute expressions and `readme_type`
141143// attribute strings in the same way, so they are compatible even though they have different types
142- func getTypeFromString (str string ) (entities.Type , error ) {
144+ func getTypeFromString (str string , ctxFunctions map [ string ]function. Function ) (entities.Type , error ) {
143145 expr , parseDiags := hclsyntax .ParseExpression ([]byte (str ), "" , hcl.Pos {Line : 1 , Column : 1 , Byte : 0 })
144146 if parseDiags .HasErrors () {
145147 return entities.Type {}, fmt .Errorf ("parsing type string expression: %v" , parseDiags .Errs ())
146148 }
147149
148- return getTypeFromExpression (expr )
150+ return getTypeFromExpression (expr , ctxFunctions )
149151}
150152
151153func getVariablesMap (expr hcl.Expression ) map [string ]cty.Value {
152- myMap := make (map [string ]cty.Value )
154+ varMap := make (map [string ]cty.Value )
153155 for _ , variable := range expr .Variables () {
154156 name := variable .RootName ()
155157
156- myMap [name ] = cty .StringVal (name )
158+ varMap [name ] = cty .StringVal (name )
157159 }
158160
159- return myMap
161+ return varMap
160162}
161163
162- func getEvalContextForExpr (expr hcl.Expression ) * hcl.EvalContext {
164+ func getEvalContextForExpr (expr hcl.Expression , ctxFunctions map [ string ]function. Function ) * hcl.EvalContext {
163165 return & hcl.EvalContext {
164- Functions : map [string ]function.Function {
165- "object" : complexTypeFunc (types .TerraformObject ),
166- "map" : nestedTypeFunc (types .TerraformMap ),
167- "list" : nestedTypeFunc (types .TerraformList ),
168- "set" : nestedTypeFunc (types .TerraformSet ),
169- },
166+ Functions : ctxFunctions ,
170167 Variables : getVariablesMap (expr ),
171168 }
172169}
170+
171+ func init () {
172+ varFunctions = map [string ]function.Function {
173+ "object" : complexTypeFunc (types .TerraformObject ),
174+ "map" : nestedTypeFunc (types .TerraformMap ),
175+ "list" : nestedTypeFunc (types .TerraformList ),
176+ "set" : nestedTypeFunc (types .TerraformSet ),
177+ }
178+
179+ outputFunctions = map [string ]function.Function {
180+ "resource" : complexTypeFunc (types .TerraformResource ),
181+ "object" : complexTypeFunc (types .TerraformObject ),
182+ "map" : nestedTypeFunc (types .TerraformMap ),
183+ "list" : nestedTypeFunc (types .TerraformList ),
184+ "set" : nestedTypeFunc (types .TerraformSet ),
185+ }
186+ }
0 commit comments