addrs: More string parsing helpers for addresses
Our main "parse" methods in this package work with hcl.Traversals, but we're gradually adding helpers to parse these directly froms strings since the visual noise of doing the traversal parse first is inconvenient in situations where addresses are coming from non-config locations where no source information is available anyway.
This commit is contained in:
parent
00199cd2ed
commit
ba894ee05c
|
@ -4,11 +4,11 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hcl2/hcl"
|
||||||
|
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
"github.com/zclconf/go-cty/cty/gocty"
|
"github.com/zclconf/go-cty/cty/gocty"
|
||||||
|
|
||||||
"github.com/zclconf/go-cty/cty"
|
|
||||||
|
|
||||||
"github.com/hashicorp/hcl2/hcl"
|
|
||||||
"github.com/hashicorp/terraform/tfdiags"
|
"github.com/hashicorp/terraform/tfdiags"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,6 +48,35 @@ func ParseModuleInstance(traversal hcl.Traversal) (ModuleInstance, tfdiags.Diagn
|
||||||
return mi, diags
|
return mi, diags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseModuleInstanceStr is a helper wrapper around ParseModuleInstance
|
||||||
|
// 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 reference 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
|
||||||
|
// ParseProviderConfigCompact.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
// then the returned address is invalid.
|
||||||
|
func ParseModuleInstanceStr(str string) (ModuleInstance, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
addr, addrDiags := ParseModuleInstance(traversal)
|
||||||
|
diags = diags.Append(addrDiags)
|
||||||
|
return addr, diags
|
||||||
|
}
|
||||||
|
|
||||||
func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Traversal, tfdiags.Diagnostics) {
|
func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Traversal, tfdiags.Diagnostics) {
|
||||||
remain := traversal
|
remain := traversal
|
||||||
var mi ModuleInstance
|
var mi ModuleInstance
|
||||||
|
|
|
@ -75,6 +75,35 @@ func ParseProviderConfigCompact(traversal hcl.Traversal) (ProviderConfig, tfdiag
|
||||||
return ret, diags
|
return ret, diags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseProviderConfigCompactStr is a helper wrapper around ParseProviderConfigCompact
|
||||||
|
// 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 reference 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
|
||||||
|
// ParseProviderConfigCompact.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
// then the returned address is invalid.
|
||||||
|
func ParseProviderConfigCompactStr(str string) (ProviderConfig, 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 ProviderConfig{}, diags
|
||||||
|
}
|
||||||
|
|
||||||
|
addr, addrDiags := ParseProviderConfigCompact(traversal)
|
||||||
|
diags = diags.Append(addrDiags)
|
||||||
|
return addr, diags
|
||||||
|
}
|
||||||
|
|
||||||
// Absolute returns an AbsProviderConfig from the receiver and the given module
|
// Absolute returns an AbsProviderConfig from the receiver and the given module
|
||||||
// instance address.
|
// instance address.
|
||||||
func (pc ProviderConfig) Absolute(module ModuleInstance) AbsProviderConfig {
|
func (pc ProviderConfig) Absolute(module ModuleInstance) AbsProviderConfig {
|
||||||
|
|
Loading…
Reference in New Issue