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	"io"
14	"math"
15
16	"github.com/couchbase/query/util"
17)
18
19/*
20BoolValue is defined as a bool type.
21*/
22type boolValue bool
23
24var FALSE_VALUE Value = boolValue(false)
25var TRUE_VALUE Value = boolValue(true)
26
27/*
28_FALSE _BYTES / _TRUE _BYTES that are slices of bytes
29representing false and true.
30*/
31var _FALSE_BYTES = []byte("false")
32var _TRUE_BYTES = []byte("true")
33
34func (this boolValue) String() string {
35	if this {
36		return "true"
37	} else {
38		return "false"
39	}
40}
41
42func (this boolValue) MarshalJSON() ([]byte, error) {
43	if this {
44		return _TRUE_BYTES, nil
45	} else {
46		return _FALSE_BYTES, nil
47	}
48}
49
50func (this boolValue) WriteJSON(w io.Writer, prefix, indent string) error {
51	b, err := this.MarshalJSON()
52	if err != nil {
53		return err
54	}
55	_, err = w.Write(b)
56	return err
57}
58
59/*
60Type BOOLEAN
61*/
62func (this boolValue) Type() Type {
63	return BOOLEAN
64}
65
66func (this boolValue) Actual() interface{} {
67	return bool(this)
68}
69
70func (this boolValue) ActualForIndex() interface{} {
71	return bool(this)
72}
73
74func (this boolValue) Equals(other Value) Value {
75	other = other.unwrap()
76	switch other := other.(type) {
77	case missingValue:
78		return other
79	case *nullValue:
80		return other
81	case boolValue:
82		if this == other {
83			return TRUE_VALUE
84		}
85	}
86
87	return FALSE_VALUE
88}
89
90func (this boolValue) EquivalentTo(other Value) bool {
91	other = other.unwrap()
92	switch other := other.(type) {
93	case boolValue:
94		return this == other
95	default:
96		return false
97	}
98}
99
100func (this boolValue) Collate(other Value) int {
101	other = other.unwrap()
102	switch other := other.(type) {
103	case boolValue:
104		if this == other {
105			return 0
106		} else if !this {
107			return -1
108		} else {
109			return 1
110		}
111	default:
112		return int(BOOLEAN - other.Type())
113	}
114}
115
116func (this boolValue) Compare(other Value) Value {
117	other = other.unwrap()
118	switch other := other.(type) {
119	case missingValue:
120		return other
121	case *nullValue:
122		return other
123	default:
124		return intValue(this.Collate(other))
125	}
126}
127
128/*
129Cast receiver to bool and return.
130*/
131func (this boolValue) Truth() bool {
132	return bool(this)
133}
134
135/*
136Return receiver.
137*/
138func (this boolValue) Copy() Value {
139	return this
140}
141
142/*
143Return receiver.
144*/
145func (this boolValue) CopyForUpdate() Value {
146	return this
147}
148
149/*
150Calls missingField.
151*/
152func (this boolValue) Field(field string) (Value, bool) {
153	return missingField(field), false
154}
155
156/*
157Not valid for bool.
158*/
159func (this boolValue) SetField(field string, val interface{}) error {
160	return Unsettable(field)
161}
162
163/*
164Not valid for bool.
165*/
166func (this boolValue) UnsetField(field string) error {
167	return Unsettable(field)
168}
169
170/*
171Calls missingIndex.
172*/
173func (this boolValue) Index(index int) (Value, bool) {
174	return missingIndex(index), false
175}
176
177/*
178Not valid for bool.
179*/
180func (this boolValue) SetIndex(index int, val interface{}) error {
181	return Unsettable(index)
182}
183
184/*
185Returns NULL_VALUE
186*/
187func (this boolValue) Slice(start, end int) (Value, bool) {
188	return NULL_VALUE, false
189}
190
191/*
192Returns NULL_VALUE
193*/
194func (this boolValue) SliceTail(start int) (Value, bool) {
195	return NULL_VALUE, false
196}
197
198/*
199Returns the input buffer as is.
200*/
201func (this boolValue) Descendants(buffer []interface{}) []interface{} {
202	return buffer
203}
204
205/*
206Bool has no fields to list. Hence return nil.
207*/
208func (this boolValue) Fields() map[string]interface{} {
209	return nil
210}
211
212func (this boolValue) FieldNames(buffer []string) []string {
213	return nil
214}
215
216/*
217Returns the input buffer as is.
218*/
219func (this boolValue) DescendantPairs(buffer []util.IPair) []util.IPair {
220	return buffer
221}
222
223/*
224FALSE is succeeded by TRUE, TRUE by numbers.
225*/
226func (this boolValue) Successor() Value {
227	if bool(this) {
228		return _MIN_NUMBER_VALUE
229	} else {
230		return TRUE_VALUE
231	}
232}
233
234func (this boolValue) Recycle() {
235}
236
237func (this boolValue) Tokens(set *Set, options Value) *Set {
238	set.Add(this)
239	return set
240}
241
242func (this boolValue) ContainsToken(token, options Value) bool {
243	return this.EquivalentTo(token)
244}
245
246func (this boolValue) ContainsMatchingToken(matcher MatchFunc, options Value) bool {
247	return matcher(bool(this))
248}
249
250func (this boolValue) unwrap() Value {
251	return this
252}
253
254var _MIN_NUMBER_VALUE = floatValue(-math.MaxFloat64)
255