|
15 | 15 | """Schemas for BigQuery tables / queries.""" |
16 | 16 |
|
17 | 17 | import collections |
| 18 | +from typing import Optional |
18 | 19 |
|
19 | 20 | from google.cloud.bigquery_v2 import types |
20 | 21 |
|
@@ -106,11 +107,25 @@ def __init__( |
106 | 107 | self._properties["maxLength"] = max_length |
107 | 108 | self._fields = tuple(fields) |
108 | 109 |
|
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() |
114 | 129 |
|
115 | 130 | @staticmethod |
116 | 131 | def __get_int(api_repr, name): |
@@ -138,9 +153,9 @@ def from_api_repr(cls, api_repr: dict) -> "SchemaField": |
138 | 153 | description = api_repr.get("description", _DEFAULT_VALUE) |
139 | 154 | fields = api_repr.get("fields", ()) |
140 | 155 |
|
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 | + ) |
144 | 159 |
|
145 | 160 | return cls( |
146 | 161 | field_type=field_type, |
|
0 commit comments