Python Library to generate proto buffer code from python using type annotations
python -m venv venv
source venv/bin/activate # venv/Scripts/activate on Windowspip install protomodelCreate a python file and add the following code:
from protomodel import message, service, rpc
@message
class HelloRequest:
name: str
@message
class HelloReply:
message: str
@service
class Greeter:
@rpc
def say_hello(hello_request: HelloRequest) -> HelloReply:
passRun the following command to generate a proto buffer file:
python -m protomodel generate --python_file=hello.py --proto_name=hello.protoAnd then you will get the following result:
syntax = "proto3";
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}Add a package name:
python -m protomodel generate --python_file=hello.py \
--proto_name=hello.proto --package_name=my_package_namesyntax = "proto3";
package my_package_name;
...@message
class Item:
id: int
name: str
description: str
price: float
enabled: bool
@message
class ItemsList:
items: list[Item]message Item {
int32 id = 1;
string name = 2;
string description = 3;
double price = 4;
bool enabled = 5;
}
message ItemsList {
repeated Item items = 1;
}