google-cloud-spanner 0.34.1-preview

Google Cloud Client Libraries for Rust - Spanner
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2026 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
//
//     https://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.

// TODO(#4969)
#![allow(dead_code)]

use crate::generated::gapic_dataplane::model::TypeAnnotationCode;
use gaxi::prost::ConvertError;
use std::sync::LazyLock;

/// Spanner type definition.
#[derive(Clone, Debug, PartialEq, Default)]
#[repr(transparent)]
pub struct Type(pub(crate) crate::generated::gapic_dataplane::model::Type);

macro_rules! define_type_code {
    ($($variant:ident = $val:expr),* $(,)?) => {
        /// Spanner type code.
        ///
        /// Maps directly to the gRPC type codes defined in
        /// [`crate::generated::gapic_dataplane::model::TypeCode`].
        #[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Default)]
        #[repr(i32)]
        #[non_exhaustive]
        pub enum TypeCode {
            /// Not specified.
            #[default]
            Unspecified = 0,
            $(
                #[doc = concat!("Spanner `", stringify!($variant), "` data type.")]
                $variant = $val
            ),*,
            /// An unknown or unsupported type code value.
            Unknown(i32),
        }

        impl From<i32> for TypeCode {
            fn from(value: i32) -> Self {
                match value {
                    0 => TypeCode::Unspecified,
                    $($val => TypeCode::$variant),*,
                    v => TypeCode::Unknown(v),
                }
            }
        }

        impl From<TypeCode> for i32 {
            fn from(value: TypeCode) -> Self {
                match value {
                    TypeCode::Unspecified => 0,
                    $(TypeCode::$variant => $val),*,
                    TypeCode::Unknown(v) => v,
                }
            }
        }

        impl From<crate::generated::gapic_dataplane::model::TypeCode> for TypeCode {
            fn from(value: crate::generated::gapic_dataplane::model::TypeCode) -> Self {
                 match value.value() {
                    Some(v) => v.into(),
                    None => TypeCode::Unspecified,
                }
            }
        }

        impl From<TypeCode> for crate::generated::gapic_dataplane::model::TypeCode {
            fn from(value: TypeCode) -> Self {
                let v: i32 = value.into();
                v.into()
            }
        }
    };
}

// The values here must match the values in the `google.spanner.v1.TypeCode` enum.
// We cannot use the generated constants directly because they are not exposed as public constants.
// See https://github.com/googleapis/googleapis/blob/master/google/spanner/v1/type.proto
define_type_code!(
    Bool = 1,
    Int64 = 2,
    Float64 = 3,
    Float32 = 15,
    Timestamp = 4,
    Date = 5,
    String = 6,
    Bytes = 7,
    Array = 8,
    Struct = 9,
    Numeric = 10,
    Json = 11,
    Proto = 13,
    Enum = 14,
    Interval = 16,
    Uuid = 17,
);

impl From<crate::generated::gapic_dataplane::model::Type> for Type {
    fn from(value: crate::generated::gapic_dataplane::model::Type) -> Self {
        Type(value)
    }
}

impl From<Type> for crate::generated::gapic_dataplane::model::Type {
    fn from(value: Type) -> Self {
        value.0
    }
}

impl From<crate::google::spanner::v1::Type> for Type {
    fn from(value: crate::google::spanner::v1::Type) -> Self {
        use gaxi::prost::FromProto;
        value.cnv().unwrap_or_default().into()
    }
}

impl Type {
    /// Returns the type code.
    pub fn code(&self) -> TypeCode {
        self.0.code.clone().into()
    }

    /// Returns the element type of the array, or `None` if this type is not an array.
    pub fn array_element_type(&self) -> Option<Type> {
        self.0
            .array_element_type
            .as_deref()
            .map(|t| Type(t.clone()))
    }
}

impl gaxi::prost::ToProto<i32> for TypeCode {
    type Output = i32;

    fn to_proto(self) -> Result<i32, ConvertError> {
        let internal: crate::generated::gapic_dataplane::model::TypeCode = self.into();

        internal.to_proto()
    }
}

impl gaxi::prost::ToProto<crate::generated::gapic_dataplane::model::Type> for Type {
    type Output = crate::generated::gapic_dataplane::model::Type;

    fn to_proto(self) -> Result<crate::generated::gapic_dataplane::model::Type, ConvertError> {
        Ok(self.0)
    }
}

