Skip to content

Commit

Permalink
De-dup logic for converting None policy tags
Browse files Browse the repository at this point in the history
  • Loading branch information
plamut committed Jun 21, 2021
1 parent c7b8c02 commit 947f917
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions google/cloud/bigquery/schema.py
Expand Up @@ -15,6 +15,7 @@
"""Schemas for BigQuery tables / queries."""

import collections
from typing import Optional

from google.cloud.bigquery_v2 import types

Expand Down Expand Up @@ -106,11 +107,25 @@ def __init__(
self._properties["maxLength"] = max_length
self._fields = tuple(fields)

if policy_tags is None:
is_record = field_type is not None and field_type.upper() in _STRUCT_TYPES
self._policy_tags = None if is_record else PolicyTagList()
else:
self._policy_tags = policy_tags
self._policy_tags = self._determine_policy_tags(field_type, policy_tags)

@staticmethod
def _determine_policy_tags(
field_type: str, given_policy_tags
) -> Optional["PolicyTagList"]:
"""Return the given policy tags, or their suitable representation if `None`.
Args:
field_type: The type of the schema field.
given_policy_tags: The policy tags to maybe ajdust.
"""
if given_policy_tags is not None:
return given_policy_tags

if field_type is not None and field_type.upper() in _STRUCT_TYPES:
return None

return PolicyTagList()

@staticmethod
def __get_int(api_repr, name):
Expand Down Expand Up @@ -138,9 +153,9 @@ def from_api_repr(cls, api_repr: dict) -> "SchemaField":
description = api_repr.get("description", _DEFAULT_VALUE)
fields = api_repr.get("fields", ())

policy_tags = PolicyTagList.from_api_repr(api_repr.get("policyTags"))
if policy_tags is None and field_type not in _STRUCT_TYPES:
policy_tags = PolicyTagList()
policy_tags = cls._determine_policy_tags(
field_type, PolicyTagList.from_api_repr(api_repr.get("policyTags"))
)

return cls(
field_type=field_type,
Expand Down

0 comments on commit 947f917

Please sign in to comment.