|
| 1 | +# strict-protobuf-generator |
| 2 | + |
| 3 | +Generate Java constructor helpers for your Protocol Buffers (proto3) messages. |
| 4 | + |
| 5 | +This plugin enables simple, type-safe construction of Protobuf messages by generating static factory methods based on message fields. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- ✅ Constructor generation based on Protobuf message fields |
| 12 | +- ✅ Supports `proto3 optional` fields (null-safe) |
| 13 | +- ✅ Auto-generates import statements for message and enum types |
| 14 | +- ✅ Supports nested package structures |
| 15 | +- ✅ Easy integration with `protoc` via plugin |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Installation |
| 20 | +Using Gradle (Local Development) |
| 21 | + |
| 22 | +```bash |
| 23 | +git clone https://github.com/protobuf-utils/strict-protobuf-generator.git |
| 24 | +cd strict-protobuf-generator |
| 25 | +./gradlew :protoc-plugin:installDist |
| 26 | +``` |
| 27 | +This will generate the plugin binaries at |
| 28 | +``` |
| 29 | +protoc-plugin/build/install/protoc-plugin |
| 30 | +``` |
| 31 | +Make sure you have Java 17+ and protobuf-compiler installed in your environment. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Example Usage |
| 36 | +Given the following example.proto |
| 37 | + |
| 38 | +```protobuf |
| 39 | +syntax = "proto3"; |
| 40 | +
|
| 41 | +package example; |
| 42 | +
|
| 43 | +option java_package = "com.example"; |
| 44 | +option java_multiple_files = true; |
| 45 | +
|
| 46 | +message User { |
| 47 | + string id = 1; |
| 48 | + optional string email = 2; |
| 49 | +} |
| 50 | +``` |
| 51 | +Run bash |
| 52 | +```bash |
| 53 | +protoc \ |
| 54 | + --plugin=protoc-gen-strictproto=./scripts/protoc-gen-strictproto \ |
| 55 | + --strictproto_out=./gen \ |
| 56 | + --proto_path=. \ |
| 57 | + path/to/example.proto |
| 58 | +``` |
| 59 | +The generated code will look like |
| 60 | +```java |
| 61 | +package com.example; |
| 62 | + |
| 63 | +public final class UserConstructor { |
| 64 | + |
| 65 | + private UserConstructor() {} |
| 66 | + |
| 67 | + public static User from(String id, String email) { |
| 68 | + User.Builder builder = User.newBuilder(); |
| 69 | + builder.setId(id); |
| 70 | + if (email != null) { |
| 71 | + builder.setEmail(email); |
| 72 | + } |
| 73 | + return builder.build(); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Docker Support |
| 81 | +You can use Docker to run the generator in isolation |
| 82 | + |
| 83 | +```bash |
| 84 | +docker compose build strictproto-dev |
| 85 | +docker compose run --rm strictproto-dev bash scripts/generate-strictproto.sh |
| 86 | +``` |
| 87 | +Make sure your .proto and scripts/ directory are available inside the container. |
| 88 | + |
| 89 | + |
0 commit comments