github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/config/lang/ast/scope.go (about)

     1  package ast
     2  
     3  // Scope is the interface used to look up variables and functions while
     4  // evaluating. How these functions/variables are defined are up to the caller.
     5  type Scope interface {
     6  	LookupFunc(string) (Function, bool)
     7  	LookupVar(string) (Variable, bool)
     8  }
     9  
    10  // Variable is a variable value for execution given as input to the engine.
    11  // It records the value of a variables along with their type.
    12  type Variable struct {
    13  	Value interface{}
    14  	Type  Type
    15  }
    16  
    17  // Function defines a function that can be executed by the engine.
    18  // The type checker will validate that the proper types will be called
    19  // to the callback.
    20  type Function struct {
    21  	// ArgTypes is the list of types in argument order. These are the
    22  	// required arguments.
    23  	//
    24  	// ReturnType is the type of the returned value. The Callback MUST
    25  	// return this type.
    26  	ArgTypes   []Type
    27  	ReturnType Type
    28  
    29  	// Variadic, if true, says that this function is variadic, meaning
    30  	// it takes a variable number of arguments. In this case, the
    31  	// VariadicType must be set.
    32  	Variadic     bool
    33  	VariadicType Type
    34  
    35  	// Callback is the function called for a function. The argument
    36  	// types are guaranteed to match the spec above by the type checker.
    37  	// The length of the args is strictly == len(ArgTypes) unless Varidiac
    38  	// is true, in which case its >= len(ArgTypes).
    39  	Callback func([]interface{}) (interface{}, error)
    40  }
    41  
    42  // BasicScope is a simple scope that looks up variables and functions
    43  // using a map.
    44  type BasicScope struct {
    45  	FuncMap map[string]Function
    46  	VarMap  map[string]Variable
    47  }
    48  
    49  func (s *BasicScope) LookupFunc(n string) (Function, bool) {
    50  	if s == nil {
    51  		return Function{}, false
    52  	}
    53  
    54  	v, ok := s.FuncMap[n]
    55  	return v, ok
    56  }
    57  
    58  func (s *BasicScope) LookupVar(n string) (Variable, bool) {
    59  	if s == nil {
    60  		return Variable{}, false
    61  	}
    62  
    63  	v, ok := s.VarMap[n]
    64  	return v, ok
    65  }