本文整理匯總了Golang中github.com/couchbaselabs/dparval.NewValue函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewValue函數的具體用法?Golang NewValue怎麽用?Golang NewValue使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewValue函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Evaluate
func (this *FunctionCallLower) Evaluate(item *dparval.Value) (*dparval.Value, error) {
// first evaluate the argument
av, err := this.Operands[0].Expr.Evaluate(item)
// the spec defines this functin to ONLY operate on strings
// all other types result in NULL
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// undefined returns null
return dparval.NewValue(nil), nil
default:
// any other error return to caller
return nil, err
}
}
if av.Type() == dparval.STRING {
avalue := av.Value()
switch avalue := avalue.(type) {
case string:
return dparval.NewValue(strings.ToLower(avalue)), nil
}
}
return dparval.NewValue(nil), nil
}
示例2: TestCount
func TestCount(t *testing.T) {
dataset := dparval.ValueCollection{
dparval.NewValue(map[string]interface{}{
"name": "marty",
"status": "alive",
}),
dparval.NewValue(map[string]interface{}{
"name": "gerald",
"status": nil,
}),
dparval.NewValue(map[string]interface{}{
"name": "steve",
}),
}
tests := AggregateTestSet{
// test * (counts all rows)
{
NewFunctionCall("COUNT", FunctionArgExpressionList{NewStarFunctionArgExpression()}),
dparval.NewValue(3.0),
},
// test expression (eliminiates null and missing)
{
NewFunctionCall("COUNT", FunctionArgExpressionList{NewFunctionArgExpression(NewProperty("status"))}),
dparval.NewValue(1.0),
},
}
tests.Run(t, dataset)
}
示例3: Evaluate
func (this *FunctionCallCeil) Evaluate(item *dparval.Value) (*dparval.Value, error) {
// first evaluate the argument
av, err := this.Operands[0].Expr.Evaluate(item)
// the spec defines this functin to ONLY operate on numeric values
// all other types result in NULL
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// undefined returns null
return dparval.NewValue(nil), nil
default:
// any other error return to caller
return nil, err
}
}
if av.Type() == dparval.NUMBER {
avalue := av.Value()
switch avalue := avalue.(type) {
case float64:
return dparval.NewValue(math.Ceil(avalue)), nil
}
}
return dparval.NewValue(nil), nil
}
示例4: Evaluate
func (this *OrOperator) Evaluate(item *dparval.Value) (*dparval.Value, error) {
var rv interface{}
var re error
rv = false
re = nil
for _, operand := range this.Operands {
operandVal, err := operand.Evaluate(item)
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
rv = nil
re = err
continue
default:
// any other error should be returned to caller
return nil, err
}
}
// now interpret the evaluated value in a boolean context
operandValVal := operandVal.Value()
operandBoolVal := ValueInBooleanContext(operandValVal)
if operandBoolVal == true {
return dparval.NewValue(true), nil
} else if operandBoolVal == nil && rv == false {
rv = operandBoolVal
re = nil
}
// if operandBoolVal is true, do nothing
// rv starts as true, and should never change back to true
}
if re != nil {
return nil, re
}
return dparval.NewValue(rv), re
}
示例5: init
func init() {
doc := dparval.NewValue(map[string]interface{}{
"name": "mike",
"children": []interface{}{
map[string]interface{}{
"name": "bob",
},
map[string]interface{}{
"name": "dan",
},
},
})
doc.SetAttachment("meta", map[string]interface{}{"id": "1"})
joinerTestData = append(joinerTestData, doc)
doc = dparval.NewValue(map[string]interface{}{
"name": "dustin",
"children": []interface{}{
map[string]interface{}{
"name": "mary",
},
map[string]interface{}{
"name": "jane",
},
},
})
doc.SetAttachment("meta", map[string]interface{}{"id": "2"})
joinerTestData = append(joinerTestData, doc)
}
示例6: ValueCount
func (vi *viewIndex) ValueCount() (int64, query.Error) {
indexItemChannel := make(catalog.EntryChannel)
indexWarnChannel := make(query.ErrorChannel)
indexErrorChannel := make(query.ErrorChannel)
go vi.ScanRange(catalog.LookupValue{dparval.NewValue(nil)}, catalog.LookupValue{dparval.NewValue(nil)}, catalog.Both, 0, indexItemChannel, indexWarnChannel, indexErrorChannel)
var err query.Error
nullCount := int64(0)
ok := true
for ok {
select {
case _, ok = <-indexItemChannel:
if ok {
nullCount += 1
}
case _, ok = <-indexWarnChannel:
// ignore warnings here
case err, ok = <-indexErrorChannel:
if err != nil {
return 0, err
}
}
}
totalCount, err := ViewTotalRows(vi.bucket.cbbucket, vi.DDocName(), vi.ViewName(), map[string]interface{}{})
if err != nil {
return 0, err
}
return totalCount - nullCount, nil
}
示例7: Evaluate
func (this *FunctionCallLength) Evaluate(item *dparval.Value) (*dparval.Value, error) {
// first evaluate the argument
av, err := this.Operands[0].Expr.Evaluate(item)
// the spec does not define it to operate on missing, so return null
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// undefined returns null
return dparval.NewValue(nil), nil
default:
// any other error return to caller
return nil, err
}
}
// return the length only if the operand is a string
if av.Type() == dparval.STRING {
avalue := av.Value()
switch avalue := avalue.(type) {
case string:
return dparval.NewValue(float64(len(avalue))), nil
}
}
return dparval.NewValue(nil), nil
}
示例8: Evaluate
func (this *FunctionCallStrToMillis) Evaluate(item *dparval.Value) (*dparval.Value, error) {
av, err := this.Operands[0].Expr.Evaluate(item)
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// undefined returns null
return dparval.NewValue(nil), nil
default:
// any other error return to caller
return nil, err
}
}
val := av.Value()
switch val := val.(type) {
case string:
t, err := strToTime(val)
if err != nil {
return nil, fmt.Errorf("Date not in a recognized format.")
}
return dparval.NewValue(timeToMillis(t)), nil
default:
return dparval.NewValue(nil), nil
}
}
示例9: UpdateAggregate
func (this *FunctionCallMin) UpdateAggregate(group *dparval.Value, item *dparval.Value) error {
aggregate_key := this.Key()
currentVal, err := aggregateValue(group, aggregate_key)
if err != nil {
return fmt.Errorf("group defaults not set correctly")
}
if this.Operands[0].Expr != nil {
val, err := this.Operands[0].Expr.Evaluate(item)
val, err = eliminateNullMissing(val, err)
if err != nil {
return err
}
if val != nil {
nextVal := val.Value()
currVal := currentVal.Value()
if currVal == nil {
// any value is greater than nil (we eliminated null/mising already)
setAggregateValue(group, aggregate_key, dparval.NewValue(nextVal))
} else {
// check to see
comp := CollateJSON(nextVal, currVal)
if comp < 0 {
setAggregateValue(group, aggregate_key, dparval.NewValue(nextVal))
}
}
}
}
return nil
}
示例10: Evaluate
func (this *CollectionAnyOperator) Evaluate(item *dparval.Value) (*dparval.Value, error) {
// first evaluate the over
ov, err := this.Over.Evaluate(item)
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// spec says return false
return dparval.NewValue(false), nil
default:
// any other error should be returned to caller
return nil, err
}
}
// see if we're dealing with an array
if ov.Type() == dparval.ARRAY {
// by accessing the array contents this way
// we avoid parsing it
ok := true
index := 0
for ok {
inner, err := ov.Index(index)
index = index + 1
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
ok = false
default:
return nil, err
}
} else {
// duplicate the existing context
innerContext := item.Duplicate()
// add this object named as the alias
innerContext.SetPath(this.As, inner)
// now evaluate the condition in this new context
innerResult, err := this.Condition.Evaluate(innerContext)
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// this is not true, keep trying
continue
default:
// any other error should be returned to caller
return nil, err
}
}
innerResultVal := innerResult.Value()
// check to see if this value is true
innerBoolResult := ValueInBooleanContext(innerResultVal)
if innerBoolResult == true {
return dparval.NewValue(true), nil
}
}
}
return dparval.NewValue(false), nil
}
return dparval.NewValue(false), nil
}
示例11: DefaultAggregate
func (this *FunctionCallMax) DefaultAggregate(group *dparval.Value) error {
aggregate_key := this.Key()
currentVal, err := aggregateValue(group, aggregate_key)
if err != nil {
currentVal = dparval.NewValue(nil)
// store this, so that even if all values are eliminated we return null
setAggregateValue(group, aggregate_key, dparval.NewValue(currentVal))
}
return nil
}
示例12: Evaluate
func (this *FunctionCallSplit) Evaluate(item *dparval.Value) (*dparval.Value, error) {
// first evaluate the arguments
av, err := this.Operands[0].Expr.Evaluate(item)
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// undefined returns null
return dparval.NewValue(nil), nil
default:
// any other error return to caller
return nil, err
}
// FIXME warn if arguments were wrong type?
if av.Type() != dparval.STRING {
return dparval.NewValue(nil), nil
}
}
var sep *dparval.Value = nil
if len(this.Operands) > 1 {
sep, err = this.Operands[1].Expr.Evaluate(item)
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
// undefined returns null
return dparval.NewValue(nil), nil
default:
// any other error return to caller
return nil, err
}
}
// FIXME warn if arguments were wrong type?
if sep.Type() != dparval.STRING {
return dparval.NewValue(nil), nil
}
}
var sa []string
if sep != nil {
sa = strings.Split(av.Value().(string),
sep.Value().(string))
} else {
sa = strings.Fields(av.Value().(string))
}
rv := make([]interface{}, len(sa))
for i, s := range sa {
rv[i] = s
}
return dparval.NewValue(rv), nil
}
示例13: Evaluate
func (this *InOperator) Evaluate(context *dparval.Value) (*dparval.Value, error) {
false_result := dparval.NewValue(false)
true_result := dparval.NewValue(true)
lv, err := this.Left.Evaluate(context)
if err != nil {
return nil, err
}
rv, err := this.Right.Evaluate(context)
if err != nil {
return nil, err
}
lvalue := lv.Value()
if rv.Type() == dparval.ARRAY {
ok := true
index := 0
for ok {
inner, err := rv.Index(index)
index = index + 1
if err != nil {
switch err := err.(type) {
case *dparval.Undefined:
ok = false
default:
return nil, err
}
} else {
if lv.Type() != inner.Type() {
continue
} else {
iv := inner.Value()
switch lvalue := lvalue.(type) {
case string:
if lvalue == iv {
return true_result, nil
}
default:
if reflect.DeepEqual(lvalue, iv) == true {
return true_result, nil
}
}
}
}
}
}
return false_result, nil
}
示例14: TestSumAndAvg
// sum and avg have same elimination rules, test them together
func TestSumAndAvg(t *testing.T) {
dataset := dparval.ValueCollection{
dparval.NewValue(map[string]interface{}{
"name": "marty",
"score": 20.0,
}),
dparval.NewValue(map[string]interface{}{
"name": "gerald",
"score": nil,
}),
dparval.NewValue(map[string]interface{}{
"name": "steve",
}),
dparval.NewValue(map[string]interface{}{
"name": "siri",
"score": "thirty",
}),
dparval.NewValue(map[string]interface{}{
"name": "deep",
"score": 10.0,
}),
dparval.NewValue(map[string]interface{}{
"name": "ketaki",
"score": "false",
}),
dparval.NewValue(map[string]interface{}{
"name": "pratap",
"score": []interface{}{5.5},
}),
dparval.NewValue(map[string]interface{}{
"name": "karen",
"score": map[string]interface{}{"score": 5.5},
}),
}
tests := AggregateTestSet{
// test expression (eliminiates null and missing)
{
NewFunctionCall("SUM", FunctionArgExpressionList{NewFunctionArgExpression(NewProperty("score"))}),
dparval.NewValue(30.0),
},
{
NewFunctionCall("AVG", FunctionArgExpressionList{NewFunctionArgExpression(NewProperty("score"))}),
dparval.NewValue(15.0),
},
}
tests.Run(t, dataset)
}
示例15: init
func init() {
doc := dparval.NewValue(map[string]interface{}{"name": "mike", "age": 100.0})
doc.SetAttachment("meta", map[string]interface{}{"id": "1"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "dustin"})
doc.SetAttachment("meta", map[string]interface{}{"id": "1"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "bob", "age": nil})
doc.SetAttachment("meta", map[string]interface{}{"id": "1"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "marty", "age": 99.0})
doc.SetAttachment("meta", map[string]interface{}{"id": "1"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "steve", "age": 200.0})
doc.SetAttachment("meta", map[string]interface{}{"id": "2"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "gerald", "age": 175.0})
doc.SetAttachment("meta", map[string]interface{}{"id": "3"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "siri", "age": 74.0})
doc.SetAttachment("meta", map[string]interface{}{"id": "4"})
testData = append(testData, doc)
doc = dparval.NewValue(map[string]interface{}{"name": "ali", "age": 100.0})
doc.SetAttachment("meta", map[string]interface{}{"id": "1"})
testData = append(testData, doc)
}