proto

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 12 Imported by: 0

README

Proto Export

This package provides functionality to export dynamically generated Protobuf schemas as .proto files.

Features

  • Export FileDescriptorSets to .proto files
  • Support for multiple files with proper imports
  • ZIP export for easy distribution
  • HTTP endpoints for serving proto files
  • CLI tool for command-line export

Usage

Programmatic API
// Export service definition only
protoContent, err := svc.ExportProto()

// Export all proto files (service + messages)
allProtos, err := svc.ExportAllProtos()

// Get raw FileDescriptorSet
fdset := svc.GetFileDescriptorSet()
HTTP Endpoints

When using the gateway, proto files are automatically served:

# List available proto files
curl http://localhost:8080/proto

# Download specific proto file
curl http://localhost:8080/proto/user.v1.proto

# Download all as ZIP
curl -O http://localhost:8080/proto.zip
CLI Tool
# Export to stdout
go run examples/export-proto/main.go

# Export to directory
go run examples/export-proto/main.go -output ./proto

# Export as single file
go run examples/export-proto/main.go -single -output ./proto

# Export as ZIP
go run examples/export-proto/main.go -zip proto.zip

Implementation Details

The proto export functionality uses the jhump/protoreflect library to convert FileDescriptorSets back to .proto source files. The exported files maintain proper:

  • Package declarations
  • Import statements
  • Message definitions with field types and tags
  • Service definitions with RPC methods
  • Validation metadata in field options

Example Output

syntax = "proto3";

package user.v1;

import "user.v1/createuserrequest.proto";
import "user.v1/createuserresponse.proto";

service UserService {
  rpc CreateUser ( CreateUserRequest ) returns ( CreateUserResponse );
  rpc GetUser ( GetUserRequest ) returns ( GetUserResponse );
}

Benefits

  1. Interoperability: Generate proto files for use with other gRPC tools
  2. Documentation: Human-readable API definitions
  3. Code Generation: Use exported protos with standard protoc toolchain
  4. Version Control: Track API changes in proto format
  5. Client SDKs: Generate clients in any language from exported protos

Documentation

Overview

Package proto provides proto file export functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertFromRegistry

ConvertFromRegistry converts from protoreflect.FileDescriptor to FileDescriptorProto. This is useful when working with protobuf registry files.

func ConvertToFileDescriptor

func ConvertToFileDescriptor(fdp *descriptorpb.FileDescriptorProto) (protoreflect.FileDescriptor, error)

ConvertToFileDescriptor converts a FileDescriptorProto to protoreflect.FileDescriptor. This is a helper function for cases where you need the intermediate representation.

func MergeFileDescriptorSets

func MergeFileDescriptorSets(sets ...*descriptorpb.FileDescriptorSet) *descriptorpb.FileDescriptorSet

MergeFileDescriptorSets merges multiple FileDescriptorSets into one.

Types

type ExportOption added in v0.5.0

type ExportOption func(*ExportOptions)

ExportOption is a functional option for configuring ExportOptions.

func WithCSharpNamespace added in v0.5.0

func WithCSharpNamespace(ns string) ExportOption

WithCSharpNamespace sets the C# namespace option for exported proto files.

func WithGoPackage added in v0.5.0

func WithGoPackage(pkg string) ExportOption

WithGoPackage sets the Go package option for exported proto files.

func WithJavaMultipleFiles added in v0.5.0

func WithJavaMultipleFiles(multiple bool) ExportOption

WithJavaMultipleFiles sets the Java multiple files option for exported proto files.

func WithJavaOuterClass added in v0.5.0

func WithJavaOuterClass(className string) ExportOption

WithJavaOuterClass sets the Java outer classname option for exported proto files.

func WithJavaPackage added in v0.5.0

func WithJavaPackage(pkg string) ExportOption

WithJavaPackage sets the Java package option for exported proto files.

func WithObjcClassPrefix added in v0.5.0

func WithObjcClassPrefix(prefix string) ExportOption

WithObjcClassPrefix sets the Objective-C class prefix option for exported proto files.

func WithPhpMetadataNamespace added in v0.5.0

func WithPhpMetadataNamespace(ns string) ExportOption

WithPhpMetadataNamespace sets the PHP metadata namespace option for exported proto files.

func WithPhpNamespace added in v0.5.0

func WithPhpNamespace(ns string) ExportOption

WithPhpNamespace sets the PHP namespace option for exported proto files.

func WithPythonPackage added in v0.5.0

func WithPythonPackage(pkg string) ExportOption

WithPythonPackage sets the Python package option for exported proto files.

func WithRubyPackage added in v0.5.0

func WithRubyPackage(pkg string) ExportOption

WithRubyPackage sets the Ruby package option for exported proto files.

type ExportOptions

type ExportOptions struct {
	// IncludeComments adds comments to the exported proto files
	IncludeComments bool
	// SortElements sorts messages, fields, etc. alphabetically
	SortElements bool
	// Indent configures the indentation string (default: 2 spaces)
	Indent string
	// LanguageOptions contains language-specific options for the proto file
	LanguageOptions LanguageOptions
}

ExportOptions configures proto file export.

func DefaultExportOptions

func DefaultExportOptions() ExportOptions

DefaultExportOptions returns default export options.

func (*ExportOptions) ApplyOptions added in v0.5.0

func (opts *ExportOptions) ApplyOptions(options ...ExportOption)

ApplyOptions applies the given options to ExportOptions.

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter handles proto file export operations.

func NewExporter

func NewExporter(opts *ExportOptions) *Exporter

NewExporter creates a new proto exporter.

func (*Exporter) ExportFileDescriptorProto

func (e *Exporter) ExportFileDescriptorProto(fdp *descriptorpb.FileDescriptorProto) (string, error)

ExportFileDescriptorProto exports a single proto file.

func (*Exporter) ExportFileDescriptorSet

func (e *Exporter) ExportFileDescriptorSet(fdset *descriptorpb.FileDescriptorSet) (map[string]string, error)

ExportFileDescriptorSet exports all proto files from a FileDescriptorSet.

func (*Exporter) ExportToZip

func (e *Exporter) ExportToZip(fdset *descriptorpb.FileDescriptorSet) ([]byte, error)

ExportToZip exports all proto files to a ZIP archive.

type LanguageOptions added in v0.5.0

type LanguageOptions struct {
	// Go options
	GoPackage string

	// Java options
	JavaPackage       string
	JavaOuterClass    string
	JavaMultipleFiles bool

	// C# options
	CSharpNamespace string

	// PHP options
	PhpNamespace         string
	PhpMetadataNamespace string

	// Ruby options
	RubyPackage string

	// Python options (usually not needed, but can be specified)
	PythonPackage string

	// Objective-C/Swift options
	ObjcClassPrefix string
}

LanguageOptions contains language-specific options for proto files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL