1/*
2Copyright 2017 Google LLC
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package spanner
18
19import (
20	"encoding/base64"
21	"math/big"
22	"strconv"
23	"time"
24
25	"cloud.google.com/go/civil"
26	proto3 "github.com/golang/protobuf/ptypes/struct"
27	sppb "google.golang.org/genproto/googleapis/spanner/v1"
28)
29
30// Helpers to generate protobuf values and Cloud Spanner types.
31
32func stringProto(s string) *proto3.Value {
33	return &proto3.Value{Kind: stringKind(s)}
34}
35
36func stringKind(s string) *proto3.Value_StringValue {
37	return &proto3.Value_StringValue{StringValue: s}
38}
39
40func stringType() *sppb.Type {
41	return &sppb.Type{Code: sppb.TypeCode_STRING}
42}
43
44func boolProto(b bool) *proto3.Value {
45	return &proto3.Value{Kind: &proto3.Value_BoolValue{BoolValue: b}}
46}
47
48func boolType() *sppb.Type {
49	return &sppb.Type{Code: sppb.TypeCode_BOOL}
50}
51
52func intProto(n int64) *proto3.Value {
53	return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: strconv.FormatInt(n, 10)}}
54}
55
56func intType() *sppb.Type {
57	return &sppb.Type{Code: sppb.TypeCode_INT64}
58}
59
60func floatProto(n float64) *proto3.Value {
61	return &proto3.Value{Kind: &proto3.Value_NumberValue{NumberValue: n}}
62}
63
64func floatType() *sppb.Type {
65	return &sppb.Type{Code: sppb.TypeCode_FLOAT64}
66}
67
68func numericProto(n *big.Rat) *proto3.Value {
69	return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: NumericString(n)}}
70}
71
72func numericType() *sppb.Type {
73	return &sppb.Type{Code: sppb.TypeCode_NUMERIC}
74}
75
76func bytesProto(b []byte) *proto3.Value {
77	return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString(b)}}
78}
79
80func bytesType() *sppb.Type {
81	return &sppb.Type{Code: sppb.TypeCode_BYTES}
82}
83
84func timeProto(t time.Time) *proto3.Value {
85	return stringProto(t.UTC().Format(time.RFC3339Nano))
86}
87
88func timeType() *sppb.Type {
89	return &sppb.Type{Code: sppb.TypeCode_TIMESTAMP}
90}
91
92func dateProto(d civil.Date) *proto3.Value {
93	return stringProto(d.String())
94}
95
96func dateType() *sppb.Type {
97	return &sppb.Type{Code: sppb.TypeCode_DATE}
98}
99
100func listProto(p ...*proto3.Value) *proto3.Value {
101	return &proto3.Value{Kind: &proto3.Value_ListValue{ListValue: &proto3.ListValue{Values: p}}}
102}
103
104func listValueProto(p ...*proto3.Value) *proto3.ListValue {
105	return &proto3.ListValue{Values: p}
106}
107
108func listType(t *sppb.Type) *sppb.Type {
109	return &sppb.Type{Code: sppb.TypeCode_ARRAY, ArrayElementType: t}
110}
111
112func mkField(n string, t *sppb.Type) *sppb.StructType_Field {
113	return &sppb.StructType_Field{Name: n, Type: t}
114}
115
116func structType(fields ...*sppb.StructType_Field) *sppb.Type {
117	return &sppb.Type{Code: sppb.TypeCode_STRUCT, StructType: &sppb.StructType{Fields: fields}}
118}
119
120func nullProto() *proto3.Value {
121	return &proto3.Value{Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE}}
122}
123