|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import inspect |
| 17 | + |
| 18 | +from inspect import signature |
| 19 | + |
| 20 | +from functions_framework import _function_registry |
| 21 | +from functions_framework.exceptions import FunctionsFrameworkException |
| 22 | + |
| 23 | +"""Registers user function in the REGISTRY_MAP and the INPUT_TYPE_MAP. |
| 24 | +Also performs some validity checks for the input type of the function |
| 25 | +
|
| 26 | +Args: |
| 27 | + decorator_type: The type provided by the @typed(input_type) decorator |
| 28 | + func: User function |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +def register_typed_event(decorator_type, func): |
| 33 | + try: |
| 34 | + sig = signature(func) |
| 35 | + annotation_type = list(sig.parameters.values())[0].annotation |
| 36 | + input_type = _select_input_type(decorator_type, annotation_type) |
| 37 | + _validate_input_type(input_type) |
| 38 | + except IndexError: |
| 39 | + raise FunctionsFrameworkException( |
| 40 | + "Function signature is missing an input parameter." |
| 41 | + "The function should be defined as 'def your_fn(in: inputType)'" |
| 42 | + ) |
| 43 | + except Exception as e: |
| 44 | + raise FunctionsFrameworkException( |
| 45 | + "Functions using the @typed decorator must provide " |
| 46 | + "the type of the input parameter by specifying @typed(inputType) and/or using python " |
| 47 | + "type annotations 'def your_fn(in: inputType)'" |
| 48 | + ) |
| 49 | + |
| 50 | + _function_registry.INPUT_TYPE_MAP[func.__name__] = input_type |
| 51 | + _function_registry.REGISTRY_MAP[ |
| 52 | + func.__name__ |
| 53 | + ] = _function_registry.TYPED_SIGNATURE_TYPE |
| 54 | + |
| 55 | + |
| 56 | +""" Checks whether the response type of the typed function has a to_dict method""" |
| 57 | + |
| 58 | + |
| 59 | +def _validate_return_type(response): |
| 60 | + if not (hasattr(response, "to_dict") and callable(getattr(response, "to_dict"))): |
| 61 | + raise AttributeError( |
| 62 | + "The type {response} does not have the required method called " |
| 63 | + " 'to_dict'.".format(response=type(response)) |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +"""Selects the input type for the typed function provided through the @typed(input_type) |
| 68 | +decorator or through the parameter annotation in the user function |
| 69 | +""" |
| 70 | + |
| 71 | + |
| 72 | +def _select_input_type(decorator_type, annotation_type): |
| 73 | + if decorator_type == None and annotation_type is inspect._empty: |
| 74 | + raise TypeError( |
| 75 | + "The function defined does not contain Type of the input object." |
| 76 | + ) |
| 77 | + |
| 78 | + if ( |
| 79 | + decorator_type != None |
| 80 | + and annotation_type is not inspect._empty |
| 81 | + and decorator_type != annotation_type |
| 82 | + ): |
| 83 | + raise TypeError( |
| 84 | + "The object type provided via 'typed' decorator: '{decorator_type}'" |
| 85 | + "is different than the one specified by the function parameter's type annotation : '{annotation_type}'.".format( |
| 86 | + decorator_type=decorator_type, annotation_type=annotation_type |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + if decorator_type == None: |
| 91 | + return annotation_type |
| 92 | + return decorator_type |
| 93 | + |
| 94 | + |
| 95 | +"""Checks for the from_dict method implementation in the input type class""" |
| 96 | + |
| 97 | + |
| 98 | +def _validate_input_type(input_type): |
| 99 | + if not ( |
| 100 | + hasattr(input_type, "from_dict") and callable(getattr(input_type, "from_dict")) |
| 101 | + ): |
| 102 | + raise AttributeError( |
| 103 | + "The type {decorator_type} does not have the required method called " |
| 104 | + " 'from_dict'.".format(decorator_type=input_type) |
| 105 | + ) |
0 commit comments