Skip to content

Commit

Permalink
Merge pull request #49 from so1n/48-dependencies-clash-with-protobuf-…
Browse files Browse the repository at this point in the history
…5261

Fix, Remove dependency:grpcio-tools
  • Loading branch information
so1n authored Apr 12, 2024
2 parents 3860842 + 6f68a62 commit c1d71ab
Show file tree
Hide file tree
Showing 22 changed files with 3,294 additions and 3,265 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ It supports the most complete functions and is also very simple to use.
Assume that it is usually generated through the following command Code corresponding to Protobuf file:
```bash
python -m grpc_tools.protoc -I. example.proto
# or
protoc -I. --python_out=. example.proto
```
After installing `protobuf-to-pydantic`,can use the `protobuf-to-pydantic` plugin with the `--protobuf-to-pydantic_out` option with the following command:
```bash
python -m grpc_tools.protoc -I. --protobuf-to-pydantic_out=. example.proto
# or
protoc -I. --protobuf-to-pydantic_out=. example.proto
```

In this command, `--protobuf-to-pydantic_out=.` means using the `prorobuf-to-pydantic` plug-in,
Expand Down Expand Up @@ -113,6 +117,8 @@ where the left side of `:` indicates that the configuration file path to be read
The final complete command is as follows:
```bash
python -m grpc_tools.protoc -I. --protobuf-to-pydantic_out=config_path=plugin_config.py:. example.proto
# or
protoc -I. --protobuf-to-pydantic_out=config_path=plugin_config.py:. example.proto
```
Through this command, can load the corresponding configuration and run the `protobuf-to-pydantic` plug-in。

Expand Down Expand Up @@ -163,7 +169,7 @@ message UserMessage {
string user_name=6;
}
```
`grpc_tools.protoc` can be used to generate the Python code file corresponding to the `Protobuf` file (the file name is `demo_pb2.py`), and the code related to the `UserMessage` is stored in the code file.
`protoc` can be used to generate the Python code file corresponding to the `Protobuf` file (the file name is `demo_pb2.py`), and the code related to the `UserMessage` is stored in the code file.

At `Python` runtime, The func `msg_to_pydantic_model` can be called to read the `UserMessage` object from the `demo_pb2` module and generate the corresponding `Pydantic Model` object as follows:
```Python
Expand Down Expand Up @@ -347,6 +353,11 @@ python -m grpc_tools.protoc
--python_out=./python_example_proto_code \
--grpc_python_out=./python_example_proto_code \
-I. \
# or
protoc
--python_out=./python_example_proto_code \
--grpc_python_out=./python_example_proto_code \
-I. \
```
Then the path that needs to be filled in for `parse_msg_desc_method` at this time is `./protobuf_to_pydantic/example`.
The following sample code:
Expand Down
13 changes: 12 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ poetry add protobuf-to-pydantic -E mypy-protobuf
`protobuf-to-pydantic`插件是`protobuf-to-pydantic`推荐的`Pydantic Model`源码生成的方式,它支持的功能是最全的,同时使用起来也非常简单,假设平时是通过如下命令生成Protobuf文件对应的代码:
```bash
python -m grpc_tools.protoc -I. example.proto
# or
protoc -I. --python_out=. example.proto
```
那么,在安装`protobuf-to-pydantic`后可以通过`--protobuf-to-pydantic_out`选项使用`protobuf-to-pydantic`的插件,命令如下:
```bash
python -m grpc_tools.protoc -I. --protobuf-to-pydantic_out=. example.proto
# or
protoc -I. --protobuf-to-pydantic_out=. example.proto
```
在这个命令中`--protobuf-to-pydantic_out=.`表示使用`prorobuf_to_pydantic`插件,
且声明了`protobuf-to-pydantic`插件的输出位置为`.`(`.`表示采用`grpc_tools.proto`使用的输出路径)。
Expand Down Expand Up @@ -108,6 +112,8 @@ file_name_suffix = "_p2p"
最终完整的命令如下:
```bash
python -m grpc_tools.protoc -I. --protobuf-to-pydantic_out=config_path=plugin_config.py:. example.proto
# or
protoc -I. --protobuf-to-pydantic_out=config_path=plugin_config.py:. example.proto
```
通过这个命令就可以加载对应的配置再运行`protobuf-to-pydantic`插件。

Expand Down Expand Up @@ -157,7 +163,7 @@ message UserMessage {
string user_name=6;
}
```
通过`grpc_tools.protoc`可以生成`Protobuf`文件对应的`Python`代码文件(文件名为`demo_pb2.py`),代码文件中存在`UserMessage`的相关代码。
通过`protoc`可以生成`Protobuf`文件对应的`Python`代码文件(文件名为`demo_pb2.py`),代码文件中存在`UserMessage`的相关代码。

`Python`运行时可以调用`protobuf-to-pydantic``msg_to_pydantic_model`函数读取`demo_pb2`模块中的`UserMessage`对象,并生成生成对应的`Pydantic Model`对象,如下:
```Python
Expand Down Expand Up @@ -334,6 +340,11 @@ python -m grpc_tools.protoc
--python_out=./python_example_proto_code \
--grpc_python_out=./python_example_proto_code \
-I. \
# or
protoc
--python_out=./python_example_proto_code \
--grpc_python_out=./python_example_proto_code \
-I. \
```
那么此时`parse_msg_desc_method`需要填写的路径是`./protobuf_to_pydantic/example`
比如下面的示例代码:
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c1d71ab

Please sign in to comment.