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	"strconv"
22	"time"
23
24	"cloud.google.com/go/civil"
25	proto3 "github.com/golang/protobuf/ptypes/struct"
26	sppb "google.golang.org/genproto/googleapis/spanner/v1"
27)
28
29// Helpers to generate protobuf values and Cloud Spanner types.
30
31func stringProto(s string) *proto3.Value {
32	return &proto3.Value{Kind: stringKind(s)}
33}
34
35func stringKind(s string) *proto3.Value_StringValue {
36	return &proto3.Value_StringValue{StringValue: s}
37}
38
39func stringType() *sppb.Type {
40	return &sppb.Type{Code: sppb.TypeCode_STRING}
41}
42
43func boolProto(b bool) *proto3.Value {
44	return &proto3.Value{Kind: &proto3.Value_BoolValue{BoolValue: b}}
45}
46
47func boolType() *sppb.Type {
48	return &sppb.Type{Code: sppb.TypeCode_BOOL}
49}
50
51func intProto(n int64) *proto3.Value {
52	return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: strconv.FormatInt(n, 10)}}
53}
54
55func intType() *sppb.Type {
56	return &sppb.Type{Code: sppb.TypeCode_INT64}
57}
58
59func floatProto(n float64) *proto3.Value {
60	return &proto3.Value{Kind: &proto3.Value_NumberValue{NumberValue: n}}
61}
62
63func floatType() *sppb.Type {
64	return &sppb.Type{Code: sppb.TypeCode_FLOAT64}
65}
66
67func bytesProto(b []byte) *proto3.Value {
68	return &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString(b)}}
69}
70
71func bytesType() *sppb.Type {
72	return &sppb.Type{Code: sppb.TypeCode_BYTES}
73}
74
75func timeProto(t time.Time) *proto3.Value {
76	return stringProto(t.UTC().Format(time.RFC3339Nano))
77}
78
79func timeType() *sppb.Type {
80	return &sppb.Type{Code: sppb.TypeCode_TIMESTAMP}
81}
82
83func dateProto(d civil.Date) *proto3.Value {
84	return stringProto(d.String())
85}
86
87func dateType() *sppb.Type {
88	return &sppb.Type{Code: sppb.TypeCode_DATE}
89}
90
91func listProto(p ...*proto3.Value) *proto3.Value {
92	return &proto3.Value{Kind: &proto3.Value_ListValue{ListValue: &proto3.ListValue{Values: p}}}
93}
94
95func listValueProto(p ...*proto3.Value) *proto3.ListValue {
96	return &proto3.ListValue{Values: p}
97}
98
99func listType(t *sppb.Type) *sppb.Type {
100	return &sppb.Type{Code: sppb.TypeCode_ARRAY, ArrayElementType: t}
101}
102
103func mkField(n string, t *sppb.Type) *sppb.StructType_Field {
104	return &sppb.StructType_Field{Name: n, Type: t}
105}
106
107func structType(fields ...*sppb.StructType_Field) *sppb.Type {
108	return &sppb.Type{Code: sppb.TypeCode_STRUCT, StructType: &sppb.StructType{Fields: fields}}
109}
110
111func nullProto() *proto3.Value {
112	return &proto3.Value{Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE}}
113}
114