Skip to main content

Protocol Buffers compiler plugin to quickly generate boilerplate code from your Google Protocol Buffer (proto) definitions.

Project description

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) and then runs template scripts on Python the generate code.

Template script on Python

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

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

The json_filename 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.

Samples 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

  • LOGGING_FILE: a filename for logging
  • LOGGING_LEVEL: a logging level
  • 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

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 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,
};

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

protoboiler-1.0.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

protoboiler-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: protoboiler-1.0.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for protoboiler-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b5ad511b12e05ef397c3fa81b90be197b2266f011aca745bb6e2447c4f847ea3
MD5 439bacf3943e6d91021248cd4ec85693
BLAKE2b-256 91478cb203a9002ba00fdd14840809346235ecaea67580fbd1e72b416b4399eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: protoboiler-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for protoboiler-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e54626ad831f5d5519daec4c31fbe68f5484cbbfbffa9e8308bcd702d7548d8
MD5 e06f0ce67e9cf6e5086f5f0ba568ce6a
BLAKE2b-256 5253eff99e2b0b5c1d3b179a8b385ef0a17ebd09980811e611a17709f06060ba

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