forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
120 lines (96 loc) · 2.74 KB
/
util.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
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package client
import (
"fmt"
"reflect"
"time"
"github.com/gogo/protobuf/proto"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/duration"
)
// TODO(pmattis): The methods in this file needs tests.
func marshalKey(k interface{}) (roachpb.Key, error) {
switch t := k.(type) {
case *roachpb.Key:
return *t, nil
case roachpb.Key:
return t, nil
case string:
return roachpb.Key(t), nil
case []byte:
return roachpb.Key(t), nil
}
return nil, fmt.Errorf("unable to marshal key: %T %q", k, k)
}
// marshalValue returns a roachpb.Value initialized from the source
// interface{}, returning an error if the types are not compatible.
func marshalValue(v interface{}) (roachpb.Value, error) {
var r roachpb.Value
// Handle a few common types via a type switch.
switch t := v.(type) {
case *roachpb.Value:
return *t, nil
case nil:
return r, nil
case bool:
r.SetBool(t)
return r, nil
case string:
r.SetBytes([]byte(t))
return r, nil
case []byte:
r.SetBytes(t)
return r, nil
case apd.Decimal:
err := r.SetDecimal(&t)
return r, err
case roachpb.Key:
r.SetBytes([]byte(t))
return r, nil
case time.Time:
r.SetTime(t)
return r, nil
case duration.Duration:
err := r.SetDuration(t)
return r, err
case proto.Message:
err := r.SetProto(t)
return r, err
case roachpb.Value:
panic("unexpected type roachpb.Value (use *roachpb.Value)")
}
// Handle all of the Go primitive types besides struct and pointers. This
// switch also handles types based on a primitive type (e.g. "type MyInt
// int").
switch v := reflect.ValueOf(v); v.Kind() {
case reflect.Bool:
r.SetBool(v.Bool())
return r, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
r.SetInt(v.Int())
return r, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
r.SetInt(int64(v.Uint()))
return r, nil
case reflect.Float32, reflect.Float64:
r.SetFloat(v.Float())
return r, nil
case reflect.String:
r.SetBytes([]byte(v.String()))
return r, nil
}
return r, fmt.Errorf("unable to marshal %T: %v", v, v)
}