Skip to content

Commit 5c2b0eb

Browse files
committed
De-dup logic for converting None policy tags
1 parent c7b8c02 commit 5c2b0eb

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

google/cloud/bigquery/schema.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Schemas for BigQuery tables / queries."""
1616

1717
import collections
18+
from typing import Optional
1819

1920
from google.cloud.bigquery_v2 import types
2021

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

109-
if policy_tags is None:
110-
is_record = field_type is not None and field_type.upper() in _STRUCT_TYPES
111-
self._policy_tags = None if is_record else PolicyTagList()
112-
else:
113-
self._policy_tags = policy_tags
110+
self._policy_tags = self._determine_policy_tags(field_type, policy_tags)
111+
112+
@staticmethod
113+
def _determine_policy_tags(
114+
field_type: str, given_policy_tags: Optional["PolicyTagList"]
115+
) -> Optional["PolicyTagList"]:
116+
"""Return the given policy tags, or their suitable representation if `None`.
117+
118+
Args:
119+
field_type: The type of the schema field.
120+
given_policy_tags: The policy tags to maybe ajdust.
121+
"""
122+
if given_policy_tags is not None:
123+
return given_policy_tags
124+
125+
if field_type is not None and field_type.upper() in _STRUCT_TYPES:
126+
return None
127+
128+
return PolicyTagList()
114129

115130
@staticmethod
116131
def __get_int(api_repr, name):
@@ -138,9 +153,9 @@ def from_api_repr(cls, api_repr: dict) -> "SchemaField":
138153
description = api_repr.get("description", _DEFAULT_VALUE)
139154
fields = api_repr.get("fields", ())
140155

141-
policy_tags = PolicyTagList.from_api_repr(api_repr.get("policyTags"))
142-
if policy_tags is None and field_type not in _STRUCT_TYPES:
143-
policy_tags = PolicyTagList()
156+
policy_tags = cls._determine_policy_tags(
157+
field_type, PolicyTagList.from_api_repr(api_repr.get("policyTags"))
158+
)
144159

145160
return cls(
146161
field_type=field_type,

0 commit comments

Comments
 (0)