Skip to content

Commit ae7d3e1

Browse files
committed
feat: Add _client_helpers.j2 partial template\n\nAdds a Jinja2 partial template containing helper macros for the client generation.
1 parent a3117d8 commit ae7d3e1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{#
2+
This is a partial template file intended to be included in other templates.
3+
It contains helper methods for the BigQueryClient class.
4+
#}
5+
6+
# --- HELPER METHODS ---
7+
def _parse_dataset_path(self, dataset_path: str) -> Tuple[Optional[str], str]:
8+
"""
9+
Helper to parse project_id and/or dataset_id from a string identifier.
10+
11+
Args:
12+
dataset_path: A string in the format 'project_id.dataset_id' or
13+
'dataset_id'.
14+
15+
Returns:
16+
A tuple of (project_id, dataset_id).
17+
"""
18+
if "." in dataset_path:
19+
# Use rsplit to handle legacy paths like `google.com:my-project.my_dataset`.
20+
project_id, dataset_id = dataset_path.rsplit(".", 1)
21+
return project_id, dataset_id
22+
return self.project, dataset_path
23+
24+
def _parse_dataset_id_to_dict(self, dataset_id: "DatasetIdentifier") -> dict:
25+
"""
26+
Helper to create a dictionary from a project_id and dataset_id to pass
27+
internally between helper functions.
28+
29+
Args:
30+
dataset_id: A string or DatasetReference.
31+
32+
Returns:
33+
A dict of {"project_id": project_id, "dataset_id": dataset_id_str }.
34+
"""
35+
if isinstance(dataset_id, str):
36+
project_id, dataset_id_str = self._parse_dataset_path(dataset_id)
37+
return {"project_id": project_id, "dataset_id": dataset_id_str}
38+
elif isinstance(dataset_id, dataset_reference.DatasetReference):
39+
return {
40+
"project_id": dataset_id.project_id,
41+
"dataset_id": dataset_id.dataset_id,
42+
}
43+
else:
44+
raise TypeError(f"Invalid type for dataset_id: {type(dataset_id)}")
45+
46+
def _parse_project_id_to_dict(self, project_id: Optional[str] = None) -> dict:
47+
"""Helper to create a request dictionary from a project_id."""
48+
final_project_id = project_id or self.project
49+
return {"project_id": final_project_id}

0 commit comments

Comments
 (0)