|
2 | 2 |
|
3 | 3 | # We're ignoring pylint's warning about names since we want
|
4 | 4 | # the manifest to match the container specification.
|
5 |
| - |
6 | 5 | # pylint: disable=invalid-name
|
7 | 6 |
|
8 |
| -from dataclasses import dataclass |
9 |
| -from typing import TypedDict, Optional, Union |
10 |
| -from typing_extensions import NotRequired, Required |
| 7 | +import dataclasses as _dataclasses |
| 8 | +import typing as _typing |
| 9 | +import typing_extensions as _typing_extensions |
11 | 10 |
|
12 |
| -from firebase_functions.params import Expression, Param |
| 11 | +import firebase_functions.params as _params |
13 | 12 |
|
14 | 13 |
|
15 |
| -class SecretEnvironmentVariable(TypedDict): |
16 |
| - key: Required[str] |
17 |
| - secret: NotRequired[str] |
| 14 | +class SecretEnvironmentVariable(_typing.TypedDict): |
| 15 | + key: _typing_extensions.Required[str] |
| 16 | + secret: _typing_extensions.NotRequired[str] |
18 | 17 |
|
19 | 18 |
|
20 |
| -class HttpsTrigger(TypedDict): |
| 19 | +class HttpsTrigger(_typing.TypedDict): |
21 | 20 | """
|
22 | 21 | Trigger definition for arbitrary HTTPS endpoints.
|
23 | 22 | """
|
24 |
| - invoker: NotRequired[list[str]] |
| 23 | + invoker: _typing_extensions.NotRequired[list[str]] |
25 | 24 | """
|
26 | 25 | Which service account should be able to trigger this function. No value means "make public"
|
27 | 26 | on create and don't do anything on update.
|
28 | 27 | """
|
29 | 28 |
|
30 | 29 |
|
31 |
| -class CallableTrigger(TypedDict): |
| 30 | +class CallableTrigger(_typing.TypedDict): |
32 | 31 | """
|
33 | 32 | Trigger definitions for RPCs servers using the HTTP protocol defined at
|
34 | 33 | https://firebase.google.com/docs/functions/callable-reference
|
35 | 34 | """
|
36 | 35 |
|
37 | 36 |
|
38 |
| -class EventTrigger(TypedDict): |
| 37 | +class EventTrigger(_typing.TypedDict): |
39 | 38 | """
|
40 | 39 | Trigger definitions for endpoints that listen to CloudEvents emitted by
|
41 | 40 | other systems (or legacy Google events for GCF gen 1)
|
42 | 41 | """
|
43 |
| - eventFilters: NotRequired[dict[str, Union[str, Expression[str]]]] |
44 |
| - eventFilterPathPatterns: NotRequired[dict[str, Union[str, Expression[str]]]] |
45 |
| - channel: NotRequired[str] |
46 |
| - eventType: Required[str] |
47 |
| - retry: Required[Union[bool, Expression[bool]]] |
48 |
| - region: NotRequired[str] |
49 |
| - serviceAccountEmail: NotRequired[str] |
| 42 | + eventFilters: _typing_extensions.NotRequired[dict[str, str | |
| 43 | + _params.Expression[str]]] |
| 44 | + eventFilterPathPatterns: _typing_extensions.NotRequired[dict[ |
| 45 | + str, str | _params.Expression[str]]] |
| 46 | + channel: _typing_extensions.NotRequired[str] |
| 47 | + eventType: _typing_extensions.Required[str] |
| 48 | + retry: _typing_extensions.Required[bool | _params.Expression[bool]] |
| 49 | + region: _typing_extensions.NotRequired[str] |
| 50 | + serviceAccountEmail: _typing_extensions.NotRequired[str] |
50 | 51 |
|
51 | 52 |
|
52 |
| -class RetryConfig(TypedDict): |
53 |
| - retryCount: NotRequired[Union[int, Expression[int]]] |
54 |
| - maxRetrySeconds: NotRequired[Union[str, Expression[str]]] |
55 |
| - minBackoffSeconds: NotRequired[Union[str, Expression[str]]] |
56 |
| - maxBackoffSeconds: NotRequired[Union[str, Expression[str]]] |
57 |
| - maxDoublings: NotRequired[Union[int, Expression[int]]] |
| 53 | +class RetryConfig(_typing.TypedDict): |
| 54 | + retryCount: _typing_extensions.NotRequired[int | _params.Expression[int]] |
| 55 | + maxRetrySeconds: _typing_extensions.NotRequired[str | |
| 56 | + _params.Expression[str]] |
| 57 | + minBackoffSeconds: _typing_extensions.NotRequired[str | |
| 58 | + _params.Expression[str]] |
| 59 | + maxBackoffSeconds: _typing_extensions.NotRequired[str | |
| 60 | + _params.Expression[str]] |
| 61 | + maxDoublings: _typing_extensions.NotRequired[int | _params.Expression[int]] |
58 | 62 |
|
59 | 63 |
|
60 |
| -class ScheduleTrigger(TypedDict): |
61 |
| - schedule: NotRequired[Union[str, Expression[str]]] |
62 |
| - timeZone: NotRequired[Union[str, Expression[str]]] |
63 |
| - retryConfig: NotRequired[RetryConfig] |
| 64 | +class ScheduleTrigger(_typing.TypedDict): |
| 65 | + schedule: _typing_extensions.NotRequired[str | _params.Expression[str]] |
| 66 | + timeZone: _typing_extensions.NotRequired[str | _params.Expression[str]] |
| 67 | + retryConfig: _typing_extensions.NotRequired[RetryConfig] |
64 | 68 |
|
65 | 69 |
|
66 |
| -class BlockingTrigger(TypedDict): |
67 |
| - eventType: Required[str] |
| 70 | +class BlockingTrigger(_typing.TypedDict): |
| 71 | + eventType: _typing_extensions.Required[str] |
68 | 72 |
|
69 | 73 |
|
70 |
| -class VpcSettings(TypedDict): |
71 |
| - connector: Required[Union[str, Expression[str]]] |
72 |
| - egressSettings: NotRequired[str] |
| 74 | +class VpcSettings(_typing.TypedDict): |
| 75 | + connector: _typing_extensions.Required[str | _params.Expression[str]] |
| 76 | + egressSettings: _typing_extensions.NotRequired[str] |
73 | 77 |
|
74 | 78 |
|
75 |
| -@dataclass(frozen=True) |
| 79 | +@_dataclasses.dataclass(frozen=True) |
76 | 80 | class ManifestEndpoint:
|
77 | 81 | """An definition of a function as appears in the Manifest."""
|
78 | 82 |
|
79 |
| - entryPoint: Optional[str] = None |
80 |
| - region: Optional[list[str]] = None |
81 |
| - platform: Optional[str] = "gcfv2" |
82 |
| - availableMemoryMb: Union[int, Expression[int], None] = None |
83 |
| - maxInstances: Union[int, Expression[int], None] = None |
84 |
| - minInstances: Union[int, Expression[int], None] = None |
85 |
| - concurrency: Union[int, Expression[int], None] = None |
86 |
| - serviceAccountEmail: Optional[str] = None |
87 |
| - timeoutSeconds: Union[int, Expression[int], None] = None |
88 |
| - cpu: Union[int, str] = "gcf_gen1" |
89 |
| - vpc: Optional[VpcSettings] = None |
90 |
| - labels: Optional[dict[str, str]] = None |
91 |
| - ingressSettings: Optional[str] = None |
92 |
| - environmentVariables: Optional[dict[str, str]] = None |
93 |
| - secretEnvironmentVariables: Optional[list[SecretEnvironmentVariable]] = None |
94 |
| - httpsTrigger: Optional[HttpsTrigger] = None |
95 |
| - callableTrigger: Optional[CallableTrigger] = None |
96 |
| - eventTrigger: Optional[EventTrigger] = None |
97 |
| - scheduleTrigger: Optional[ScheduleTrigger] = None |
98 |
| - blockingTrigger: Optional[BlockingTrigger] = None |
99 |
| - |
100 |
| - |
101 |
| -class ManifestRequiredApi(TypedDict): |
102 |
| - api: Required[str] |
103 |
| - reason: Required[str] |
104 |
| - |
105 |
| - |
106 |
| -@dataclass(frozen=True) |
| 83 | + entryPoint: _typing.Optional[str] = None |
| 84 | + region: _typing.Optional[list[str]] = _dataclasses.field( |
| 85 | + default_factory=list[str]) |
| 86 | + platform: _typing.Optional[str] = "gcfv2" |
| 87 | + availableMemoryMb: int | _params.Expression[int] | None = None |
| 88 | + maxInstances: int | _params.Expression[int] | None = None |
| 89 | + minInstances: int | _params.Expression[int] | None = None |
| 90 | + concurrency: int | _params.Expression[int] | None = None |
| 91 | + serviceAccountEmail: _typing.Optional[str] = None |
| 92 | + timeoutSeconds: int | _params.Expression[int] | None = None |
| 93 | + cpu: int | str = "gcf_gen1" |
| 94 | + vpc: _typing.Optional[VpcSettings] = None |
| 95 | + labels: _typing.Optional[dict[str, str]] = None |
| 96 | + ingressSettings: _typing.Optional[str] = None |
| 97 | + environmentVariables: _typing.Optional[dict[str, str]] = None |
| 98 | + secretEnvironmentVariables: _typing.Optional[ |
| 99 | + list[SecretEnvironmentVariable]] = _dataclasses.field( |
| 100 | + default_factory=list[SecretEnvironmentVariable]) |
| 101 | + httpsTrigger: _typing.Optional[HttpsTrigger] = None |
| 102 | + callableTrigger: _typing.Optional[CallableTrigger] = None |
| 103 | + eventTrigger: _typing.Optional[EventTrigger] = None |
| 104 | + scheduleTrigger: _typing.Optional[ScheduleTrigger] = None |
| 105 | + blockingTrigger: _typing.Optional[BlockingTrigger] = None |
| 106 | + |
| 107 | + |
| 108 | +class ManifestRequiredApi(_typing.TypedDict): |
| 109 | + api: _typing_extensions.Required[str] |
| 110 | + reason: _typing_extensions.Required[str] |
| 111 | + |
| 112 | + |
| 113 | +@_dataclasses.dataclass(frozen=True) |
107 | 114 | class ManifestStack:
|
108 | 115 | endpoints: dict[str, ManifestEndpoint]
|
109 | 116 | specVersion: str = "v1alpha1"
|
110 |
| - params: Optional[list[Param]] = None |
111 |
| - requiredApis: list[ManifestRequiredApi] = [] |
| 117 | + params: _typing.Optional[list[_params.Param]] = _dataclasses.field( |
| 118 | + default_factory=list[_params.Param]) |
| 119 | + requiredApis: list[ManifestRequiredApi] = _dataclasses.field( |
| 120 | + default_factory=list[ManifestRequiredApi]) |
| 121 | + |
| 122 | + |
| 123 | +def _param_to_spec( |
| 124 | + param: _params.Param | _params.SecretParam) -> dict[str, _typing.Any]: |
| 125 | + spec_dict: dict[str, _typing.Any] = { |
| 126 | + "name": param.name, |
| 127 | + "label": param.label, |
| 128 | + "description": param.description, |
| 129 | + "immutable": param.immutable, |
| 130 | + } |
| 131 | + |
| 132 | + if isinstance(param, _params.Param): |
| 133 | + spec_dict["default"] = param.default |
| 134 | + # TODO spec representation of inputs |
| 135 | + |
| 136 | + if isinstance(param, _params.BoolParam): |
| 137 | + spec_dict["type"] = "boolean" |
| 138 | + elif isinstance(param, _params.IntParam): |
| 139 | + spec_dict["type"] = "int" |
| 140 | + elif isinstance(param, _params.FloatParam): |
| 141 | + spec_dict["type"] = "float" |
| 142 | + elif isinstance(param, _params.SecretParam): |
| 143 | + spec_dict["type"] = "secret" |
| 144 | + elif isinstance(param, _params.ListParam): |
| 145 | + spec_dict["type"] = "list" |
| 146 | + if spec_dict["default"] is not None: |
| 147 | + spec_dict["default"] = ",".join(spec_dict["default"]) |
| 148 | + elif isinstance(param, _params.StringParam): |
| 149 | + spec_dict["type"] = "string" |
| 150 | + else: |
| 151 | + raise NotImplementedError("Unsupported param type.") |
| 152 | + |
| 153 | + return _dict_to_spec(spec_dict) |
| 154 | + |
| 155 | + |
| 156 | +def _object_to_spec(data) -> object: |
| 157 | + if isinstance(data, _params.Expression): |
| 158 | + return data.to_cel() |
| 159 | + elif _dataclasses.is_dataclass(data): |
| 160 | + return _dataclass_to_spec(data) |
| 161 | + elif isinstance(data, list): |
| 162 | + return list(map(_object_to_spec, data)) |
| 163 | + elif isinstance(data, dict): |
| 164 | + return _dict_to_spec(data) |
| 165 | + else: |
| 166 | + return data |
| 167 | + |
| 168 | + |
| 169 | +def _dict_factory(data: list[_typing.Tuple[str, _typing.Any]]) -> dict: |
| 170 | + out: dict = {} |
| 171 | + for key, value in data: |
| 172 | + if value is not None: |
| 173 | + out[key] = _object_to_spec(value) |
| 174 | + return out |
| 175 | + |
| 176 | + |
| 177 | +def _dataclass_to_spec(data) -> dict: |
| 178 | + out: dict = {} |
| 179 | + for field in _dataclasses.fields(data): |
| 180 | + value = _object_to_spec(getattr(data, field.name)) |
| 181 | + if value is not None: |
| 182 | + out[field.name] = value |
| 183 | + return out |
| 184 | + |
| 185 | + |
| 186 | +def _dict_to_spec(data: dict) -> dict: |
| 187 | + return _dict_factory([(k, v) for k, v in data.items()]) |
| 188 | + |
| 189 | + |
| 190 | +def _manifest_to_spec(manifest: ManifestStack) -> dict: |
| 191 | + out: dict = _dataclass_to_spec(manifest) |
| 192 | + if "params" in out: |
| 193 | + out["params"] = list(map(_param_to_spec, out["params"])) |
| 194 | + return out |
0 commit comments