From ba894ee05c14889f0498836a04841a184aa04962 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 7 Jun 2018 17:20:28 -0700 Subject: [PATCH] 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. --- addrs/module_instance.go | 35 ++++++++++++++++++++++++++++++++--- addrs/provider_config.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/addrs/module_instance.go b/addrs/module_instance.go index 53184dedad..ab15fd7502 100644 --- a/addrs/module_instance.go +++ b/addrs/module_instance.go @@ -4,11 +4,11 @@ import ( "bytes" "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" - - "github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/terraform/tfdiags" ) @@ -48,6 +48,35 @@ func ParseModuleInstance(traversal hcl.Traversal) (ModuleInstance, tfdiags.Diagn 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) { remain := traversal var mi ModuleInstance diff --git a/addrs/provider_config.go b/addrs/provider_config.go index 7fa2ff683e..340dd19e79 100644 --- a/addrs/provider_config.go +++ b/addrs/provider_config.go @@ -75,6 +75,35 @@ func ParseProviderConfigCompact(traversal hcl.Traversal) (ProviderConfig, tfdiag 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 // instance address. func (pc ProviderConfig) Absolute(module ModuleInstance) AbsProviderConfig {