proto

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 11 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 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
}

ExportOptions configures proto file export.

func DefaultExportOptions

func DefaultExportOptions() ExportOptions

DefaultExportOptions returns default export options.

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.

Jump to

Keyboard shortcuts

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