Protoc plugin to generate polyglot message validators for protocol buffers
—
The core plugin system provides the main protoc plugin functionality built on the protoc-gen-star framework. It handles proto file processing, module management, and code generation orchestration.
func main()Main entry point for the protoc plugin. Initializes the PGS framework with proto3 optional support and registers the validator module.
Usage in main.go:
optional := uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
pgs.
Init(pgs.DebugEnv("DEBUG_PGV"), pgs.SupportedFeatures(&optional)).
RegisterModule(module.Validator()).
RegisterPostProcessor(pgsgo.GoFmt()).
Render()func main() // in cmd/protoc-gen-validate-go/main.go
func main() // in cmd/protoc-gen-validate-java/main.go
func main() // in cmd/protoc-gen-validate-cpp/main.goLanguage-specific plugin entry points that pre-configure the target language.
Example for Go-specific plugin:
pgs.
Init(pgs.DebugEnv("DEBUG_PGV"), pgs.SupportedFeatures(&optional)).
RegisterModule(module.ValidatorForLanguage("go")).
RegisterPostProcessor(pgsgo.GoFmt()).
Render()type Module struct {
*pgs.ModuleBase
ctx pgsgo.Context
lang string
}Core PGV module implementing the PGS Module interface. Contains embedded base module, Go language context, and target language configuration.
Fields:
*pgs.ModuleBase: Embedded PGS base module functionalityctx pgsgo.Context: Go language-specific context for code generationlang string: Target language ("go", "java", "cc")func Validator() pgs.ModuleCreates a new validator module instance with language determined by parameters.
Returns: Module interface for PGS framework
func ValidatorForLanguage(lang string) pgs.ModuleCreates a validator module for a specific target language.
Parameters:
lang string: Target language ("go", "java", "cc")Returns: Module interface with pre-configured language
func (m *Module) Name() stringReturns the module name identifier used by the PGS framework.
Returns: "validator"
func (m *Module) InitContext(ctx pgs.BuildContext)Initializes the module with the PGS build context and sets up language-specific context.
Parameters:
ctx pgs.BuildContext: PGS build context containing parameters and environmentfunc (m *Module) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Package) []pgs.ArtifactMain execution method that processes proto files and generates validation code.
Parameters:
targets map[string]pgs.File: Map of target proto files to processpkgs map[string]pgs.Package: Map of packages involved in generationReturns: []pgs.Artifact - Generated artifacts for output
Process:
CheckRulesThe plugin accepts several parameters to control generation:
lang"go", "java", "cc"module"github.com/example/project"func (m *Module) CheckRules(msg pgs.Message)Validates that validation rules applied to message fields are consistent and don't contradict each other. Called automatically during Execute for all messages.
Parameters:
msg pgs.Message: Protocol buffer message to validate rules forBehavior:
gt: 10, lt: 5)protoc \
-I . \
-I path/to/validate/ \
--go_out="./generated" \
--validate_out="lang=go:./generated" \
example.protoprotoc \
--validate_out="lang=go,module=github.com/example/project:./generated" \
example.proto# buf.gen.yaml
version: v1
plugins:
- plugin: buf.build/bufbuild/validate-go
out: genThe plugin will fail with descriptive errors for:
lang parameter when not pre-configuredErrors are reported through the PGS framework's error handling system and will cause protoc to exit with a non-zero status code.
Install with Tessl CLI
npx tessl i tessl/go-protoc-gen-validate