Skip to main content

Proto Boiler Plugin

Protocol Buffers compiler plugin allows you quickly generate boilerplate code from your Google Protocol Buffer (proto) definitions using template scripts on Python.

The plugin creates a JSON file with intermediate representation (IR) of your proto definitions and then runs template scripts on Python to generate code.

Intermediate representation (IR)

The plugin provides methods to easily navigate through specific proto definitions in the intermediate representation. For example, to iterate through all services, you can write the following:

# for each .proto file
for file, _ in IR.node_iter(IR.decl, 'FILE'):
    # for each service
    for service, _ in IR.node_iter(file['decl'], 'SERVICE'):
        print('service:', service['name'])
        # for each method
        for method, _ in IR.node_iter(service['decl']):
            print('  method:', method['name'])
            print('    input:', method['input'])
            print('      streaming:', method['client_streaming'])
            print('    output:', method['output'])
            print('      streaming:', method['server_streaming'])

Template script on Python

The script should implement the function that takes an IR filename and optional a .proto file name parameters:

def boiling(json_filename: str, proto_filename: str | None)

The plugin calls the boiling() function to generate code. The json_filename file contains an intermediate representation of all processed .proto files. The additional parameter proto_filename can be used to filter the data and generate code only for the file specified in the TEMPLATE_LIST parameter of the configuration file.

The script should output the result code into the stdout stream, for that f-codec, that wraps lonesome f-strings in print() can be used. Or you can involve any other output method, such as the standard print() function.

Example templates for generating .cpp, .swift and .proto source code can be found in "sample/templ/".

You can test code generation using the "sample/" templates and proto files by running:

./boiling-sample.sh

Configuration file

A configuration file (in Python) can contain the following parameters:

  • LOGGING_FILE: a filename for logging, str
  • LOGGING_LEVEL: a logging level, int
  • TEMPLATE_LIST: a list of template files, with optionally specifying a .proto file, list[templ | tuple[templ, proto]]
    • templ: a file mask, like "..py"
    • proto: a name of the specific .proto file that will be provided to the template's boiling() function.
  • IR_FILE: a filename for saving IR, str

Custom configuration parameters must be prefixed with MY_.

How to install the package

python3 -m venv ./venv

source ./venv/bin/activate

python3 -m pip install protoboiler

deactivate

How to use the plugin

To generate code, invoke protoc and provide a configuration file (optional) to the plugin using the protoboiler_out parameter:

source ./venv/bin/activate

protoc -I$proto_dir --protoboiler_out=config=$config_file:$output_dir $proto_dir/*.proto

deactivate

Getting started

Given you have a proto file "logging.proto":

syntax = "proto3";

package logging;

enum Level {
  NOTSET = 0;
  DEBUG = 10;
  INFO = 20;
  WARNING = 30;
  ERROR = 40;
  CRITICAL = 50;
}

and a template file "logging.cpp.py" that use f-codec to output the generated code into stdout:

# -*- coding: f -*-

from protoboiler import IR

def boiling(json_filename: str, _):
    IR.open(json_filename)

    f'''
// Generated file.
'''
    for file, _ in IR.node_iter(IR.decl, 'FILE'):
        for enum, _ in IR.node_iter(file['decl'], 'ENUM'):
            f'''
enum class {enum['name']} {{
'''
            for value in enum['value']:
                f'''
    {value['name']} = {value['number']},
'''
            f'''
}};

'''

Running the following command:

protoc -I. --protoboiler_out=. ./logging.proto

you will generate "logging.cpp":

// Generated file.
enum class Level {
    NOTSET = 0,
    DEBUG = 10,
    INFO = 20,
    WARNING = 30,
    ERROR = 40,
    CRITICAL = 50,
};

Development

Given that you have cloned this repository and installed uv, you can install the plugin dependencies:

cd protoboiler/

uv sync

Now you are ready to run the plugin without installing the package. The launcher.py script, which uses protoc bundled into grpcio-tools, is included in the repository for your convenience:

uv run python3 launcher.py $config_file $proto_dir $output_dir

To run the test suite:

uv run pytest

The golden test generates code from the "sample/" templates and proto files, and compares the results with the reference files in "tests/golden/". To regenerate the reference files after an intended change of the code generation, run:

UPDATE_GOLDENS=1 uv run pytest

Download files

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

Source Distribution

protoboiler-2.0.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

protoboiler-2.0.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file protoboiler-2.0.0.tar.gz.

File metadata

  • Download URL: protoboiler-2.0.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for protoboiler-2.0.0.tar.gz
Algorithm Hash digest
SHA256 382546a5dde2dce3e031f0080e1a5d425447f277c4baf8389097ecf4fe876d3b
MD5 ded4c4581c7a96ecb870262ab778bb51
BLAKE2b-256 0c12c93caefaafd23002c4ab76204401e8e13bd86f22e17bf44f91370ff0d444

See more details on using hashes here.

File details

Details for the file protoboiler-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: protoboiler-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for protoboiler-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9925c56363f47e0a5e4d1cf3690d6ef76ad61a079e430e059a62e489bd3d0a64
MD5 56be4daeb6acde9ea3edbfe2786c4eaf
BLAKE2b-256 3ea7b0e5e8ea5a0ed73c9891921b82cba6f27cdd3ebc9200255c8716e27be033

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page