github.com/rvichery/terraform@v0.11.10/configs/compat_shim.go (about)

     1  package configs
     2  
     3  import (
     4  	"github.com/hashicorp/hcl2/hcl"
     5  	"github.com/hashicorp/hcl2/hcl/hclsyntax"
     6  	"github.com/zclconf/go-cty/cty"
     7  )
     8  
     9  // -------------------------------------------------------------------------
    10  // Functions in this file are compatibility shims intended to ease conversion
    11  // from the old configuration loader. Any use of these functions that makes
    12  // a change should generate a deprecation warning explaining to the user how
    13  // to update their code for new patterns.
    14  //
    15  // Shims are particularly important for any patterns that have been widely
    16  // documented in books, tutorials, etc. Users will still be starting from
    17  // these examples and we want to help them adopt the latest patterns rather
    18  // than leave them stranded.
    19  // -------------------------------------------------------------------------
    20  
    21  // shimTraversalInString takes any arbitrary expression and checks if it is
    22  // a quoted string in the native syntax. If it _is_, then it is parsed as a
    23  // traversal and re-wrapped into a synthetic traversal expression and a
    24  // warning is generated. Otherwise, the given expression is just returned
    25  // verbatim.
    26  //
    27  // This function has no effect on expressions from the JSON syntax, since
    28  // traversals in strings are the required pattern in that syntax.
    29  //
    30  // If wantKeyword is set, the generated warning diagnostic will talk about
    31  // keywords rather than references. The behavior is otherwise unchanged, and
    32  // the caller remains responsible for checking that the result is indeed
    33  // a keyword, e.g. using hcl.ExprAsKeyword.
    34  func shimTraversalInString(expr hcl.Expression, wantKeyword bool) (hcl.Expression, hcl.Diagnostics) {
    35  	if !exprIsNativeQuotedString(expr) {
    36  		return expr, nil
    37  	}
    38  
    39  	strVal, diags := expr.Value(nil)
    40  	if diags.HasErrors() || strVal.IsNull() || !strVal.IsKnown() {
    41  		// Since we're not even able to attempt a shim here, we'll discard
    42  		// the diagnostics we saw so far and let the caller's own error
    43  		// handling take care of reporting the invalid expression.
    44  		return expr, nil
    45  	}
    46  
    47  	// The position handling here isn't _quite_ right because it won't
    48  	// take into account any escape sequences in the literal string, but
    49  	// it should be close enough for any error reporting to make sense.
    50  	srcRange := expr.Range()
    51  	startPos := srcRange.Start // copy
    52  	startPos.Column++          // skip initial quote
    53  	startPos.Byte++            // skip initial quote
    54  
    55  	traversal, tDiags := hclsyntax.ParseTraversalAbs(
    56  		[]byte(strVal.AsString()),
    57  		srcRange.Filename,
    58  		startPos,
    59  	)
    60  	diags = append(diags, tDiags...)
    61  
    62  	// For initial release our deprecation warnings are disabled to allow
    63  	// a period where modules can be compatible with both old and new
    64  	// conventions.
    65  	// FIXME: Re-enable these deprecation warnings in a release prior to
    66  	// Terraform 0.13 and then remove the shims altogether for 0.13.
    67  	/*
    68  		if wantKeyword {
    69  			diags = append(diags, &hcl.Diagnostic{
    70  				Severity: hcl.DiagWarning,
    71  				Summary:  "Quoted keywords are deprecated",
    72  				Detail:   "In this context, keywords are expected literally rather than in quotes. Previous versions of Terraform required quotes, but that usage is now deprecated. Remove the quotes surrounding this keyword to silence this warning.",
    73  				Subject:  &srcRange,
    74  			})
    75  		} else {
    76  			diags = append(diags, &hcl.Diagnostic{
    77  				Severity: hcl.DiagWarning,
    78  				Summary:  "Quoted references are deprecated",
    79  				Detail:   "In this context, references are expected literally rather than in quotes. Previous versions of Terraform required quotes, but that usage is now deprecated. Remove the quotes surrounding this reference to silence this warning.",
    80  				Subject:  &srcRange,
    81  			})
    82  		}
    83  	*/
    84  
    85  	return &hclsyntax.ScopeTraversalExpr{
    86  		Traversal: traversal,
    87  		SrcRange:  srcRange,
    88  	}, diags
    89  }
    90  
    91  // shimIsIgnoreChangesStar returns true if the given expression seems to be
    92  // a string literal whose value is "*". This is used to support a legacy
    93  // form of ignore_changes = all .
    94  //
    95  // This function does not itself emit any diagnostics, so it's the caller's
    96  // responsibility to emit a warning diagnostic when this function returns true.
    97  func shimIsIgnoreChangesStar(expr hcl.Expression) bool {
    98  	val, valDiags := expr.Value(nil)
    99  	if valDiags.HasErrors() {
   100  		return false
   101  	}
   102  	if val.Type() != cty.String || val.IsNull() || !val.IsKnown() {
   103  		return false
   104  	}
   105  	return val.AsString() == "*"
   106  }