MCPcopy
hub / github.com/hashicorp/hcl / Value

Method Value

hclsyntax/expression.go:1191–1265  ·  view source on GitHub ↗
(ctx *hcl.EvalContext)

Source from the content-addressed store, hash-verified

1189}
1190
1191func (e *ObjectConsExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
1192 var vals map[string]cty.Value
1193 var diags hcl.Diagnostics
1194 var marks []cty.ValueMarks
1195
1196 // This will get set to true if we fail to produce any of our keys,
1197 // either because they are actually unknown or if the evaluation produces
1198 // errors. In all of these case we must return DynamicPseudoType because
1199 // we're unable to know the full set of keys our object has, and thus
1200 // we can't produce a complete value of the intended type.
1201 //
1202 // We still evaluate all of the item keys and values to make sure that we
1203 // get as complete as possible a set of diagnostics.
1204 known := true
1205
1206 vals = make(map[string]cty.Value, len(e.Items))
1207 for _, item := range e.Items {
1208 key, keyDiags := item.KeyExpr.Value(ctx)
1209 diags = append(diags, keyDiags...)
1210
1211 val, valDiags := item.ValueExpr.Value(ctx)
1212 diags = append(diags, valDiags...)
1213
1214 if keyDiags.HasErrors() {
1215 known = false
1216 continue
1217 }
1218
1219 if key.IsNull() {
1220 diags = append(diags, &hcl.Diagnostic{
1221 Severity: hcl.DiagError,
1222 Summary: "Null value as key",
1223 Detail: "Can't use a null value as a key.",
1224 Subject: item.ValueExpr.Range().Ptr(),
1225 Expression: item.KeyExpr,
1226 EvalContext: ctx,
1227 })
1228 known = false
1229 continue
1230 }
1231
1232 key, keyMarks := key.Unmark()
1233 marks = append(marks, keyMarks)
1234
1235 var err error
1236 key, err = convert.Convert(key, cty.String)
1237 if err != nil {
1238 diags = append(diags, &hcl.Diagnostic{
1239 Severity: hcl.DiagError,
1240 Summary: "Incorrect key type",
1241 Detail: fmt.Sprintf("Can't use this value as a key: %s.", err.Error()),
1242 Subject: item.KeyExpr.Range().Ptr(),
1243 Expression: item.KeyExpr,
1244 EvalContext: ctx,
1245 })
1246 known = false
1247 continue
1248 }

Callers

nothing calls this directly

Calls 5

HasErrorsMethod · 0.80
PtrMethod · 0.80
ValueMethod · 0.65
RangeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected