core: BuiltinEvalContext constructs evaluationStateData itself

Previously our evaluationStateData object was constructed inside
Evaluator.Scope, but this was awkward because all of the fields inside it
need to be populated from BuiltinEvalContext fields, and so the signature
of Evaluator.Scope kept growing new arguments over time.

Instead, we reassign the responsibilities here so that Evaluator.Scope
takes an already-constructed lang.Data, and then teach BuiltinEvalContext
to build this object itself from its own internal values.
This commit is contained in:
Martin Atkins 2018-05-03 17:24:49 -07:00
parent b0435cd533
commit 4b5868f653
2 changed files with 14 additions and 7 deletions

View File

@ -294,7 +294,8 @@ func (ctx *BuiltinEvalContext) CloseProvisioner(n string) error {
func (ctx *BuiltinEvalContext) EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, key addrs.InstanceKey) (cty.Value, hcl.Body, tfdiags.Diagnostics) { func (ctx *BuiltinEvalContext) EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, key addrs.InstanceKey) (cty.Value, hcl.Body, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
scope := ctx.Evaluator.Scope(ctx.PathValue, self, key) evalData := ctx.evaluationStateData(key)
scope := ctx.Evaluator.Scope(evalData, self)
body, evalDiags := scope.ExpandBlock(body, schema) body, evalDiags := scope.ExpandBlock(body, schema)
diags = diags.Append(evalDiags) diags = diags.Append(evalDiags)
val, evalDiags := scope.EvalBlock(body, schema) val, evalDiags := scope.EvalBlock(body, schema)
@ -303,10 +304,19 @@ func (ctx *BuiltinEvalContext) EvaluateBlock(body hcl.Body, schema *configschema
} }
func (ctx *BuiltinEvalContext) EvaluateExpr(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics) { func (ctx *BuiltinEvalContext) EvaluateExpr(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics) {
scope := ctx.Evaluator.Scope(ctx.PathValue, self, addrs.NoKey) evalData := ctx.evaluationStateData(addrs.NoKey)
scope := ctx.Evaluator.Scope(evalData, self)
return scope.EvalExpr(expr, wantType) return scope.EvalExpr(expr, wantType)
} }
func (ctx *BuiltinEvalContext) evaluationStateData(key addrs.InstanceKey) *evaluationStateData {
return &evaluationStateData{
Evaluator: ctx.Evaluator,
ModulePath: ctx.PathValue,
InstanceKey: key,
}
}
func (ctx *BuiltinEvalContext) Path() addrs.ModuleInstance { func (ctx *BuiltinEvalContext) Path() addrs.ModuleInstance {
return ctx.PathValue return ctx.PathValue
} }

View File

@ -42,12 +42,9 @@ type Evaluator struct {
// If the "self" argument is nil then the "self" object is not available // If the "self" argument is nil then the "self" object is not available
// in evaluated expressions. Otherwise, it behaves as an alias for the given // in evaluated expressions. Otherwise, it behaves as an alias for the given
// address. // address.
func (e *Evaluator) Scope(modulePath addrs.ModuleInstance, self addrs.Referenceable) *lang.Scope { func (e *Evaluator) Scope(data lang.Data, self addrs.Referenceable) *lang.Scope {
return &lang.Scope{ return &lang.Scope{
Data: &evaluationStateData{ Data: data,
Evaluator: e,
ModulePath: modulePath,
},
SelfAddr: self, SelfAddr: self,
PureOnly: e.Operation != walkApply && e.Operation != walkDestroy, PureOnly: e.Operation != walkApply && e.Operation != walkDestroy,
BaseDir: ".", // Always current working directory for now. BaseDir: ".", // Always current working directory for now.