github.com/mithrandie/csvq@v1.18.1/lib/constant/main.go (about)

     1  package constant
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/mithrandie/csvq/lib/parser"
     8  	"github.com/mithrandie/csvq/lib/value"
     9  )
    10  
    11  var ErrInvalidType = errors.New("invalid constant type")
    12  var ErrUndefined = errors.New("constant is not defined")
    13  
    14  func Get(expr parser.Constant) (value.Primary, error) {
    15  	if m, ok := Definition[strings.ToUpper(expr.Space)]; ok {
    16  		if c, ok := m[strings.ToUpper(expr.Name)]; ok {
    17  			return ConvertConstantToPrivamryValue(c)
    18  		}
    19  	}
    20  	return nil, ErrUndefined
    21  }
    22  
    23  func ConvertConstantToPrivamryValue(c interface{}) (value.Primary, error) {
    24  	switch v := c.(type) {
    25  	case int64:
    26  		return value.NewInteger(v), nil
    27  	case float64:
    28  		return value.NewFloat(v), nil
    29  	default:
    30  		return nil, ErrInvalidType
    31  	}
    32  }
    33  
    34  func Count() int {
    35  	count := 0
    36  
    37  	for _, v := range Definition {
    38  		count = count + len(v)
    39  	}
    40  
    41  	return count
    42  }