Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate plugin with buf cli #26

Closed
dwwhitlock opened this issue Oct 26, 2023 · 2 comments · Fixed by #28
Closed

Integrate plugin with buf cli #26

dwwhitlock opened this issue Oct 26, 2023 · 2 comments · Fixed by #28

Comments

@dwwhitlock
Copy link

Is your feature request related to a problem? Please describe.
Our team is using the buf cli to manage protobuf builds because
managing protoc calls can be clunky.

Describe the solution you'd like
I'd like this plugin integrated with the CodeGeneratorRequest so that a Dockerfile can be implemented that will allow a custom plugin to be implemented.

Additionally, this will facilitate adoption by Buf to make it a hosted plugin, which will likely increase adoption of this plugin.

Describe alternatives you've considered
The alternative is to use the CLI as documented. However, buf generate is a much friendlier interface to using this.

Additional context
bufbuild/plugins#589

@so1n
Copy link
Owner

so1n commented Oct 26, 2023

Is your feature request related to a problem? Please describe. Our team is using the buf cli to manage protobuf builds because managing protoc calls can be clunky.

Describe the solution you'd like I'd like this plugin integrated with the CodeGeneratorRequest so that a Dockerfile can be implemented that will allow a custom plugin to be implemented.

Additionally, this will facilitate adoption by Buf to make it a hosted plugin, which will likely increase adoption of this plugin.

Describe alternatives you've considered The alternative is to use the CLI as documented. However, buf generate is a much friendlier interface to using this.

Additional context bufbuild/plugins#589

I'm sorry this is my first experience with buf cli and it will take some time to get used to it.
But the concept of buf cli is great and i will implement it sonn.

@so1n
Copy link
Owner

so1n commented Oct 30, 2023

@dwwhitlock Currently,some changes that need to be made to make it compatible with the buf cli. Until then, you can use protobuf-to-pydantic via buf-cli with the instructions below.

Let's assume that the project is now structured as follows:

├── proto-demo
│   ├── buf.yaml  # gen by `npx buf mod init`
│   ├── common
│   │   ├── p2p_validate.proto 
│   │   └── validate.proto
│   └── demo.proto
├── buf.gen.yaml
├── plugin_config.py
└── pyproject.toml

Among them, p2p_validate.proto and validate.proto cloned from github to proto-demo/common.

The contents of the other documents are as follows:

  • demo.proto
    syntax = "proto3";
    
    package demo;
    
    import "common/p2p_validate.proto";
    import "common/validate.proto";
    
    message p2p_demo{
      int32 a = 1[(p2p_validate.rules).int32.default=1];
    }
    
    message validate_demo{
      string b = 1[(validate.rules).string.max_len=3];
    }    
  • plugin_config.py
    from typing import List
    
    # Ignore files to prevent program errors
    ignore_pkg_list: List[str] = ["validate", "p2p_validate"]
  • buf.gen.yaml
    version: v1beta1
    plugins:
      - name: protobuf-to-pydantic
        out: proto_out
        opt:
          - paths=source_relative
          - config_path=plugin_config.py
  • pyproject.toml
    ...
    
    [tool.poetry.dependencies]
    python = "^3.8"
    protobuf-to-pydantic = "^0.2.0.3"
    grpcio-tools = "^1.59.0"
    pydantic = "^2.4.2"
    mypy-protobuf = "^3.5.0"
    toml = "^0.10.2"
    
    ...

All the files are ready, install 'protoc' and 'buf' on the system.
Finally, can execute the 'protobuf-to-pydanitc' plugin via buf-cli in the terminal.

# Set the current directory to the working directory to load the plugin_config.py
export PYTHONPATH=$PYTHONPATH:.
# Poetry can be used to load information into the current Python environment
poetry run npx buf generate proto-demo 

If 'poetry' is not installed, can directly activate the current python environment and execute the command, as follows

# Set the current directory to the working directory to load the plugin_config.py
export PYTHONPATH=$PYTHONPATH:.
# Suppose the path of the current python program is '.venv/bin/python'
source .venv/bin/activate
npx buf generate proto-demo

@so1n so1n linked a pull request Nov 7, 2023 that will close this issue
@so1n so1n closed this as completed in #28 Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants