forked from rethinkdb/rethinkdb-go
/
datum.go
154 lines (138 loc) · 3.64 KB
/
datum.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
package gorethink
import (
"code.google.com/p/goprotobuf/proto"
"fmt"
p "github.com/dancannon/gorethink/ql2"
"reflect"
)
// Converts a query term to a datum. If the term cannot be converted to a datum
// object then the function panics.
func constructDatum(t RqlTerm) (*p.Term, error) {
if t.data == nil {
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_NULL.Enum(),
},
}, nil
} else {
typ := reflect.TypeOf(t.data)
switch typ.Kind() {
case reflect.Bool:
// Cast value to string
rv := reflect.ValueOf(t.data)
fv := rv.Bool()
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_BOOL.Enum(),
RBool: proto.Bool(fv),
},
}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// Cast value to float64
rv := reflect.ValueOf(t.data)
fv := float64(rv.Uint())
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_NUM.Enum(),
RNum: proto.Float64(fv),
},
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// Cast value to float64
rv := reflect.ValueOf(t.data)
fv := float64(rv.Int())
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_NUM.Enum(),
RNum: proto.Float64(fv),
},
}, nil
case reflect.Float32, reflect.Float64:
// Cast value to float64
rv := reflect.ValueOf(t.data)
fv := rv.Float()
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_NUM.Enum(),
RNum: proto.Float64(fv),
},
}, nil
case reflect.String:
// Cast value to string
rv := reflect.ValueOf(t.data)
fv := rv.String()
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_STR.Enum(),
RStr: proto.String(fv),
},
}, nil
default:
return &p.Term{}, fmt.Errorf("Cannot convert type '%T' to Datum", t.data)
}
}
}
// deconstructDatum converts a datum object to an arbitrary type
func deconstructDatum(datum *p.Datum, opts map[string]interface{}) (interface{}, error) {
switch datum.GetType() {
case p.Datum_R_NULL:
return nil, nil
case p.Datum_R_BOOL:
return datum.GetRBool(), nil
case p.Datum_R_NUM:
return datum.GetRNum(), nil
case p.Datum_R_STR:
return datum.GetRStr(), nil
case p.Datum_R_ARRAY:
items := []interface{}{}
for _, d := range datum.GetRArray() {
item, err := deconstructDatum(d, opts)
if err != nil {
return nil, err
}
items = append(items, item)
}
return items, nil
case p.Datum_R_OBJECT:
obj := map[interface{}]interface{}{}
for _, assoc := range datum.GetRObject() {
key := assoc.GetKey()
val, err := deconstructDatum(assoc.GetVal(), opts)
if err != nil {
return nil, err
}
obj[key] = val
}
// Handle ReQL pseudo-types
if reqlType, ok := obj["$reql_type$"]; ok {
if reqlType == "TIME" {
// load timeformat, set to native if the option was not set
timeFormat := "native"
if opt, ok := opts["time_format"]; ok {
if sopt, ok := opt.(string); ok {
timeFormat = sopt
} else {
return nil, fmt.Errorf("Invalid time_format run option \"%s\".", opt)
}
}
if timeFormat == "native" {
return reqlTimeToNativeTime(obj["epoch_time"].(float64), obj["timezone"].(string))
} else if timeFormat == "raw" {
return obj, nil
} else {
return nil, fmt.Errorf("Unknown time_format run option \"%s\".", reqlType)
}
} else {
return obj, nil
}
}
return obj, nil
}
return nil, fmt.Errorf("Unknown Datum type %s encountered in response.", datum.GetType().String())
}