Skip to content

Commit c37be67

Browse files
committed
Updates Schema object and class related tests for Schema and Table
1 parent 7670971 commit c37be67

File tree

4 files changed

+391
-195
lines changed

4 files changed

+391
-195
lines changed

google/cloud/bigquery/schema.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,6 @@ def _build_schema_resource(fields):
550550

551551
def _to_schema_fields(schema):
552552
"""TODO docstring
553-
QUESTION: do we want a flag to force the generation of a Schema object?
554-
555553
CAST a list of elements to either:
556554
* a Schema object with SchemaFields and an attribute
557555
* a list of SchemaFields but no attribute
@@ -829,8 +827,6 @@ def serde_info(self) -> Any:
829827
prop = _get_sub_prop(self._properties, ["serDeInfo"])
830828
if prop is not None:
831829
prop = StorageDescriptor().from_api_repr(prop)
832-
print(f"DINOSAUR prop: {prop}")
833-
834830
return prop
835831

836832
@serde_info.setter
@@ -957,6 +953,7 @@ def from_api_repr(cls, resource: dict) -> SerDeInfo:
957953

958954

959955
class Schema:
956+
# TODO docstrings and type hints
960957
def __init__(self, fields=None, foreign_type_info=None):
961958
self._properties = {}
962959
self._fields = [] if fields is None else list(fields) # Internal List
@@ -998,13 +995,38 @@ def __iter__(self):
998995
return iter(self._fields)
999996

1000997
def __str__(self):
1001-
return str(self._fields)
998+
return str(self._fields) # This does not handle the case where FTI exists
1002999

10031000
def __repr__(self):
1004-
return f"Schema({self.foreign_type_info!r}, {self._fields!r})"
1001+
return f"Schema({self._fields!r}, {self.foreign_type_info!r})"
10051002

10061003
def append(self, item):
10071004
self._fields.append(item)
10081005

10091006
def extend(self, iterable):
10101007
self._fields.extend(iterable)
1008+
1009+
def to_api_repr(self) -> dict:
1010+
"""Build an API representation of this object.
1011+
1012+
Returns:
1013+
Dict[str, Any]:
1014+
A dictionary in the format used by the BigQuery API.
1015+
"""
1016+
return copy.deepcopy(self._properties)
1017+
1018+
@classmethod
1019+
def from_api_repr(cls, resource: dict) -> Schema:
1020+
"""Factory: constructs an instance of the class (cls)
1021+
given its API representation.
1022+
1023+
Args:
1024+
resource (Dict[str, Any]):
1025+
API representation of the object to be instantiated.
1026+
1027+
Returns:
1028+
An instance of the class initialized with data from 'resource'.
1029+
"""
1030+
config = cls("")
1031+
config._properties = copy.deepcopy(resource)
1032+
return config

google/cloud/bigquery/table.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ def schema(self, value):
464464

465465
if value is None:
466466
self._properties[api_field] = None
467+
elif isinstance(value, Schema):
468+
self._properties[api_field] = value
467469
else:
468470
value = _to_schema_fields(value)
469471
self._properties[api_field] = {"fields": _build_schema_resource(value)}

0 commit comments

Comments
 (0)