static TYPE_INT64: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Int64));
static TYPE_STRING: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::String));
static TYPE_BOOL: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Bool));
static TYPE_FLOAT32: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Float32));
static TYPE_FLOAT64: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Float64));
static TYPE_JSON: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Json));
static TYPE_BYTES: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Bytes));
static TYPE_TIMESTAMP: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Timestamp));
static TYPE_DATE: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Date));
static TYPE_NUMERIC: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Numeric));
static TYPE_UUID: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Uuid));
static TYPE_INTERVAL: LazyLock<Type> = LazyLock::new(|| create_type(TypeCode::Interval));
static TYPE_PG_NUMERIC: LazyLock<Type> = LazyLock::new(|| {
    let mut t = create_type(TypeCode::Numeric);
    t.0.type_annotation = TypeAnnotationCode::PgNumeric;
    t
});
static TYPE_PG_JSONB: LazyLock<Type> = LazyLock::new(|| {
    let mut t = create_type(TypeCode::Json);
    t.0.type_annotation = TypeAnnotationCode::PgJsonb;
    t
});
static TYPE_PG_OID: LazyLock<Type> = LazyLock::new(|| {
    let mut t = create_type(TypeCode::Int64);
    t.0.type_annotation = TypeAnnotationCode::PgOid;
    t
});

/// Returns a `Type` representing `INT64` (GoogleSQL) or `bigint` (PostgreSQL).
pub fn int64() -> Type {
    TYPE_INT64.clone()
}

/// Returns a `Type` representing `STRING` (GoogleSQL) or `character varying` (PostgreSQL).
pub fn string() -> Type {
    TYPE_STRING.clone()
}

/// Returns a `Type` representing `BOOL` (GoogleSQL) or `boolean` (PostgreSQL).
pub fn bool() -> Type {
    TYPE_BOOL.clone()
}

/// Returns a `Type` representing `FLOAT32` (GoogleSQL) or `real` (PostgreSQL).
pub fn float32() -> Type {
    TYPE_FLOAT32.clone()
}

/// Returns a `Type` representing `FLOAT64` (GoogleSQL) or `double precision` (PostgreSQL).
pub fn float64() -> Type {
    TYPE_FLOAT64.clone()
}

/// Returns a `Type` representing `JSON` (GoogleSQL).
pub fn json() -> Type {
    TYPE_JSON.clone()
}

/// Returns a `Type` representing `BYTES` (GoogleSQL) or `bytea` (PostgreSQL).
pub fn bytes() -> Type {
    TYPE_BYTES.clone()
}

/// Returns a `Type` representing `TIMESTAMP` (GoogleSQL) or `timestamp with time zone` (PostgreSQL).
pub fn timestamp() -> Type {
    TYPE_TIMESTAMP.clone()
}

/// Returns a `Type` representing `DATE` (GoogleSQL) or `date` (PostgreSQL).
pub fn date() -> Type {
    TYPE_DATE.clone()
}

/// Returns a `Type` representing `NUMERIC` (GoogleSQL) or `numeric` (PostgreSQL).
pub fn numeric() -> Type {
    TYPE_NUMERIC.clone()
}

/// Returns a `Type` representing `UUID` (GoogleSQL) or `uuid` (PostgreSQL).
pub fn uuid() -> Type {
    TYPE_UUID.clone()
}

/// Returns a `Type` representing `INTERVAL` (GoogleSQL) or `interval` (PostgreSQL).
pub fn interval() -> Type {
    TYPE_INTERVAL.clone()
}

/// Returns a `Type` representing `numeric` (PostgreSQL).
pub fn pg_numeric() -> Type {
    TYPE_PG_NUMERIC.clone()
}

/// Returns a `Type` representing `jsonb` (PostgreSQL).
pub fn pg_jsonb() -> Type {
    TYPE_PG_JSONB.clone()
}

/// Returns a `Type` representing `oid` (PostgreSQL).
pub fn pg_oid() -> Type {
    TYPE_PG_OID.clone()
}

/// Returns a `Type` representing `ARRAY<t>` (GoogleSQL) or `t[]` (PostgreSQL).
pub fn array(element_type: Type) -> Type {
    let mut t = create_type(TypeCode::Array);
    t.0.array_element_type = Some(Box::new(element_type.0));
    t
}

