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 jsonType() *sppb.Type {
77	return &sppb.Type{Code: sppb.TypeCode_JSON}
78}
79
80func bytesProto(b []byte) *proto3.Value {
81	return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString(b)}}
82}
83
84func bytesType() *sppb.Type {
85	return &sppb.Type{Code: sppb.TypeCode_BYTES}
86}
87
88func timeProto(t time.Time) *proto3.Value {
89	return stringProto(t.UTC().Format(time.RFC3339Nano))
90}
91
92func timeType() *sppb.Type {
93	return &sppb.Type{Code: sppb.TypeCode_TIMESTAMP}
94}
95
96func dateProto(d civil.Date) *proto3.Value {
97	return stringProto(d.String())
98}
99
100func dateType() *sppb.Type {
101	return &sppb.Type{Code: sppb.TypeCode_DATE}
102}
103
104func listProto(p ...*proto3.Value) *proto3.Value {
105	return &proto3.Value{Kind: &proto3.Value_ListValue{ListValue: &proto3.ListValue{Values: p}}}
106}
107
108func listValueProto(p ...*proto3.Value) *proto3.ListValue {
109	return &proto3.ListValue{Values: p}
110}
111
112func listType(t *sppb.Type) *sppb.Type {
113	return &sppb.Type{Code: sppb.TypeCode_ARRAY, ArrayElementType: t}
114}
115
116func mkField(n string, t *sppb.Type) *sppb.StructType_Field {
117	return &sppb.StructType_Field{Name: n, Type: t}
118}
119
120func structType(fields ...*sppb.StructType_Field) *sppb.Type {
121	return &sppb.Type{Code: sppb.TypeCode_STRUCT, StructType: &sppb.StructType{Fields: fields}}
122}
123
124func nullProto() *proto3.Value {
125	return &proto3.Value{Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE}}
126}
127