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

Function GetAttr

ops.go:266–393  ·  view source on GitHub ↗

GetAttr is a helper function that performs the same operation as the attribute access in the HCL expression language. That is, the result is the same as it would be for obj.attr in a configuration expression. This is exported so that applications can access attributes in a manner consistent with ho

(obj cty.Value, attrName string, srcRange *Range)

Source from the content-addressed store, hash-verified

264// though nil can be provided if the calling application is going to
265// ignore the subject of the returned diagnostics anyway.
266func GetAttr(obj cty.Value, attrName string, srcRange *Range) (cty.Value, Diagnostics) {
267 if obj.IsNull() {
268 return cty.DynamicVal, Diagnostics{
269 {
270 Severity: DiagError,
271 Summary: "Attempt to get attribute from null value",
272 Detail: "This value is null, so it does not have any attributes.",
273 Subject: srcRange,
274 },
275 }
276 }
277
278 const unsupportedAttr = "Unsupported attribute"
279
280 ty := obj.Type()
281 switch {
282 case ty.IsObjectType():
283 if !ty.HasAttribute(attrName) {
284 return cty.DynamicVal, Diagnostics{
285 {
286 Severity: DiagError,
287 Summary: unsupportedAttr,
288 Detail: fmt.Sprintf("This object does not have an attribute named %q.", attrName),
289 Subject: srcRange,
290 },
291 }
292 }
293
294 if !obj.IsKnown() {
295 return cty.UnknownVal(ty.AttributeType(attrName)).WithSameMarks(obj), nil
296 }
297
298 return obj.GetAttr(attrName), nil
299 case ty.IsMapType():
300 if !obj.IsKnown() {
301 return cty.UnknownVal(ty.ElementType()).WithSameMarks(obj), nil
302 }
303
304 idx := cty.StringVal(attrName)
305
306 // Here we drop marks from HasIndex result, in order to allow basic
307 // traversal of a marked map in the same way we can traverse a marked
308 // object
309 hasIndex, _ := obj.HasIndex(idx).Unmark()
310 if hasIndex.False() {
311 return cty.DynamicVal, Diagnostics{
312 {
313 Severity: DiagError,
314 Summary: "Missing map element",
315 Detail: fmt.Sprintf("This map does not have an element with the key %q.", attrName),
316 Subject: srcRange,
317 },
318 }
319 }
320
321 return obj.Index(idx), nil
322 case ty == cty.DynamicPseudoType:
323 return cty.DynamicVal.WithSameMarks(obj), nil

Callers 2

TraversalStepMethod · 0.85
ApplyPathFunction · 0.85

Calls 1

TypeMethod · 0.45

Tested by

no test coverage detected