本文整理汇总了Golang中common.CoerceValues函数的典型用法代码示例。如果您正苦于以下问题:Golang CoerceValues函数的具体用法?Golang CoerceValues怎么用?Golang CoerceValues使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CoerceValues函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: EqualityOperator
func EqualityOperator(leftValue, rightValue *protocol.FieldValue) (OperatorResult, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
if v1.(string) == v2.(string) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_INT:
if v1.(int64) == v2.(int64) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_DOUBLE:
if v1.(float64) == v2.(float64) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_BOOL:
if v1.(bool) == v2.(bool) {
return MATCH, nil
}
return NO_MATCH, nil
default:
return INVALID, nil
}
}
示例2: InOperator
func InOperator(leftValue *protocol.FieldValue, rightValue []*protocol.FieldValue) (OperatorResult, error) {
for _, v := range rightValue {
v1, v2, cType := common.CoerceValues(leftValue, v)
var result bool
switch cType {
case common.TYPE_STRING:
result = v1.(string) == v2.(string)
case common.TYPE_INT:
result = v1.(int64) == v2.(int64)
case common.TYPE_DOUBLE:
result = v1.(float64) == v2.(float64)
case common.TYPE_BOOL:
result = v1.(bool) == v2.(bool)
default:
return INVALID, nil
}
if result {
return MATCH, nil
}
}
return NO_MATCH, nil
}
示例3: RegexMatcherOperator
func RegexMatcherOperator(leftValue, rightValue *protocol.FieldValue) (bool, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
// TODO: assume that the regex is valid
matches, _ := regexp.MatchString(v2.(string), v1.(string))
return matches, nil
default:
return false, nil
}
}
示例4: RegexMatcherOperator
func RegexMatcherOperator(leftValue, rightValue *protocol.FieldValue) (OperatorResult, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
// TODO: assume that the regex is valid
if ok, _ := regexp.MatchString(v2.(string), v1.(string)); ok {
return MATCH, nil
}
return NO_MATCH, nil
default:
return INVALID, nil
}
}
示例5: GreaterThanOperator
func GreaterThanOperator(leftValue, rightValue *protocol.FieldValue) (bool, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
return v1.(string) > v2.(string), nil
case common.TYPE_INT:
return v1.(int64) > v2.(int64), nil
case common.TYPE_DOUBLE:
return v1.(float64) > v2.(float64), nil
default:
return false, nil
}
}
示例6: MultiplyOperator
func MultiplyOperator(elems []*parser.Value, fields []string, point *protocol.Point) (*protocol.FieldValue, error) {
leftValue, err := GetValue(elems[0], fields, point)
if err != nil {
return nil, err
}
rightValues, err := GetValue(elems[1], fields, point)
if err != nil {
return nil, err
}
left, right, valueType := common.CoerceValues(leftValue, rightValues)
switch valueType {
case common.TYPE_DOUBLE:
value := left.(float64) * right.(float64)
return &protocol.FieldValue{DoubleValue: &value}, nil
case common.TYPE_INT:
value := left.(int64) * right.(int64)
return &protocol.FieldValue{Int64Value: &value}, nil
}
return nil, fmt.Errorf("* operator doesn't work with %v types", valueType)
}