Skip to content

Commit d34d30f

Browse files
tseaverlandrito
authored andcommitted
Add factories to ease creation of array / struct parameter types. (googleapis#3700)
Closes: googleapis#3364
1 parent 8d47e80 commit d34d30f

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

spanner/google/cloud/spanner/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@
2727
from google.cloud.spanner.pool import BurstyPool
2828
from google.cloud.spanner.pool import FixedSizePool
2929

30+
from google.cloud.spanner.types import ArrayParamType
3031
from google.cloud.spanner.types import BOOL_PARAM_TYPE
3132
from google.cloud.spanner.types import BYTES_PARAM_TYPE
3233
from google.cloud.spanner.types import DATE_PARAM_TYPE
3334
from google.cloud.spanner.types import FLOAT64_PARAM_TYPE
3435
from google.cloud.spanner.types import INT64_PARAM_TYPE
3536
from google.cloud.spanner.types import STRING_PARAM_TYPE
37+
from google.cloud.spanner.types import StructField
38+
from google.cloud.spanner.types import StructParamType
3639
from google.cloud.spanner.types import TIMESTAMP_PARAM_TYPE
3740

3841

3942
__all__ = [
4043
'__version__',
4144
'AbstractSessionPool',
45+
'ArrayParamType',
4246
'BOOL_PARAM_TYPE',
4347
'BYTES_PARAM_TYPE',
4448
'BurstyPool',
@@ -50,5 +54,7 @@
5054
'KeyRange',
5155
'KeySet',
5256
'STRING_PARAM_TYPE',
57+
'StructField',
58+
'StructParamType',
5359
'TIMESTAMP_PARAM_TYPE',
5460
]

spanner/google/cloud/spanner/types.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,44 @@
2525
FLOAT64_PARAM_TYPE = type_pb2.Type(code=type_pb2.FLOAT64)
2626
DATE_PARAM_TYPE = type_pb2.Type(code=type_pb2.DATE)
2727
TIMESTAMP_PARAM_TYPE = type_pb2.Type(code=type_pb2.TIMESTAMP)
28+
29+
30+
def ArrayParamType(element_type): # pylint: disable=invalid-name
31+
"""Construct an array paramter type description protobuf.
32+
33+
:type element_type: :class:`type_pb2.Type`
34+
:param element_type: the type of elements of the array
35+
36+
:rtype: :class:`type_pb2.Type`
37+
:returns: the appropriate array-type protobuf
38+
"""
39+
return type_pb2.Type(code=type_pb2.ARRAY, array_element_type=element_type)
40+
41+
42+
def StructField(name, field_type): # pylint: disable=invalid-name
43+
"""Construct a field description protobuf.
44+
45+
:type name: str
46+
:param name: the name of the field
47+
48+
:type field_type: :class:`type_pb2.Type`
49+
:param field_type: the type of the field
50+
51+
:rtype: :class:`type_pb2.StructType.Field`
52+
:returns: the appropriate array-type protobuf
53+
"""
54+
return type_pb2.StructType.Field(name=name, type=field_type)
55+
56+
57+
def StructParamType(fields): # pylint: disable=invalid-name
58+
"""Construct a struct paramter type description protobuf.
59+
60+
:type fields: list of :class:`type_pb2.StructType.Field`
61+
:param fields: the fields of the struct
62+
63+
:rtype: :class:`type_pb2.Type`
64+
:returns: the appropriate struct-type protobuf
65+
"""
66+
return type_pb2.Type(
67+
code=type_pb2.STRUCT,
68+
struct_type=type_pb2.StructType(fields=fields))

spanner/tests/unit/test_types.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2017 Google Inc. All rights reserved.
2+
#
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+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import unittest
17+
18+
19+
class Test_ArrayParamType(unittest.TestCase):
20+
21+
def test_it(self):
22+
from google.cloud.proto.spanner.v1 import type_pb2
23+
from google.cloud.spanner.types import ArrayParamType
24+
from google.cloud.spanner.types import INT64_PARAM_TYPE
25+
26+
expected = type_pb2.Type(
27+
code=type_pb2.ARRAY,
28+
array_element_type=type_pb2.Type(code=type_pb2.INT64))
29+
30+
found = ArrayParamType(INT64_PARAM_TYPE)
31+
32+
self.assertEqual(found, expected)
33+
34+
35+
class Test_Struct(unittest.TestCase):
36+
37+
def test_it(self):
38+
from google.cloud.proto.spanner.v1 import type_pb2
39+
from google.cloud.spanner.types import INT64_PARAM_TYPE
40+
from google.cloud.spanner.types import STRING_PARAM_TYPE
41+
from google.cloud.spanner.types import StructParamType
42+
from google.cloud.spanner.types import StructField
43+
44+
struct_type = type_pb2.StructType(fields=[
45+
type_pb2.StructType.Field(
46+
name='name',
47+
type=type_pb2.Type(code=type_pb2.STRING)),
48+
type_pb2.StructType.Field(
49+
name='count',
50+
type=type_pb2.Type(code=type_pb2.INT64)),
51+
])
52+
expected = type_pb2.Type(
53+
code=type_pb2.STRUCT,
54+
struct_type=struct_type)
55+
56+
found = StructParamType([
57+
StructField('name', STRING_PARAM_TYPE),
58+
StructField('count', INT64_PARAM_TYPE),
59+
])
60+
61+
self.assertEqual(found, expected)

0 commit comments

Comments
 (0)