protogen

package
v0.0.39 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package protogen holds the file passes a proto generation runs over buf's output: the generated Python package's skeleton (__init__.py files, the py.typed marker) and its package-qualified imports, the protobuf-es REQUIRED-field promotion read from a descriptor set, and the clean / stash-and-settle moves that regenerate a directory without losing its committed contents on a failed run.

Index

Constants

This section is empty.

Variables

View Source
var ImportPrefixRe = regexp.MustCompile(
	`^(from )([a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)*)( import .*)$`,
)

ImportPrefixRe matches "from <bare_package> import ..." lines in generated Python protobuf files.

Functions

func CleanGenDir

func CleanGenDir(dir string) error

CleanGenDir removes everything in dir except go.mod and go.sum, preserving the module identity so that go mod tidy under go.work stays deterministic.

func DeoptionalizeTSFields

func DeoptionalizeTSFields(tsDir string, required map[string]map[string]struct{}) error

DeoptionalizeTSFields rewrites every `_pb.ts` under tsDir, turning `name?: T | undefined` into `name: T` for each property whose enclosing message + name is in required. Files are written only when a property actually changed.

func EnsurePythonInitFiles

func EnsurePythonInitFiles(root, genPkg string) error

EnsurePythonInitFiles walks the generated Python package directory and creates an empty __init__.py in every directory that lacks one.

func EnsurePythonTypedMarker

func EnsurePythonTypedMarker(root, genPkg string) error

EnsurePythonTypedMarker writes an empty PEP 561 py.typed marker at the root of the generated Python package, so basedpyright treats the editable-installed generated package as a typed package and reads its .py/.pyi stubs when resolving proto types for downstream consumers. It is idempotent (an existing marker is left as-is by the WriteFile overwrite of empty content).

func ReadDescriptorSet

func ReadDescriptorSet(path string) (*descriptorpb.FileDescriptorSet, error)

ReadDescriptorSet reads the google.protobuf.FileDescriptorSet `buf build --as-file-descriptor-set` wrote at path, so field options (field_behavior) are read as structured data — the REQUIRED set DeoptionalizeTSFields promotes. An empty file is an empty set.

func RequiredMessageFields

func RequiredMessageFields(
	fds *descriptorpb.FileDescriptorSet,
) map[string]map[string]struct{}

RequiredMessageFields returns, per fully-qualified message name, the set of camelCase field names (the protobuf-es property names, i.e. json_name) that are singular message fields marked field_behavior = REQUIRED — exactly the fields protoc-gen-es types `?: T | undefined` but the contract guarantees present.

func RewritePythonImports

func RewritePythonImports(root, genPkg string) error

RewritePythonImports walks the generated Python package directory and rewrites bare cross-package imports to use the fully-qualified path, in both the runtime `_pb2.py`/`_pb2_grpc.py` modules and their `_pb2.pyi`/`_pb2_grpc.pyi` type stubs (the latter so basedpyright resolves proto types for consumers). Only imports whose first path segment names a real proto-package subdirectory of the generated root are qualified; stdlib and third-party imports the mypy `.pyi` stubs carry (collections, typing, grpc) are left alone.

func SettleDir

func SettleDir(dir, backup string, restore bool) error

SettleDir ends a StashDir: on restore (the generator failed) it puts the stashed contents back in place of whatever the generator left, so a failed generator (a buf or BSR error) never leaves committed generated output deleted; either way it removes the backup. An empty backup is a no-op.

func StashDir

func StashDir(dir string) (string, error)

StashDir moves dir's current contents aside and leaves dir empty for a generator to fill, returning the backup directory SettleDir takes. The backup sits beside dir so the moves are same-filesystem renames; a missing dir stashes nothing.

Types

This section is empty.

Jump to

Keyboard shortcuts

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