-
Notifications
You must be signed in to change notification settings - Fork 28
/
conversions.go
172 lines (161 loc) · 4.98 KB
/
conversions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package conversions
import (
"errors"
"math/big"
"strings"
"v.io/v23/query/engine/internal/query_parser"
)
func ConvertValueToString(o *query_parser.Operand) (*query_parser.Operand, error) {
// Other types must be explicitly converted to string with the Str() function.
var c query_parser.Operand
c.Type = query_parser.TypStr
c.Off = o.Off
switch o.Type {
case query_parser.TypStr:
c.Str = o.Str
c.Prefix = o.Prefix // non-nil for rhs of like expressions
c.Pattern = o.Pattern // non-nil for rhs of like expressions
default:
return nil, errors.New("Cannot convert operand to string.")
}
return &c, nil
}
func ConvertValueToTime(o *query_parser.Operand) (*query_parser.Operand, error) {
switch o.Type {
case query_parser.TypTime:
return o, nil
default:
return nil, errors.New("Cannot convert operand to time.")
}
}
func ConvertValueToBigRat(o *query_parser.Operand) (*query_parser.Operand, error) {
// operand cannot be string literal.
var c query_parser.Operand
c.Type = query_parser.TypBigRat
switch o.Type {
case query_parser.TypBigInt:
var b big.Rat
c.BigRat = b.SetInt(o.BigInt)
case query_parser.TypBigRat:
c.BigRat = o.BigRat
case query_parser.TypBool:
return nil, errors.New("Cannot convert bool to big.Rat.")
case query_parser.TypFloat:
var b big.Rat
c.BigRat = b.SetFloat64(o.Float)
case query_parser.TypInt:
c.BigRat = big.NewRat(o.Int, 1)
case query_parser.TypUint:
var bi big.Int
bi.SetUint64(o.Uint)
var br big.Rat
c.BigRat = br.SetInt(&bi)
case query_parser.TypObject:
return nil, errors.New("Cannot convert object to big.Rat.")
default:
// TODO(jkline): Log this logic error and all other similar cases.
return nil, errors.New("Cannot convert operand to big.Rat.")
}
return &c, nil
}
func ConvertValueToFloat(o *query_parser.Operand) (*query_parser.Operand, error) {
// Operand cannot be literal, big.Rat or big.Int
var c query_parser.Operand
c.Type = query_parser.TypFloat
switch o.Type {
case query_parser.TypBool:
return nil, errors.New("Cannot convert bool to float64.")
case query_parser.TypFloat:
c.Float = o.Float
case query_parser.TypInt:
c.Float = float64(o.Int)
case query_parser.TypUint:
c.Float = float64(o.Uint)
case query_parser.TypObject:
return nil, errors.New("Cannot convert object to float64.")
default:
// TODO(jkline): Log this logic error and all other similar cases.
return nil, errors.New("Cannot convert operand to float64.")
}
return &c, nil
}
func ConvertValueToBool(o *query_parser.Operand) (*query_parser.Operand, error) {
var c query_parser.Operand
c.Type = query_parser.TypBool
switch o.Type {
case query_parser.TypBool:
c.Bool = o.Bool
case query_parser.TypStr:
switch strings.ToLower(o.Str) {
case "true":
c.Bool = true
case "false":
c.Bool = false
default:
return nil, errors.New("Cannot convert object to bool.")
}
default:
return nil, errors.New("Cannot convert operand to bool.")
}
return &c, nil
}
func ConvertValueToBigInt(o *query_parser.Operand) (*query_parser.Operand, error) {
// Operand cannot be literal, big.Rat or float.
var c query_parser.Operand
c.Type = query_parser.TypBigInt
switch o.Type {
case query_parser.TypBigInt:
c.BigInt = o.BigInt
case query_parser.TypBool:
return nil, errors.New("Cannot convert bool to big.Int.")
case query_parser.TypInt:
c.BigInt = big.NewInt(o.Int)
case query_parser.TypUint:
var b big.Int
b.SetUint64(o.Uint)
c.BigInt = &b
case query_parser.TypObject:
return nil, errors.New("Cannot convert object to big.Int.")
default:
// TODO(jkline): Log this logic error and all other similar cases.
return nil, errors.New("Cannot convert operand to big.Int.")
}
return &c, nil
}
func ConvertValueToInt(o *query_parser.Operand) (*query_parser.Operand, error) {
// Operand cannot be literal, big.Rat or float or uint64.
var c query_parser.Operand
c.Type = query_parser.TypInt
switch o.Type {
case query_parser.TypBool:
return nil, errors.New("Cannot convert bool to int64.")
case query_parser.TypInt:
c.Int = o.Int
case query_parser.TypObject:
return nil, errors.New("Cannot convert object to int64.")
default:
// TODO(jkline): Log this logic error and all other similar cases.
return nil, errors.New("Cannot convert operand to int64.")
}
return &c, nil
}
func ConvertValueToUint(o *query_parser.Operand) (*query_parser.Operand, error) {
// Operand cannot be literal, big.Rat or float or int64.
var c query_parser.Operand
c.Type = query_parser.TypUint
switch o.Type {
case query_parser.TypBool:
return nil, errors.New("Cannot convert bool to int64.")
case query_parser.TypUint:
c.Uint = o.Uint
case query_parser.TypObject:
return nil, errors.New("Cannot convert object to int64.")
default:
// TODO(jkline): Log this logic error and all other similar cases.
return nil, errors.New("Cannot convert operand to int64.")
}
return &c, nil
}