github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/hcl2/model/functions.go (about)

     1  // Copyright 2016-2020, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package model
    16  
    17  import (
    18  	"github.com/hashicorp/hcl/v2"
    19  	"github.com/hashicorp/hcl/v2/hclsyntax"
    20  	"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
    21  )
    22  
    23  // FunctionSignature represents a possibly-type-polymorphic function signature.
    24  type FunctionSignature interface {
    25  	// GetSignature returns the static signature for the function when invoked with the given arguments.
    26  	GetSignature(arguments []Expression) (StaticFunctionSignature, hcl.Diagnostics)
    27  }
    28  
    29  // Parameter represents a single function parameter.
    30  type Parameter struct {
    31  	Name string // The name of the parameter.
    32  	Type Type   // The type of the parameter.
    33  }
    34  
    35  // StaticFunctionSignature records the parameters and return type of a function.
    36  type StaticFunctionSignature struct {
    37  	// The function's fixed parameters.
    38  	Parameters []Parameter
    39  	// The function's variadic parameter, if any. Any arguments that follow a function's fixed arguments must be
    40  	// assignable to this parameter.
    41  	VarargsParameter *Parameter
    42  	// The return type of the function.
    43  	ReturnType Type
    44  }
    45  
    46  // GetSignature returns the static signature itself.
    47  func (fs StaticFunctionSignature) GetSignature(arguments []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
    48  	return fs, nil
    49  }
    50  
    51  // GenericFunctionSignature represents a type-polymorphic function signature. The underlying function will be
    52  // invoked by GenericFunctionSignature.GetSignature to compute the static signature of the function.
    53  type GenericFunctionSignature func(arguments []Expression) (StaticFunctionSignature, hcl.Diagnostics)
    54  
    55  // GetSignature returns the static function signature when it is invoked with the given arguments.
    56  func (fs GenericFunctionSignature) GetSignature(arguments []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
    57  	return fs(arguments)
    58  }
    59  
    60  // Function represents a function definition.
    61  type Function struct {
    62  	signature FunctionSignature
    63  }
    64  
    65  // NewFunction creates a new function with the given signature.
    66  func NewFunction(signature FunctionSignature) *Function {
    67  	return &Function{signature: signature}
    68  }
    69  
    70  // SyntaxNode returns the syntax node for the function, which is always syntax.None.
    71  func (f *Function) SyntaxNode() hclsyntax.Node {
    72  	return syntax.None
    73  }
    74  
    75  // Traverse attempts to traverse the function definition. This will always fail: functions are not traversable.
    76  func (f *Function) Traverse(traverser hcl.Traverser) (Traversable, hcl.Diagnostics) {
    77  	return DynamicType, hcl.Diagnostics{cannotTraverseFunction(traverser.SourceRange())}
    78  }
    79  
    80  // GetSignature returns the static signature of the function when it is invoked with the given arguments.
    81  func (f *Function) GetSignature(arguments []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
    82  	return f.signature.GetSignature(arguments)
    83  }