本文整理汇总了Golang中github.com/couchbase/query/value.NewValue函数的典型用法代码示例。如果您正苦于以下问题:Golang NewValue函数的具体用法?Golang NewValue怎么用?Golang NewValue使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewValue函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Signature
/*
Returns the shape of the union result. If the two sub results
are equal return the first value. If either of the inputs
to the union setop are not objects then return the _JSON_SIGNATURE.
Range through the two objects and check for equality and return
object value.
*/
func (this *unionSubresult) Signature() value.Value {
first := this.first.Signature()
second := this.second.Signature()
if first.Equals(second).Truth() {
return first
}
if first.Type() != value.OBJECT ||
second.Type() != value.OBJECT {
return _JSON_SIGNATURE
}
rv := first.Copy()
sa := second.Actual().(map[string]interface{})
for k, v := range sa {
cv, ok := rv.Field(k)
if ok {
if !value.NewValue(cv).Equals(value.NewValue(v)).Truth() {
rv.SetField(k, _JSON_SIGNATURE)
}
} else {
rv.SetField(k, v)
}
}
return rv
}
示例2: Apply
/*
This method takes in two values and returns a value that
corresponds to the first position of the regular expression
pattern (already set or populated using the second value)
in the first string value, or -1 if it isnt found. If the
input type is missing return missing, and if it isnt
string then return null value. Use the FindStringIndex
method in the regexp package to return a two-element slice
of integers defining the location of the leftmost match in
the string of the regular expression as per the Go Docs. Return
the first element of this slice as a value. If a FindStringIndex
returns nil, then the regexp pattern isnt found. Hence return -1.
*/
func (this *RegexpPosition) Apply(context Context, first, second value.Value) (value.Value, error) {
if first.Type() == value.MISSING || second.Type() == value.MISSING {
return value.MISSING_VALUE, nil
} else if first.Type() != value.STRING || second.Type() != value.STRING {
return value.NULL_VALUE, nil
}
f := first.Actual().(string)
s := second.Actual().(string)
re := this.re
if re == nil {
var err error
re, err = regexp.Compile(s)
if err != nil {
return nil, err
}
}
loc := re.FindStringIndex(f)
if loc == nil {
return value.NewValue(-1.0), nil
}
return value.NewValue(float64(loc[0])), nil
}
示例3: BenchmarkN1QLCollateMap
func BenchmarkN1QLCollateMap(b *testing.B) {
v := qv.NewValue(testcases[0].text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.Collate(qv.NewValue(testcases[0].text))
}
}
示例4: Apply
/*
It returns a string based on the input expr value. Values
missing, null and strings return themselves. False, true
(boolean) and numbers return their string representation.
This is done using the Sprint method defined in fmt for Go.
All other values map to null.
*/
func (this *ToString) Apply(context Context, arg value.Value) (value.Value, error) {
switch arg.Type() {
case value.MISSING, value.NULL, value.STRING:
return arg, nil
case value.BOOLEAN:
return value.NewValue(fmt.Sprint(arg.Actual())), nil
case value.NUMBER:
f := arg.Actual().(float64)
if f == -0 {
f = 0
}
s := strconv.FormatFloat(f, 'f', -1, 64)
return value.NewValue(s), nil
case value.BINARY:
raw, ok := arg.Actual().([]byte)
if !ok {
return value.NULL_VALUE, nil
}
s := string(raw)
return value.NewValue(s), nil
default:
return value.NULL_VALUE, nil
}
}
示例5: Test2iScanRange
func Test2iScanRange(t *testing.T) {
c.LogIgnore()
//c.SetLogLevel(c.LogLevelDebug)
low, high := value.NewValue("aaaa"), value.NewValue("zzzz")
span := &datastore.Span{
Range: &datastore.Range{
Low: value.Values{low},
High: value.Values{high},
Inclusion: datastore.BOTH,
},
}
conn := datastore.NewIndexConnection(nil)
entrych := conn.EntryChannel()
quitch := conn.StopChannel()
go index.Scan("", span, false, 10000, conn)
count := 0
loop:
for {
select {
case _, ok := <-entrych:
if !ok {
break loop
}
count++
case <-quitch:
break loop
}
}
if count != 20000 {
t.Fatal("failed ScanRange() - ", count)
}
}
示例6: Apply
/*
This method removes all the occurences of the second value from the
first array value.
*/
func (this *ArrayRemove) Apply(context Context, first, second value.Value) (value.Value, error) {
if first.Type() == value.MISSING {
return first, nil
}
if first.Type() != value.ARRAY {
return value.NULL_VALUE, nil
}
if second.Type() <= value.NULL {
return first, nil
}
fa := first.Actual().([]interface{})
if len(fa) == 0 {
return first, nil
}
ra := make([]interface{}, 0, len(fa))
for _, f := range fa {
if !second.Equals(value.NewValue(f)).Truth() {
ra = append(ra, f)
}
}
return value.NewValue(ra), nil
}
示例7: EvaluateForIndex
func (this *All) EvaluateForIndex(item value.Value, context Context) (value.Value, value.Values, error) {
val, vals, err := this.array.EvaluateForIndex(item, context)
if err != nil {
return val, vals, err
}
if vals != nil {
return nil, vals, nil
}
var rv value.Values
act := val.Actual()
switch act := act.(type) {
case []interface{}:
rv = make(value.Values, len(act))
for i, a := range act {
rv[i] = value.NewValue(a)
}
case nil:
if val.Type() == value.NULL {
rv = _NULL_ARRAY
}
// Else MISSING, return rv=nil
default:
// Coerce scalar into array
rv = value.Values{value.NewValue(act)}
}
return nil, rv, nil
}
示例8: Evaluate
func (this *Any) Evaluate(item value.Value, context Context) (value.Value, error) {
missing := false
null := false
barr := make([][]interface{}, len(this.bindings))
for i, b := range this.bindings {
bv, err := b.Expression().Evaluate(item, context)
if err != nil {
return nil, err
}
if b.Descend() {
buffer := make([]interface{}, 0, 256)
bv = value.NewValue(bv.Descendants(buffer))
}
switch bv.Type() {
case value.ARRAY:
barr[i] = bv.Actual().([]interface{})
case value.MISSING:
missing = true
default:
null = true
}
}
if missing {
return value.MISSING_VALUE, nil
}
if null {
return value.NULL_VALUE, nil
}
n := -1
for _, b := range barr {
if n < 0 || len(b) < n {
n = len(b)
}
}
for i := 0; i < n; i++ {
cv := value.NewScopeValue(make(map[string]interface{}, len(this.bindings)), item)
for j, b := range this.bindings {
cv.SetField(b.Variable(), barr[j][i])
}
sv, err := this.satisfies.Evaluate(cv, context)
if err != nil {
return nil, err
}
if sv.Truth() {
return value.NewValue(true), nil
}
}
return value.NewValue(false), nil
}
示例9: TestObjectRemove_remove
func TestObjectRemove_remove(t *testing.T) {
/* tests insert of value in array at start */
e1 := NewConstant(value.NewValue(map[string]interface{}{"f1": 1, "f2": 2}))
e2 := NewConstant("f2")
er := value.NewValue(map[string]interface{}{"f1": 1})
testObjectRemove(e1, e2, er, t)
}
示例10: BenchmarkN1QLCollateFloat
func BenchmarkN1QLCollateFloat(b *testing.B) {
jsonb := []byte(`1234567890.001234556`)
v := qv.NewValue(jsonb)
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.Collate(qv.NewValue(jsonb))
}
}
示例11: TestObjectAdd_remove
func TestObjectAdd_remove(t *testing.T) {
/* tests insert of value in array at start */
e1 := NewConstant(value.NewValue(map[string]interface{}{"f1": 1, "f2": 2}))
e2 := NewConstant("f2")
e3 := NewConstant(value.MISSING_VALUE)
er := value.NewValue(value.NULL_VALUE)
testObjectAdd(e1, e2, e3, er, false, t)
}
示例12: BenchmarkN1QLCollateArray
func BenchmarkN1QLCollateArray(b *testing.B) {
jsonb := []byte(
`[123456789, 123456789.1234567879, "hello world", true, false, null]`)
v := qv.NewValue(jsonb)
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.Collate(qv.NewValue(jsonb))
}
}
示例13: TestObjectPut_replace
func TestObjectPut_replace(t *testing.T) {
/* tests insert of value in array at start */
e1 := NewConstant(value.NewValue(map[string]interface{}{"f1": 1, "f2": 2}))
e2 := NewConstant("f2")
e3 := NewConstant(3)
er := value.NewValue(map[string]interface{}{"f1": 1, "f2": 3})
testObjectPut(e1, e2, e3, er, t)
}
示例14: TestMockIndex
func TestMockIndex(t *testing.T) {
s, err := NewDatastore("mock:")
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
p, err := s.NamespaceById("p0")
if err != nil || p == nil {
t.Fatalf("expected namespace p0")
}
b, err := p.KeyspaceById("b0")
if err != nil || b == nil {
t.Fatalf("expected keyspace b0")
}
// Do a scan from keys 4 to 6 with Inclusion set to NEITHER - expect 1 result with key 5
lo := []value.Value{value.NewValue("4")}
hi := []value.Value{value.NewValue("6")}
span := &datastore.Span{Range: datastore.Range{Inclusion: datastore.NEITHER, Low: lo, High: hi}}
items, err := doIndexScan(t, b, span)
if err != nil {
t.Fatalf("unexpected error in scan: %v", err)
}
if len(items) != 1 {
t.Fatalf("unexpected number of items in scan: %d", len(items))
}
if items[0].PrimaryKey != "5" {
t.Fatalf("unexpected key in result: %v", items[0].PrimaryKey)
}
// Do a scan from keys 4 to 6 with Inclusion set to BOTH - expect 3 results
span.Range.Inclusion = datastore.BOTH
items, err = doIndexScan(t, b, span)
if err != nil {
t.Fatalf("unexpected error in scan: %v", err)
}
if len(items) != 3 {
t.Fatalf("unexpected number of items in scan: %d", len(items))
}
// Do a scan with incorrect range type - expect scan error
span.Range.Low = []value.Value{value.NewValue(4.0)}
items, err = doIndexScan(t, b, span)
}
示例15: Apply
/*
This method returns a string from a start position to the end. It is a substring.
If the input argument value type is missing, then return a missing value, and if null
return a null value. Loop through all the input values, and check the types. If it is
a number type, then check if it is an absolute non floating point number. If not
return null value. If any value other than a number or missing, return a null.
If the position is negative calculate the actual offset by adding it to the length
of the string. If the length of input arguments is 2 or more, it means that the
start and end positions are given, hence return a value which is the
slice starting from that position until the end if specified.
*/
func (this *Substr) Apply(context Context, args ...value.Value) (value.Value, error) {
null := false
if args[0].Type() == value.MISSING {
return value.MISSING_VALUE, nil
} else if args[0].Type() != value.STRING {
null = true
}
for i := 1; i < len(args); i++ {
switch args[i].Type() {
case value.MISSING:
return value.MISSING_VALUE, nil
case value.NUMBER:
vf := args[i].Actual().(float64)
if vf != math.Trunc(vf) {
null = true
}
default:
null = true
}
}
if null {
return value.NULL_VALUE, nil
}
str := args[0].Actual().(string)
pos := int(args[1].Actual().(float64))
if pos < 0 {
pos = len(str) + pos
}
if pos < 0 || pos >= len(str) {
return value.NULL_VALUE, nil
}
if len(args) == 2 {
return value.NewValue(str[pos:]), nil
}
length := int(args[2].Actual().(float64))
if length < 0 || pos+length > len(str) {
return value.NULL_VALUE, nil
}
return value.NewValue(str[pos : pos+length]), nil
}