github.com/Axway/agent-sdk@v1.1.101/pkg/filter/callexpr.go (about)

     1  package filter
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  )
     7  
     8  // CallType - Call type for filter condition
     9  type CallType int
    10  
    11  // Constants for Call type
    12  const (
    13  	GETVALUE CallType = iota
    14  	MATCHREGEX
    15  	CONTAINS
    16  	EXISTS
    17  	ANY
    18  )
    19  
    20  var callTypeMap = map[string]CallType{
    21  	"any":        ANY,
    22  	"exists":     EXISTS,
    23  	"contains":   CONTAINS,
    24  	"matchregex": MATCHREGEX,
    25  }
    26  
    27  // CallExpr - Interface for call expression in filter condition
    28  type CallExpr interface {
    29  	GetType() CallType
    30  	Execute(data Data) (interface{}, error)
    31  	String() string
    32  }
    33  
    34  // NewCallExpr - Factory method for creating CallExpr
    35  func newCallExpr(callType CallType, filterType, name string, arguments []interface{}) (callExpr CallExpr, err error) {
    36  	if (callType == ANY || callType == EXISTS) && len(arguments) != 0 {
    37  		return nil, errors.New("syntax error, unrecognized argument(s)")
    38  	}
    39  
    40  	if callType == CONTAINS || callType == MATCHREGEX {
    41  		if len(arguments) == 0 {
    42  			return nil, errors.New("syntax error, missing argument")
    43  		}
    44  		if len(arguments) != 1 {
    45  			return nil, errors.New("syntax error, unrecognized argument(s)")
    46  		}
    47  	}
    48  	callTypeSupported := false
    49  	for _, supportedCallType := range supportedExpr {
    50  		if supportedCallType == callType {
    51  			callTypeSupported = true
    52  			break
    53  		}
    54  	}
    55  
    56  	if !callTypeSupported {
    57  		return nil, errors.New("syntax error, unsupported condition")
    58  	}
    59  
    60  	switch callType {
    61  	case GETVALUE:
    62  		callExpr = newValueExpr(filterType, name)
    63  	case EXISTS:
    64  		callExpr = newExistsExpr(filterType, name)
    65  	case ANY:
    66  		callExpr = newAnyExpr(filterType)
    67  	case CONTAINS:
    68  		callExpr = newContainsExpr(filterType, name, arguments[0].(string))
    69  	case MATCHREGEX:
    70  		callExpr, err = newMatchRegExExpr(filterType, name, arguments[0].(string))
    71  	}
    72  
    73  	return
    74  }
    75  
    76  // GetCallType - Converts a string to its corresponding call type.
    77  func GetCallType(callTypeString string) (callType CallType, err error) {
    78  	callType, ok := callTypeMap[strings.ToLower(callTypeString)]
    79  	if !ok {
    80  		err = errors.New("unsupported call")
    81  	}
    82  	return
    83  }