Skip to content

Commit

Permalink
Updates Schema object and class related tests for Schema and Table
Browse files Browse the repository at this point in the history
  • Loading branch information
chalmerlowe committed Dec 6, 2024
1 parent 7670971 commit c37be67
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 195 deletions.
34 changes: 28 additions & 6 deletions google/cloud/bigquery/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,6 @@ def _build_schema_resource(fields):

def _to_schema_fields(schema):
"""TODO docstring
QUESTION: do we want a flag to force the generation of a Schema object?
CAST a list of elements to either:
* a Schema object with SchemaFields and an attribute
* a list of SchemaFields but no attribute
Expand Down Expand Up @@ -829,8 +827,6 @@ def serde_info(self) -> Any:
prop = _get_sub_prop(self._properties, ["serDeInfo"])
if prop is not None:
prop = StorageDescriptor().from_api_repr(prop)
print(f"DINOSAUR prop: {prop}")

return prop

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


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

def __str__(self):
return str(self._fields)
return str(self._fields) # This does not handle the case where FTI exists

def __repr__(self):
return f"Schema({self.foreign_type_info!r}, {self._fields!r})"
return f"Schema({self._fields!r}, {self.foreign_type_info!r})"

def append(self, item):
self._fields.append(item)

def extend(self, iterable):
self._fields.extend(iterable)

def to_api_repr(self) -> dict:
"""Build an API representation of this object.
Returns:
Dict[str, Any]:
A dictionary in the format used by the BigQuery API.
"""
return copy.deepcopy(self._properties)

@classmethod
def from_api_repr(cls, resource: dict) -> Schema:
"""Factory: constructs an instance of the class (cls)
given its API representation.
Args:
resource (Dict[str, Any]):
API representation of the object to be instantiated.
Returns:
An instance of the class initialized with data from 'resource'.
"""
config = cls("")
config._properties = copy.deepcopy(resource)
return config
2 changes: 2 additions & 0 deletions google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ def schema(self, value):

if value is None:
self._properties[api_field] = None
elif isinstance(value, Schema):
self._properties[api_field] = value
else:
value = _to_schema_fields(value)
self._properties[api_field] = {"fields": _build_schema_resource(value)}
Expand Down
Loading

0 comments on commit c37be67

Please sign in to comment.