7 "github.com/hashicorp/hcl2/hcl"
8 "github.com/zclconf/go-cty/cty"
9 "github.com/zclconf/go-cty/cty/convert"
12 type TemplateExpr struct {
18 func (e *TemplateExpr) walkChildNodes(w internalWalkFunc) {
19 for _, part := range e.Parts {
24 func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
25 buf := &bytes.Buffer{}
26 var diags hcl.Diagnostics
29 for _, part := range e.Parts {
30 partVal, partDiags := part.Value(ctx)
31 diags = append(diags, partDiags...)
34 diags = append(diags, &hcl.Diagnostic{
35 Severity: hcl.DiagError,
36 Summary: "Invalid template interpolation value",
38 "The expression result is null. Cannot include a null value in a string template.",
40 Subject: part.Range().Ptr(),
48 if !partVal.IsKnown() {
49 // If any part is unknown then the result as a whole must be
50 // unknown too. We'll keep on processing the rest of the parts
51 // anyway, because we want to still emit any diagnostics resulting
52 // from evaluating those.
57 strVal, err := convert.Convert(partVal, cty.String)
59 diags = append(diags, &hcl.Diagnostic{
60 Severity: hcl.DiagError,
61 Summary: "Invalid template interpolation value",
63 "Cannot include the given value in a string template: %s.",
66 Subject: part.Range().Ptr(),
74 buf.WriteString(strVal.AsString())
78 return cty.UnknownVal(cty.String), diags
81 return cty.StringVal(buf.String()), diags
84 func (e *TemplateExpr) Range() hcl.Range {
88 func (e *TemplateExpr) StartRange() hcl.Range {
89 return e.Parts[0].StartRange()
92 // IsStringLiteral returns true if and only if the template consists only of
93 // single string literal, as would be created for a simple quoted string like
96 // If this function returns true, then calling Value on the same expression
97 // with a nil EvalContext will return the literal value.
99 // Note that "${"foo"}", "${1}", etc aren't considered literal values for the
100 // purposes of this method, because the intent of this method is to identify
101 // situations where the user seems to be explicitly intending literal string
102 // interpretation, not situations that result in literals as a technicality
103 // of the template expression unwrapping behavior.
104 func (e *TemplateExpr) IsStringLiteral() bool {
105 if len(e.Parts) != 1 {
108 _, ok := e.Parts[0].(*LiteralValueExpr)
112 // TemplateJoinExpr is used to convert tuples of strings produced by template
113 // constructs (i.e. for loops) into flat strings, by converting the values
114 // tos strings and joining them. This AST node is not used directly; it's
115 // produced as part of the AST of a "for" loop in a template.
116 type TemplateJoinExpr struct {
120 func (e *TemplateJoinExpr) walkChildNodes(w internalWalkFunc) {
124 func (e *TemplateJoinExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
125 tuple, diags := e.Tuple.Value(ctx)
128 // This indicates a bug in the code that constructed the AST.
129 panic("TemplateJoinExpr got null tuple")
131 if tuple.Type() == cty.DynamicPseudoType {
132 return cty.UnknownVal(cty.String), diags
134 if !tuple.Type().IsTupleType() {
135 // This indicates a bug in the code that constructed the AST.
136 panic("TemplateJoinExpr got non-tuple tuple")
138 if !tuple.IsKnown() {
139 return cty.UnknownVal(cty.String), diags
142 buf := &bytes.Buffer{}
143 it := tuple.ElementIterator()
145 _, val := it.Element()
148 diags = append(diags, &hcl.Diagnostic{
149 Severity: hcl.DiagError,
150 Summary: "Invalid template interpolation value",
152 "An iteration result is null. Cannot include a null value in a string template.",
154 Subject: e.Range().Ptr(),
160 if val.Type() == cty.DynamicPseudoType {
161 return cty.UnknownVal(cty.String), diags
163 strVal, err := convert.Convert(val, cty.String)
165 diags = append(diags, &hcl.Diagnostic{
166 Severity: hcl.DiagError,
167 Summary: "Invalid template interpolation value",
169 "Cannot include one of the interpolation results into the string template: %s.",
172 Subject: e.Range().Ptr(),
179 return cty.UnknownVal(cty.String), diags
182 buf.WriteString(strVal.AsString())
185 return cty.StringVal(buf.String()), diags
188 func (e *TemplateJoinExpr) Range() hcl.Range {
189 return e.Tuple.Range()
192 func (e *TemplateJoinExpr) StartRange() hcl.Range {
193 return e.Tuple.StartRange()
196 // TemplateWrapExpr is used instead of a TemplateExpr when a template
197 // consists _only_ of a single interpolation sequence. In that case, the
198 // template's result is the single interpolation's result, verbatim with
199 // no type conversions.
200 type TemplateWrapExpr struct {
206 func (e *TemplateWrapExpr) walkChildNodes(w internalWalkFunc) {
210 func (e *TemplateWrapExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
211 return e.Wrapped.Value(ctx)
214 func (e *TemplateWrapExpr) Range() hcl.Range {
218 func (e *TemplateWrapExpr) StartRange() hcl.Range {