// EvalModuleCallArgument produces the value for a particular variable as will
// be used by a child module instance.
//
// The result is written into a map, with its key set to the local name of the
// variable, disregarding the module instance address. A map is returned instead
// of a single value as a result of trying to be convenient for use with
// EvalContext.SetModuleCallArguments, which expects a map to merge in with any
// existing arguments.
//
// validateOnly indicates that this evaluation is only for config
// validation, and we will not have any expansion module instance
// repetition data.
EvalModuleCallArgument produces the value for a particular variable as will
be used by a child module instance.

The result is written into a map, with its key set to the local name of the
variable, disregarding the module instance address. A map is returned instead
of a single value as a result of trying to be convenient for use with
EvalContext.SetModuleCallArguments, which expects a map to merge in with any
existing arguments.

validateOnly indicates that this evaluation is only for config
validation, and we will not have any expansion module instance
repetition data.
// EvalModuleCallArgument produces the value for a particular variable as will // be used by a child module instance . // // The result is written into a map with its key set to the local name of the // variable disregarding the module instance address . A map is returned instead // of a single value as a result of trying to be convenient for use with // EvalContext . SetModuleCallArguments which expects a map to merge in with any // existing arguments . // // validateOnly indicates that this evaluation is only for config // validation and we will not have any expansion module instance // repetition data .
func (n *nodeModuleVariable) EvalModuleCallArgument(ctx EvalContext, validateOnly bool) (map[string]cty.Value, error) {
	wantType := n.Config.Type
	name := n.Addr.Variable.Name
	expr := n.Expr
	if expr == nil {
		log.Printf("[ERROR] attempt to evaluate %s with nil expression", n.Addr.String())
		return nil, nil
	}
	var moduleInstanceRepetitionData instances.RepetitionData
	switch {
	case validateOnly:
		moduleInstanceRepetitionData = instances.RepetitionData{
			CountIndex: cty.UnknownVal(cty.Number),
			EachKey:    cty.UnknownVal(cty.String),
			EachValue:  cty.DynamicVal,
		}
	default:
		moduleInstanceRepetitionData = ctx.InstanceExpander().GetModuleInstanceRepetitionData(n.ModuleInstance)
	}
	scope := ctx.EvaluationScope(nil, moduleInstanceRepetitionData)
	val, diags := scope.EvalExpr(expr, cty.DynamicPseudoType)
	var convErr error
	val, convErr = convert.Convert(val, wantType)
	if convErr != nil {
		diags = diags.Append(&hcl.Diagnostic{
			Severity: hcl.DiagError,
			Summary:  "Invalid value for module argument",
			Detail: fmt.Sprintf(
				"The given value is not suitable for child module variable %q defined at %s: %s.",
				name, n.Config.DeclRange.String(), convErr,
			),
			Subject: expr.Range().Ptr(),
		})
		val = cty.UnknownVal(wantType)
	}
	vals := make(map[string]cty.Value)
	vals[name] = val
	return vals, diags.ErrWithWarnings()
}
func ( n * nodeModuleVariable ) EvalModuleCallArgument ( ctx EvalContext , validateOnly bool ) ( map [ string ] cty . Value , error ) { wantType := n . Config . Type name := n . Addr . Variable . Name expr := n . Expr if expr == nil { log . Printf ( " " , n . Addr . String ( ) ) return nil , nil } var moduleInstanceRepetitionData instances . RepetitionData switch { case validateOnly : moduleInstanceRepetitionData = instances . RepetitionData { CountIndex : cty . UnknownVal ( cty . Number ) , EachKey : cty . UnknownVal ( cty . String ) , EachValue : cty . DynamicVal , } default : moduleInstanceRepetitionData = ctx . InstanceExpander ( ) . GetModuleInstanceRepetitionData ( n . ModuleInstance ) } scope := ctx . EvaluationScope ( nil , moduleInstanceRepetitionData ) val , diags := scope . EvalExpr ( expr , cty . DynamicPseudoType ) var convErr error val , convErr = convert . Convert ( val , wantType ) if convErr != nil { diags = diags . Append ( & hcl . Diagnostic { Severity : hcl . DiagError , Summary : " " , Detail : fmt . Sprintf ( " " , name , n . Config . DeclRange . String ( ) , convErr , ) , Subject : expr . Range ( ) . Ptr ( ) , } ) val = cty . UnknownVal ( wantType ) } vals := make ( map [ string ] cty . Value ) vals [ name ] = val return vals , diags . ErrWithWarnings ( ) }
EvalModuleCallArgument produces the value for a particular variable as will
be used by a child module instance.
EvalModuleCallArgument produces the value for a particular variable as will be used by a child module instance .
// Should never happen, but we'll bail out early here rather than // crash in case it does. We set no value at all in this case, // making a subsequent call to EvalContext.SetModuleCallArguments // a no-op. // the instance expander does not track unknown expansion values, so we // have to assume all RepetitionData is unknown. // Get the repetition data for this module instance, // so we can create the appropriate scope for evaluating our expression // We intentionally passed DynamicPseudoType to EvalExpr above because // now we can do our own local type conversion and produce an error message // with better context if it fails. // We'll return a placeholder unknown value to avoid producing // redundant downstream errors.