@ -3,6 +3,8 @@ package addrs
import (
"fmt"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/terraform/tfdiags"
)
@ -138,6 +140,35 @@ func ParseTarget(traversal hcl.Traversal) (*Target, tfdiags.Diagnostics) {
} , diags
}
// ParseTargetStr is a helper wrapper around ParseTarget that takes a string
// and parses it with the HCL native syntax traversal parser before
// interpreting it.
//
// This should be used only in specialized situations since it will cause the
// created references to not have any meaningful source location information.
// If a target string is coming from a source that should be identified in
// error messages then the caller should instead parse it directly using a
// suitable function from the HCL API and pass the traversal itself to
// ParseTarget.
//
// Error diagnostics are returned if either the parsing fails or the analysis
// of the traversal fails. There is no way for the caller to distinguish the
// two kinds of diagnostics programmatically. If error diagnostics are returned
// the returned target may be nil or incomplete.
func ParseTargetStr ( str string ) ( * Target , tfdiags . Diagnostics ) {
var diags tfdiags . Diagnostics
traversal , parseDiags := hclsyntax . ParseTraversalAbs ( [ ] byte ( str ) , "" , hcl . Pos { Line : 1 , Column : 1 } )
diags = diags . Append ( parseDiags )
if parseDiags . HasErrors ( ) {
return nil , diags
}
target , targetDiags := ParseTarget ( traversal )
diags = diags . Append ( targetDiags )
return target , diags
}
// ParseAbsResource attempts to interpret the given traversal as an absolute
// resource address, using the same syntax as expected by ParseTarget.
//
@ -190,6 +221,32 @@ func ParseAbsResource(traversal hcl.Traversal) (AbsResource, tfdiags.Diagnostics
}
}
// ParseAbsResourceStr is a helper wrapper around ParseAbsResource that takes a
// string and parses it with the HCL native syntax traversal parser before
// interpreting it.
//
// Error diagnostics are returned if either the parsing fails or the analysis
// of the traversal fails. There is no way for the caller to distinguish the
// two kinds of diagnostics programmatically. If error diagnostics are returned
// the returned address may be incomplete.
//
// Since this function has no context about the source of the given string,
// any returned diagnostics will not have meaningful source location
// information.
func ParseAbsResourceStr ( str string ) ( AbsResource , tfdiags . Diagnostics ) {
var diags tfdiags . Diagnostics
traversal , parseDiags := hclsyntax . ParseTraversalAbs ( [ ] byte ( str ) , "" , hcl . Pos { Line : 1 , Column : 1 } )
diags = diags . Append ( parseDiags )
if parseDiags . HasErrors ( ) {
return AbsResource { } , diags
}
addr , addrDiags := ParseAbsResource ( traversal )
diags = diags . Append ( addrDiags )
return addr , diags
}
// ParseAbsResourceInstance attempts to interpret the given traversal as an
// absolute resource instance address, using the same syntax as expected by
// ParseTarget.
@ -233,3 +290,29 @@ func ParseAbsResourceInstance(traversal hcl.Traversal) (AbsResourceInstance, tfd
}
}
// ParseAbsResourceInstanceStr is a helper wrapper around
// ParseAbsResourceInstance that takes a string and parses it with the HCL
// native syntax traversal parser before interpreting it.
//
// Error diagnostics are returned if either the parsing fails or the analysis
// of the traversal fails. There is no way for the caller to distinguish the
// two kinds of diagnostics programmatically. If error diagnostics are returned
// the returned address may be incomplete.
//
// Since this function has no context about the source of the given string,
// any returned diagnostics will not have meaningful source location
// information.
func ParseAbsResourceInstanceStr ( str string ) ( AbsResourceInstance , tfdiags . Diagnostics ) {
var diags tfdiags . Diagnostics
traversal , parseDiags := hclsyntax . ParseTraversalAbs ( [ ] byte ( str ) , "" , hcl . Pos { Line : 1 , Column : 1 } )
diags = diags . Append ( parseDiags )
if parseDiags . HasErrors ( ) {
return AbsResourceInstance { } , diags
}
addr , addrDiags := ParseAbsResourceInstance ( traversal )
diags = diags . Append ( addrDiags )
return addr , diags
}