1// Copieright (c) 2014 Couchbase, Inc. 2// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 3// except in compliance with the License. You may obtain a copy of the License at 4// http://www.apache.org/licenses/LICENSE-2.0 5// Unless required by applicable law or agreed to in writing, software distributed under the 6// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 7// either express or implied. See the License for the specific language governing permissions 8// and limitations under the License. 9 10package value 11 12import ( 13 "bytes" 14 "fmt" 15 "io" 16 17 "github.com/couchbase/query/util" 18) 19 20type binaryValue []byte 21 22func (this binaryValue) String() string { 23 return fmt.Sprintf("\"<binary (%d b)>\"", len(this)) 24} 25 26func (this binaryValue) MarshalJSON() ([]byte, error) { 27 return []byte(this.String()), nil 28} 29 30func (this binaryValue) WriteJSON(w io.Writer, prefix, indent string) error { 31 _, err := w.Write([]byte(this.String())) 32 return err 33} 34 35func (this binaryValue) Type() Type { 36 return BINARY 37} 38 39func (this binaryValue) Actual() interface{} { 40 return []byte(this) 41} 42 43func (this binaryValue) ActualForIndex() interface{} { 44 return []byte(this) 45} 46 47func (this binaryValue) Equals(other Value) Value { 48 other = other.unwrap() 49 switch other := other.(type) { 50 case missingValue: 51 return other 52 case *nullValue: 53 return other 54 case binaryValue: 55 if bytes.Equal(this, other) { 56 return TRUE_VALUE 57 } 58 } 59 60 return FALSE_VALUE 61} 62 63func (this binaryValue) EquivalentTo(other Value) bool { 64 other = other.unwrap() 65 switch other := other.(type) { 66 case binaryValue: 67 return bytes.Equal(this, other) 68 default: 69 return false 70 } 71} 72 73func (this binaryValue) Collate(other Value) int { 74 other = other.unwrap() 75 switch other := other.(type) { 76 case binaryValue: 77 return bytes.Compare(this, other) 78 default: 79 return int(BINARY - other.Type()) 80 } 81} 82 83func (this binaryValue) Compare(other Value) Value { 84 other = other.unwrap() 85 switch other := other.(type) { 86 case missingValue: 87 return other 88 case *nullValue: 89 return other 90 default: 91 return intValue(this.Collate(other)) 92 } 93} 94 95func (this binaryValue) Truth() bool { 96 return len(this) > 0 97} 98 99func (this binaryValue) Copy() Value { 100 return this 101} 102 103func (this binaryValue) CopyForUpdate() Value { 104 return this 105} 106 107func (this binaryValue) Field(field string) (Value, bool) { 108 return missingField(field), false 109} 110 111func (this binaryValue) SetField(field string, val interface{}) error { 112 return Unsettable(field) 113} 114 115func (this binaryValue) UnsetField(field string) error { 116 return Unsettable(field) 117} 118 119func (this binaryValue) Index(index int) (Value, bool) { 120 return missingIndex(index), false 121} 122 123func (this binaryValue) SetIndex(index int, val interface{}) error { 124 return Unsettable(index) 125} 126 127func (this binaryValue) Slice(start, end int) (Value, bool) { 128 return NULL_VALUE, false 129} 130 131func (this binaryValue) SliceTail(start int) (Value, bool) { 132 return NULL_VALUE, false 133} 134 135func (this binaryValue) Descendants(buffer []interface{}) []interface{} { 136 return buffer 137} 138 139func (this binaryValue) Fields() map[string]interface{} { 140 return nil 141} 142 143func (this binaryValue) FieldNames(buffer []string) []string { 144 return nil 145} 146 147func (this binaryValue) DescendantPairs(buffer []util.IPair) []util.IPair { 148 return buffer 149} 150 151func (this binaryValue) Successor() Value { 152 return binaryValue(append(this, byte(0))) 153} 154 155func (this binaryValue) Recycle() { 156} 157 158func (this binaryValue) Tokens(set *Set, options Value) *Set { 159 set.Add(this) 160 return set 161} 162 163func (this binaryValue) ContainsToken(token, options Value) bool { 164 return this.EquivalentTo(token) 165} 166 167func (this binaryValue) ContainsMatchingToken(matcher MatchFunc, options Value) bool { 168 return matcher([]byte(this)) 169} 170 171func (this binaryValue) unwrap() Value { 172 return this 173} 174