pub(crate) fn create_type(code: TypeCode) -> Type {
    Type(crate::generated::gapic_dataplane::model::Type {
        code: code.into(),
        array_element_type: None,
        struct_type: None,
        type_annotation: TypeAnnotationCode::Unspecified,
        proto_type_fqn: String::new(),
        _unknown_fields: Default::default(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::hash::Hash;

    #[test]
    fn test_type_code_round_trip() {
        let codes = vec![
            TypeCode::Unspecified,
            TypeCode::Bool,
            TypeCode::Int64,
            TypeCode::Float64,
            TypeCode::Float32,
            TypeCode::Timestamp,
            TypeCode::Date,
            TypeCode::String,
            TypeCode::Bytes,
            TypeCode::Array,
            TypeCode::Struct,
            TypeCode::Numeric,
            TypeCode::Json,
            TypeCode::Proto,
            TypeCode::Enum,
            TypeCode::Interval,
            TypeCode::Uuid,
        ];

        for code in codes {
            let i: i32 = code.into();
            let c: TypeCode = i.into();
            assert_eq!(code, c);

            let generated: crate::generated::gapic_dataplane::model::TypeCode = code.into();
            let back: TypeCode = generated.into();
            assert_eq!(code, back);
        }
    }

    #[test]
    fn test_unknown_type_code() {
        let i = 999;
        let code: TypeCode = i.into();
        assert_eq!(code, TypeCode::Unknown(i));
        assert_eq!(i32::from(code), i);
    }

    #[test]
    fn test_unknown_type_code_from_generated() {
        use crate::generated::gapic_dataplane::model::type_code::UnknownValue;
        use wkt::internal::UnknownEnumValue;

        let i = 999;
        let unknown = UnknownValue(UnknownEnumValue::Integer(i));
        let generated = crate::generated::gapic_dataplane::model::TypeCode::UnknownValue(unknown);
        let code: TypeCode = generated.into();
        assert_eq!(code, TypeCode::Unknown(i));
    }

    #[test]
    fn test_simple_types() {
        assert_eq!(int64().code(), TypeCode::Int64);
        assert_eq!(string().code(), TypeCode::String);
        assert_eq!(bool().code(), TypeCode::Bool);
        assert_eq!(float64().code(), TypeCode::Float64);
        assert_eq!(bytes().code(), TypeCode::Bytes);
        assert_eq!(timestamp().code(), TypeCode::Timestamp);
        assert_eq!(date().code(), TypeCode::Date);
        assert_eq!(numeric().code(), TypeCode::Numeric);
        assert_eq!(json().code(), TypeCode::Json);
        assert_eq!(float32().code(), TypeCode::Float32);
        assert_eq!(uuid().code(), TypeCode::Uuid);
        assert_eq!(interval().code(), TypeCode::Interval);
        // PG types are tested in test_pg_types
    }

    #[test]
    fn test_default_type() {
        let t = Type::default();
        assert_eq!(t.code(), TypeCode::Unspecified);
        assert_eq!(
            t.0.code,
            crate::generated::gapic_dataplane::model::TypeCode::Unspecified
        );
    }

    #[test]
    fn test_to_proto_traits() {
        use gaxi::prost::ToProto;
        let t = int64();
        let proto: crate::generated::gapic_dataplane::model::Type = t.clone().to_proto().unwrap();
        assert_eq!(
            proto.code,
            crate::generated::gapic_dataplane::model::TypeCode::Int64
        );

        let code = TypeCode::Int64;
        let proto_code: i32 = code.to_proto().unwrap();
        assert_eq!(proto_code, 2);
    }

    #[test]
    fn test_from_type_traits() {
        let internal_type = crate::generated::gapic_dataplane::model::Type {
            code: crate::generated::gapic_dataplane::model::TypeCode::Bool,
            ..Default::default()
        };
        let t: Type = internal_type.clone().into();
        assert_eq!(t.code(), TypeCode::Bool);

        let back: crate::generated::gapic_dataplane::model::Type = t.into();
        assert_eq!(back.code, internal_type.code);
    }

    #[test]
    fn test_array_type() {
        let t = array(int64());
        assert_eq!(t.code(), TypeCode::Array);
        assert_eq!(
            t.0.array_element_type.unwrap().code,
            crate::generated::gapic_dataplane::model::TypeCode::Int64
        );
    }

    #[test]
    fn test_pg_types() {
        assert_eq!(pg_numeric().code(), TypeCode::Numeric);
        assert_eq!(
            pg_numeric().0.type_annotation,
            TypeAnnotationCode::PgNumeric
        );

        assert_eq!(pg_jsonb().code(), TypeCode::Json);
        assert_eq!(pg_jsonb().0.type_annotation, TypeAnnotationCode::PgJsonb);

        assert_eq!(pg_oid().code(), TypeCode::Int64);
        assert_eq!(pg_oid().0.type_annotation, TypeAnnotationCode::PgOid);
    }

    #[test]
    fn test_array_element_type() {
        let arr = array(int64());
        assert_eq!(arr.array_element_type(), Some(int64()));

        // Non-array types should return None
        assert_eq!(int64().array_element_type(), None);
        assert_eq!(string().array_element_type(), None);
        assert_eq!(Type::default().array_element_type(), None);
    }

    #[test]
    fn test_auto_traits() {
        static_assertions::assert_impl_all!(Type: Send, Sync, Clone, std::fmt::Debug);
        static_assertions::assert_impl_all!(
            TypeCode: Send,
            Sync,
            Clone,
            Copy,
            std::fmt::Debug,
            PartialEq,
            Eq,
            Hash
        );
    }
}