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

Development

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

cd protoboiler/

poetry install

Now you are ready to run the plugin without installing the package. The launcher script is included in the repository for your convenience:

poetry run ./launcher $config_file $proto_dir $output_dir

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.2.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

protoboiler-1.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for protoboiler-1.2.0.tar.gz
Algorithm Hash digest
SHA256 868d5a3b9979ad320a7bdda04822cbb564c26f81e1021e73f37aefd19895295b
MD5 d440f9b706e536caedd14726606375bd
BLAKE2b-256 e25d3f84e09783969d39906b638a9aeb32ee5e2200391416bda72e161f4d7830

See more details on using hashes here.

File details

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

File metadata

  • Download URL: protoboiler-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42edcef0a1ee205060ddc047644f1331188c4dbedf9d6870094d4800941cdefc
MD5 1df544edc27c3acdf4f534371967ed5f
BLAKE2b-256 fecb01d7e431936fc72cfa9cef2926112c1571aa707a919bb946fe45d82c9c17

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