github.com/hashicorp/hcl/v2@v2.20.0/expr_list.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hcl
     5  
     6  // ExprList tests if the given expression is a static list construct and,
     7  // if so, extracts the expressions that represent the list elements.
     8  // If the given expression is not a static list, error diagnostics are
     9  // returned.
    10  //
    11  // A particular Expression implementation can support this function by
    12  // offering a method called ExprList that takes no arguments and returns
    13  // []Expression. This method should return nil if a static list cannot
    14  // be extracted.  Alternatively, an implementation can support
    15  // UnwrapExpression to delegate handling of this function to a wrapped
    16  // Expression object.
    17  func ExprList(expr Expression) ([]Expression, Diagnostics) {
    18  	type exprList interface {
    19  		ExprList() []Expression
    20  	}
    21  
    22  	physExpr := UnwrapExpressionUntil(expr, func(expr Expression) bool {
    23  		_, supported := expr.(exprList)
    24  		return supported
    25  	})
    26  
    27  	if exL, supported := physExpr.(exprList); supported {
    28  		if list := exL.ExprList(); list != nil {
    29  			return list, nil
    30  		}
    31  	}
    32  	return nil, Diagnostics{
    33  		&Diagnostic{
    34  			Severity: DiagError,
    35  			Summary:  "Invalid expression",
    36  			Detail:   "A static list expression is required.",
    37  			Subject:  expr.StartRange().Ptr(),
    38  		},
    39  	}
    40  }