[English](README.md)|[简体中文](README_ZH.md)
# protobuf-pydantic-gen
A tool for converting between Pydantic models and Protobuf messages, enabling the generation of Pydantic `BaseModel` classes from `.proto` files.
## Features
- Supports conversion of Protobuf basic types to Python basic types.
- Converts Protobuf definition language to Pydantic `BaseModel` classes.
- Converts Protobuf definition language to `sqlmodel` ORM models.
- Implements `to_protobuf` and `from_protobuf` methods for `BaseModel` classes, facilitating bidirectional conversion between Pydantic models and Protobuf messages.
- Allows specifying `Pydantic BaseModel Field` parameters in Protobuf descriptor files.
## Installation
```shell
pip install protobuf-pydantic-gen
```
## Example
```protobuf
syntax = "proto3";
import "google/protobuf/descriptor.proto";
import "protobuf_pydantic_gen/pydantic.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
import "constant.proto";
import "example2.proto";
package pydantic_example;
message Nested {
string name = 1[(pydantic.field) = {description: "Name of the example",example: "'ohn Doe",alias: "full_name",default: "John Doe",max_length:128,primary_key:true}];
}
message Example {
option (pydantic.database) = {
as_table: true
table_name: "users",
compound_index:{
indexs:["name","age"],
index_type:"UNIQUE",
name:"uni_name_age"
},
compound_index:{
indexs:["name"],
index_type:"PRIMARY",
name:"index_name"
}
};
string name = 1[(pydantic.field) = {description: "Name of the example",alias: "full_name",default: "John Doe",max_length:128,primary_key:true}];
optional int32 age = 2 [(pydantic.field) = {description: "Age of the example",alias: "years",default: "30"}];
repeated string emails = 3 [(pydantic.field) = {description: "Emails of the example",default:"[]"}];
repeated Example2 examples = 9 [(pydantic.field) = {description: "Nested message",sa_column_type:"JSON"}];
map<string, google.protobuf.Any> entry = 4 [(pydantic.field) = {description: "Properties of the example",default:"{}"}];
Nested nested=8[(pydantic.field) = {description: "Nested message",sa_column_type:"JSON"}];
google.protobuf.Timestamp created_at = 5 [(pydantic.field) = {description: "Creation date of the example",default: "datetime.datetime.now()",required: true}];
ExampleType type = 6 [(pydantic.field) = {description: "Type of the example",default: "ExampleType.TYPE1",sa_column_type:"Enum[ExampleType]"}];
float score = 7 [(pydantic.field) = {description: "Score of the example",default: "0.0",gt: 0.0,le: 100.0,field_type: "Integer"}];
}
```
## Usage
```shell
python3 -m grpc_tools.protoc --proto_path=./protos -I=./protos -I=./ \
--python_out=./pb --pyi_out=./pb --grpc_python_out=./pb --pydantic_out=./models \
"./protos/example.proto"
```
Raw data
{
"_id": null,
"home_page": "https://github.com/begonia-org/pydantic-protobuf-gen",
"name": "protobuf-pydantic-gen",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "pydantic, sqlmodel, protobuf, gRPC, ORM",
"author": "vforfreedom96",
"author_email": "vforfreedom96@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c8/b2/55b8a0ed791ea3229b0992c2628571b7584ec2727bb12e605971e46cc687/protobuf_pydantic_gen-0.1.5.tar.gz",
"platform": null,
"description": "[English](README.md)|[\u7b80\u4f53\u4e2d\u6587](README_ZH.md) \n\n# protobuf-pydantic-gen\n\nA tool for converting between Pydantic models and Protobuf messages, enabling the generation of Pydantic `BaseModel` classes from `.proto` files.\n\n## Features\n\n- Supports conversion of Protobuf basic types to Python basic types.\n- Converts Protobuf definition language to Pydantic `BaseModel` classes.\n- Converts Protobuf definition language to `sqlmodel` ORM models.\n- Implements `to_protobuf` and `from_protobuf` methods for `BaseModel` classes, facilitating bidirectional conversion between Pydantic models and Protobuf messages.\n- Allows specifying `Pydantic BaseModel Field` parameters in Protobuf descriptor files.\n\n## Installation\n\n```shell\npip install protobuf-pydantic-gen\n```\n## Example\n```protobuf\nsyntax = \"proto3\";\n\nimport \"google/protobuf/descriptor.proto\";\nimport \"protobuf_pydantic_gen/pydantic.proto\";\nimport \"google/protobuf/timestamp.proto\";\nimport \"google/protobuf/any.proto\";\nimport \"constant.proto\";\nimport \"example2.proto\";\npackage pydantic_example;\nmessage Nested {\n\n string name = 1[(pydantic.field) = {description: \"Name of the example\",example: \"'ohn Doe\",alias: \"full_name\",default: \"John Doe\",max_length:128,primary_key:true}];\n}\nmessage Example {\n option (pydantic.database) = { \n as_table: true\n table_name: \"users\",\n compound_index:{\n indexs:[\"name\",\"age\"],\n index_type:\"UNIQUE\",\n name:\"uni_name_age\"\n },\n compound_index:{\n indexs:[\"name\"],\n index_type:\"PRIMARY\",\n name:\"index_name\"\n }\n };\n\n string name = 1[(pydantic.field) = {description: \"Name of the example\",alias: \"full_name\",default: \"John Doe\",max_length:128,primary_key:true}];\n optional int32 age = 2 [(pydantic.field) = {description: \"Age of the example\",alias: \"years\",default: \"30\"}];\n repeated string emails = 3 [(pydantic.field) = {description: \"Emails of the example\",default:\"[]\"}];\n repeated Example2 examples = 9 [(pydantic.field) = {description: \"Nested message\",sa_column_type:\"JSON\"}];\n map<string, google.protobuf.Any> entry = 4 [(pydantic.field) = {description: \"Properties of the example\",default:\"{}\"}];\nNested nested=8[(pydantic.field) = {description: \"Nested message\",sa_column_type:\"JSON\"}];\n google.protobuf.Timestamp created_at = 5 [(pydantic.field) = {description: \"Creation date of the example\",default: \"datetime.datetime.now()\",required: true}];\n ExampleType type = 6 [(pydantic.field) = {description: \"Type of the example\",default: \"ExampleType.TYPE1\",sa_column_type:\"Enum[ExampleType]\"}];\n float score = 7 [(pydantic.field) = {description: \"Score of the example\",default: \"0.0\",gt: 0.0,le: 100.0,field_type: \"Integer\"}];\n}\n\n```\n## Usage\n\n```shell\npython3 -m grpc_tools.protoc --proto_path=./protos -I=./protos -I=./ \\\n--python_out=./pb --pyi_out=./pb --grpc_python_out=./pb --pydantic_out=./models \\\n\"./protos/example.proto\"\n\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A tool for converting between Pydantic models and Protobuf messages, specifically enabling the generation of Pydantic BaseModel classes from .proto files.",
"version": "0.1.5",
"project_urls": {
"Documentation": "https://github.com/begonia-org/pydantic-protobuf-gen",
"Homepage": "https://github.com/begonia-org/pydantic-protobuf-gen",
"Repository": "https://github.com/begonia-org/pydantic-protobuf-gen.git"
},
"split_keywords": [
"pydantic",
" sqlmodel",
" protobuf",
" grpc",
" orm"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7218c09298fad721ef037f3a17548ea364e68dbd96056d5c8698860675e8b98b",
"md5": "339ecd4bdcd846f01e19aa0041e17ff3",
"sha256": "6a99f5c06e2df61a67d99846d80a6d04aaaf55218587de55ecb15baaad3f200d"
},
"downloads": -1,
"filename": "protobuf_pydantic_gen-0.1.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "339ecd4bdcd846f01e19aa0041e17ff3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 20272,
"upload_time": "2024-12-02T01:30:14",
"upload_time_iso_8601": "2024-12-02T01:30:14.752580Z",
"url": "https://files.pythonhosted.org/packages/72/18/c09298fad721ef037f3a17548ea364e68dbd96056d5c8698860675e8b98b/protobuf_pydantic_gen-0.1.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c8b255b8a0ed791ea3229b0992c2628571b7584ec2727bb12e605971e46cc687",
"md5": "96fc3ae3d4732280b9b6948e0f3ee82b",
"sha256": "5590064a498315222b1282eb485216e7ce12e85c587aabf6cb4959649ab538c0"
},
"downloads": -1,
"filename": "protobuf_pydantic_gen-0.1.5.tar.gz",
"has_sig": false,
"md5_digest": "96fc3ae3d4732280b9b6948e0f3ee82b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 18017,
"upload_time": "2024-12-02T01:30:17",
"upload_time_iso_8601": "2024-12-02T01:30:17.553861Z",
"url": "https://files.pythonhosted.org/packages/c8/b2/55b8a0ed791ea3229b0992c2628571b7584ec2727bb12e605971e46cc687/protobuf_pydantic_gen-0.1.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-02 01:30:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "begonia-org",
"github_project": "pydantic-protobuf-gen",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "protobuf-pydantic-gen"
}