protogen

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 35 Imported by: 0

README

Protogen

Generates .proto files from workbooks (Excel/CSV/XML/YAML).

Architecture

Generator
└── Book (workbook file)
    └── Sheet (worksheet)
        └── Field (column / document node)
            └── SubField (nested, recursive)

Two parsers handle different input formats:

  • tableParser — column-cursor-based, for tabular formats (CSV/Excel/YAML table)
  • documentParser — node-tree-based, for document formats (XML/YAML document)

Both produce the same IR (internalpb.Workbook), which is then rendered to .proto by the exporter.

Two-Pass Strategy

Protogen resolves cross-workbook type references via two concurrent passes:

Pass Purpose
First Extract type metadata (enum/struct/union) from special sheet modes into a shared type registry
Second Parse all sheets using resolved types; emit .proto files

MODE_DEFAULT sheets are only processed in the second pass.

When generating specific workbooks (GenWorkbook), the first-pass scope is configurable:

  • Normal (default) — scan all workbooks to build a complete type registry
  • Advanced — load the type registry from previously generated .proto files

Field Layout Detection

For map/list fields, layout is auto-detected from column naming patterns:

Layout Rule
LAYOUT_VERTICAL Single column, no digit suffix
LAYOUT_HORIZONTAL Digit-suffixed columns (Field1, Field2, …)
LAYOUT_INCELL Scalar element encoded in a single cell

Struct fields are detected as cross-cell (SPAN_DEFAULT) or inline (SPAN_INNER_CELL) based on whether the type descriptor contains embedded field definitions.

Export Pipeline

IR (internalpb.Workbook)
  └── bookExporter
        ├── file header (syntax, package)
        ├── imports + workbook options
        └── sheetExporter (per sheet, per mode)
              ├── MODE_DEFAULT       → message
              ├── MODE_ENUM_TYPE     → enum
              ├── MODE_STRUCT_TYPE   → struct message
              └── MODE_UNION_TYPE    → union + nested enum

Key behaviors: field number preservation, nested message deduplication, auto import resolution, file locking for concurrent writes.

Error Collection

Errors are collected in a 3-level hierarchy, enabling fail-fast without aborting the entire run:

flowchart TB
    Root["Generator collector (max 10 errors)"]
    Book["Book collector (max 5 errors per workbook)"]
    Sheet["Sheet collector (max 5 errors per sheet)"]
    Root --> Book
    Book --> Sheet
    Sheet -- "increments all ancestors" --> Root
    Root -- "full → cancel context" --> Root
Trigger Action
Field parse error Collected into sheet collector; skip remaining fields in current sheet
Sheet collector full Stop processing fields in current sheet; propagate to book collector
Book collector full Stop processing sheets in this book
Generator collector full Cancel context; all goroutines exit early

Concurrency

Both passes run workbooks concurrently via errgroup. The first pass populates cachedImporters (guarded by RWMutex); the second pass reuses them without re-parsing.

Documentation

Index

Constants

View Source
const Version = "0.10.0"

Variables

This section is empty.

Functions

func ExtractListFieldProp added in v0.10.6

func ExtractListFieldProp(prop *tableaupb.FieldProp, isScalarList bool, layout tableaupb.Layout) *tableaupb.FieldProp

ExtractListFieldProp extracts the specified props which the list field recognizes.

func ExtractMapFieldProp added in v0.10.6

func ExtractMapFieldProp(prop *tableaupb.FieldProp, layout tableaupb.Layout) *tableaupb.FieldProp

ExtractMapFieldProp extracts the specified props which the map field recognizes.

func ExtractScalarFieldProp added in v0.10.6

func ExtractScalarFieldProp(prop *tableaupb.FieldProp) *tableaupb.FieldProp

ExtractScalarFieldProp extracts the specified props which the scalar field recognizes.

FIXME(wenchy): wellknown type fields should also be supported. In fact, it's supported now, but it's not well tested and documented.

func ExtractStructFieldProp added in v0.10.7

func ExtractStructFieldProp(prop *tableaupb.FieldProp) *tableaupb.FieldProp

ExtractStructFieldProp extracts the specified props which the struct field recognizes.

func IsEmptyFieldProp added in v0.10.7

func IsEmptyFieldProp(prop *tableaupb.FieldProp) bool

Types

type Generator

type Generator struct {
	ProtoPackage string // protobuf package name.
	InputDir     string // input dir of workbooks.
	OutputDir    string // output dir of generated protoconf files.

	LocationName string // TZ location name.
	InputOpt     *options.ProtoInputOption
	OutputOpt    *options.ProtoOutputOption

	ProtoRegistryFiles, ProtoRegistryFilesWithGenerated *protoregistry.Files
	ProtoRegistryTypes                                  *dynamicpb.Types
	// contains filtered or unexported fields
}

func NewGenerator

func NewGenerator(protoPackage, indir, outdir string, setters ...options.Option) *Generator

func NewGeneratorWithOptions added in v0.9.7

func NewGeneratorWithOptions(protoPackage, indir, outdir string, opts *options.Options) *Generator

func (*Generator) GenAll added in v0.10.0

func (gen *Generator) GenAll() error

func (*Generator) GenWorkbook added in v0.10.0

func (gen *Generator) GenWorkbook(relWorkbookPaths ...string) error

func (*Generator) Generate

func (gen *Generator) Generate(relWorkbookPaths ...string) error

Generate generates proto files for the specified workbooks. If no workbook paths are provided, it generates proto files for all workbooks found in the input directory.

type Positioner added in v0.15.0

type Positioner interface {
	Position(row, col int) string
}

Jump to

Keyboard shortcuts

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