(request: CodeGeneratorRequest)
| 74 | |
| 75 | |
| 76 | def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse: |
| 77 | response = CodeGeneratorResponse() |
| 78 | |
| 79 | plugin_options = request.parameter.split(",") if request.parameter else [] |
| 80 | response.supported_features = CodeGeneratorResponseFeature.FEATURE_PROTO3_OPTIONAL |
| 81 | |
| 82 | request_data = PluginRequestCompiler(plugin_request_obj=request) |
| 83 | # Gather output packages |
| 84 | for proto_file in request.proto_file: |
| 85 | output_package_name = proto_file.package |
| 86 | if output_package_name not in request_data.output_packages: |
| 87 | # Create a new output if there is no output for this package |
| 88 | request_data.output_packages[output_package_name] = OutputTemplate( |
| 89 | parent_request=request_data, package_proto_obj=proto_file |
| 90 | ) |
| 91 | # Add this input file to the output corresponding to this package |
| 92 | request_data.output_packages[output_package_name].input_files.append(proto_file) |
| 93 | |
| 94 | if ( |
| 95 | proto_file.package == "google.protobuf" |
| 96 | and "INCLUDE_GOOGLE" not in plugin_options |
| 97 | ): |
| 98 | # If not INCLUDE_GOOGLE, |
| 99 | # skip outputting Google's well-known types |
| 100 | request_data.output_packages[output_package_name].output = False |
| 101 | |
| 102 | if "pydantic_dataclasses" in plugin_options: |
| 103 | request_data.output_packages[ |
| 104 | output_package_name |
| 105 | ].pydantic_dataclasses = True |
| 106 | |
| 107 | # Gather any typing generation options. |
| 108 | typing_opts = [ |
| 109 | opt[len("typing.") :] for opt in plugin_options if opt.startswith("typing.") |
| 110 | ] |
| 111 | |
| 112 | if len(typing_opts) > 1: |
| 113 | raise ValueError("Multiple typing options provided") |
| 114 | # Set the compiler type. |
| 115 | typing_opt = typing_opts[0] if typing_opts else "direct" |
| 116 | if typing_opt == "direct": |
| 117 | request_data.output_packages[ |
| 118 | output_package_name |
| 119 | ].typing_compiler = DirectImportTypingCompiler() |
| 120 | elif typing_opt == "root": |
| 121 | request_data.output_packages[ |
| 122 | output_package_name |
| 123 | ].typing_compiler = TypingImportTypingCompiler() |
| 124 | elif typing_opt == "310": |
| 125 | request_data.output_packages[ |
| 126 | output_package_name |
| 127 | ].typing_compiler = NoTyping310TypingCompiler() |
| 128 | |
| 129 | # Read Messages and Enums |
| 130 | # We need to read Messages before Services in so that we can |
| 131 | # get the references to input/output messages for each service |
| 132 | for output_package_name, output_package in request_data.output_packages.items(): |
| 133 | for proto_input_file in output_package.input_files: |
no test coverage detected