This is the protobuf compiler for MoonBit, consisting of the compiler plugin written in Go and the runtime library.
The compiler and the runtime library are not published yet. To use this protobuf generator:
- Clone this repository
- Build the compiler plugin with
moon build -C cli - Generate the MoonBit output. You can either add the compiled plugin to your
PATH, or specify it with the--pluginoption:- Add to PATH:
PATH=".:$PATH" protoc --mbt_out=. --mbt_opt=paths=source_relative,project_name=gen-proto3 src/test/reader/proto3.proto - Or use --plugin:
protoc --plugin=protoc-gen-mbt=protoc-gen-mbt.exe --mbt_out=. --mbt_opt=paths=source_relative,project_name=gen-proto3 src/test/reader/proto3.protoNote:project_namemust match the output directory name (e.g.,gen-proto3), and the directory must exist.
- Add to PATH:
- Use the generated MoonBit file given that it imports the runtime library with the alias
lib.
This will be simplified in the future development.
- Deprecated group is not supported
- Extensions and custom options are ignored
See spec
moon build -C cli
mkdir gen-proto3
cp cli/target/native/release/build/protoc-gen-mbt.exe .
# Project name must match the directory name
protoc --plugin=protoc-gen-mbt=protoc-gen-mbt.exe --mbt_out=. --mbt_opt=paths=source_relative,project_name=gen-proto3 test/reader/proto3.protoThe generated MoonBit file (e.g., proto3_pb.mbt) will be placed in the specified output directory (e.g., gen-proto3).
You can pass project parameters using --mbt_opt, separated by commas:
| Name | Type | Description | Default Value |
|---|---|---|---|
| json | bool | Generate additional JSON serialization code |
true |
| derive | string | Comma-separated list of derive traits for generated types (Show, Eq, Hash, Compare, Arbitrary) |
Show,Eq |
| async | bool | Generate async read/write code | true |
| username | string | Username to be used in moon.mod.json |
username |
| project_name | string | Project name to be used in moon.mod.json & moon.pkg.json |
protoc-gen-mbt |
Example usage:
protoc --plugin=protoc-gen-mbt=protoc-gen-mbt.exe --mbt_out=. --mbt_opt=json=true,derive=Show,Eq,Hash,username=yourname,project_name=yourproject input.protocli/- Protocol Buffer compiler plugin that generates MoonBit code from.protofileslib/- MoonBit protobuf runtime library with serialization/deserialization supportplugin/- Generated MoonBit code from the protoc plugin interface definitiontest/- Integration and snapshot testsdoc/- Project documentation (see spec.md for protobuf to MoonBit type mappings)scripts/- Development and testing scripts
Key scripts in the scripts/ directory:
Tests code generation by comparing output with snapshots:
# Run snapshot tests
python3 scripts/snapshot_test.py
# Update snapshots after intentional changes
python3 scripts/snapshot_test.py --updateTests protobuf reader functionality:
python3 scripts/reader_test.pyRegenerates plugin code from plugin.proto:
python3 scripts/generate_plugin.pyOther scripts (common.py, workflow.py) provide shared utilities for the above tools.