Skip to main content

A tool for converting between Pydantic models and Protobuf messages, specifically enabling the generation of Pydantic BaseModel classes from .proto files.

Project description

English|简体中文

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

pip install protobuf-pydantic-gen

Example

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

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"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

protobuf_pydantic_gen-0.1.4.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

protobuf_pydantic_gen-0.1.4-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file protobuf_pydantic_gen-0.1.4.tar.gz.

File metadata

  • Download URL: protobuf_pydantic_gen-0.1.4.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for protobuf_pydantic_gen-0.1.4.tar.gz
Algorithm Hash digest
SHA256 7183efc205c8ffffa53f7293197cfd6e2165fac7c018d4824b4d06d0a911d15f
MD5 e3df384fb58478534470f2d2092ba988
BLAKE2b-256 88646346dfda6da16d6ccb4759a6fd53331bf8c737ca19b376ecf8a9c7c4b4a5

See more details on using hashes here.

File details

Details for the file protobuf_pydantic_gen-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for protobuf_pydantic_gen-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8b3c942a4edd8c943ea95904206eae968093ec6b2f6fd3ff6c63f6871530c234
MD5 3b32051b99a739cadc9245bd5c0ad4bf
BLAKE2b-256 3ee2155a3a1d0ef41f41c6bf3739a445fa54abdd8725961f9075af1a6468e47b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page