forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoutils.go
122 lines (92 loc) · 3.19 KB
/
protoutils.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
/*
Copyright 2017 Google LLC
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 spanner
import (
"encoding/base64"
"math/big"
"strconv"
"time"
"cloud.google.com/go/civil"
proto3 "github.com/golang/protobuf/ptypes/struct"
sppb "google.golang.org/genproto/googleapis/spanner/v1"
)
// Helpers to generate protobuf values and Cloud Spanner types.
func stringProto(s string) *proto3.Value {
return &proto3.Value{Kind: stringKind(s)}
}
func stringKind(s string) *proto3.Value_StringValue {
return &proto3.Value_StringValue{StringValue: s}
}
func stringType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_STRING}
}
func boolProto(b bool) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_BoolValue{BoolValue: b}}
}
func boolType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_BOOL}
}
func intProto(n int64) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: strconv.FormatInt(n, 10)}}
}
func intType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_INT64}
}
func floatProto(n float64) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_NumberValue{NumberValue: n}}
}
func floatType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_FLOAT64}
}
func numericProto(n *big.Rat) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: NumericString(n)}}
}
func numericType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_NUMERIC}
}
func bytesProto(b []byte) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString(b)}}
}
func bytesType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_BYTES}
}
func timeProto(t time.Time) *proto3.Value {
return stringProto(t.UTC().Format(time.RFC3339Nano))
}
func timeType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_TIMESTAMP}
}
func dateProto(d civil.Date) *proto3.Value {
return stringProto(d.String())
}
func dateType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_DATE}
}
func listProto(p ...*proto3.Value) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_ListValue{ListValue: &proto3.ListValue{Values: p}}}
}
func listValueProto(p ...*proto3.Value) *proto3.ListValue {
return &proto3.ListValue{Values: p}
}
func listType(t *sppb.Type) *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_ARRAY, ArrayElementType: t}
}
func mkField(n string, t *sppb.Type) *sppb.StructType_Field {
return &sppb.StructType_Field{Name: n, Type: t}
}
func structType(fields ...*sppb.StructType_Field) *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_STRUCT, StructType: &sppb.StructType{Fields: fields}}
}
func nullProto() *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE}}
}