Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

114 lignes
3.0 KiB

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