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

Method Variables

json/structure.go:516–558  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

514}
515
516func (e *expression) Variables() []hcl.Traversal {
517 var vars []hcl.Traversal
518
519 switch v := e.src.(type) {
520 case *stringVal:
521 templateSrc := v.Value
522 expr, diags := hclsyntax.ParseTemplate(
523 []byte(templateSrc),
524 v.SrcRange.Filename,
525
526 // This won't produce _exactly_ the right result, since
527 // the hclsyntax parser can't "see" any escapes we removed
528 // while parsing JSON, but it's better than nothing.
529 hcl.Pos{
530 Line: v.SrcRange.Start.Line,
531
532 // skip over the opening quote mark
533 Byte: v.SrcRange.Start.Byte + 1,
534 Column: v.SrcRange.Start.Column + 1,
535 },
536 )
537 if diags.HasErrors() {
538 return vars
539 }
540 return expr.Variables()
541
542 case *arrayVal:
543 for _, jsonVal := range v.Values {
544 vars = append(vars, (&expression{src: jsonVal}).Variables()...)
545 }
546 case *objectVal:
547 for _, jsonAttr := range v.Attrs {
548 keyExpr := &stringVal{ // we're going to treat key as an expression in this context
549 Value: jsonAttr.Name,
550 SrcRange: jsonAttr.NameRange,
551 }
552 vars = append(vars, (&expression{src: keyExpr}).Variables()...)
553 vars = append(vars, (&expression{src: jsonAttr.Value}).Variables()...)
554 }
555 }
556
557 return vars
558}
559
560func (e *expression) Range() hcl.Range {
561 return e.src.Range()

Callers

nothing calls this directly

Calls 3

ParseTemplateFunction · 0.92
HasErrorsMethod · 0.80
VariablesMethod · 0.65

Tested by

no test coverage detected