tabnasproto

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tabnasproto parses Protocol Buffers .proto IDL (proto2, proto3, edition 2023/2024) into FileDescriptorProto-shaped Go values. It drives the Tabnas engine with an ABNF grammar (via @tabnas/abnf) rather than a hand-written parser. The version is auto-detected from the file's syntax/edition declaration and/or supplied via ProtoOptions.

Go port of ts/src/proto.ts.

Index

Constants

View Source
const GrammarText = "" /* 4330-byte string literal not displayed */

GrammarText is the embedded union ABNF grammar source (common + per-version deltas). Go counterpart of the TS `grammarText` (ts/src/grammar.ts).

View Source
const Version = "0.2.2"

Variables

View Source
var ScalarTypes = map[string]string{
	"double":   "TYPE_DOUBLE",
	"float":    "TYPE_FLOAT",
	"int32":    "TYPE_INT32",
	"int64":    "TYPE_INT64",
	"uint32":   "TYPE_UINT32",
	"uint64":   "TYPE_UINT64",
	"sint32":   "TYPE_SINT32",
	"sint64":   "TYPE_SINT64",
	"fixed32":  "TYPE_FIXED32",
	"fixed64":  "TYPE_FIXED64",
	"sfixed32": "TYPE_SFIXED32",
	"sfixed64": "TYPE_SFIXED64",
	"bool":     "TYPE_BOOL",
	"string":   "TYPE_STRING",
	"bytes":    "TYPE_BYTES",
}

ScalarTypes maps a bare protobuf scalar type to its FieldDescriptorProto type. A field whose type is not here is a message/enum/group reference (resolution deferred) and gets a TypeName instead. Go counterpart of the TS `SCALAR_TYPES` (ts/src/descriptor.ts).

Functions

func EditionEnum added in v0.2.2

func EditionEnum(v ProtoVersion) string

EditionEnum returns the FileDescriptorProto edition enum name for an edition version, e.g. "EDITION_2023" for "2023". FileDescriptorProto records syntax files via Syntax and edition files via Edition. Go counterpart of the TS `editionEnum` (ts/src/detect-version.ts).

func IsEdition added in v0.2.2

func IsEdition(v ProtoVersion) bool

IsEdition reports whether v is an edition version (2023 / 2024). Go counterpart of the TS `isEdition` (ts/src/detect-version.ts).

func Proto

func Proto(j *tabnas.Tabnas) error

Proto installs the union proto grammar onto j so it can parse .proto source into a {rule, src, kids} CST. Use ToDescriptor to turn that CST into a FileDescriptorProto. Mirrors the TS `tn.use(Proto)` plugin.

Types

type DescriptorProto

type DescriptorProto struct {
	Name           string                 `json:"name"`
	Field          []FieldDescriptorProto `json:"field"`
	NestedType     []DescriptorProto      `json:"nestedType"`
	EnumType       []EnumDescriptorProto  `json:"enumType"`
	OneofDecl      []OneofDescriptorProto `json:"oneofDecl"`
	Extension      []FieldDescriptorProto `json:"extension"`
	ExtensionRange []Range                `json:"extensionRange,omitempty"`
	ReservedRange  []Range                `json:"reservedRange,omitempty"`
	ReservedName   []string               `json:"reservedName,omitempty"`
	Options        map[string]OptionValue `json:"options,omitempty"`
}

DescriptorProto describes a message type.

type EnumDescriptorProto

type EnumDescriptorProto struct {
	Name          string                     `json:"name"`
	Value         []EnumValueDescriptorProto `json:"value"`
	ReservedRange []Range                    `json:"reservedRange,omitempty"`
	ReservedName  []string                   `json:"reservedName,omitempty"`
	Options       map[string]OptionValue     `json:"options,omitempty"`
}

EnumDescriptorProto describes an enum definition.

type EnumValueDescriptorProto

type EnumValueDescriptorProto struct {
	Name    string                 `json:"name"`
	Number  int                    `json:"number"`
	Options map[string]OptionValue `json:"options,omitempty"`
}

EnumValueDescriptorProto is one `NAME = number` entry in an enum.

type FieldDescriptorProto

type FieldDescriptorProto struct {
	Name   string `json:"name"`
	Number int    `json:"number"`
	Label  string `json:"label,omitempty"`
	Type   string `json:"type,omitempty"`
	// TypeName is set for message/enum/group field types (resolution deferred).
	TypeName string `json:"typeName,omitempty"`
	// Proto3Optional marks a proto3 explicit `optional` (protoc synthesises a
	// single-field oneof for it).
	Proto3Optional bool `json:"proto3Optional,omitempty"`
	// OneofIndex is the oneof this field belongs to, as an index into the
	// message's OneofDecl. A pointer so index 0 is distinguishable from unset.
	OneofIndex *int                   `json:"oneofIndex,omitempty"`
	Options    map[string]OptionValue `json:"options,omitempty"`
}

FieldDescriptorProto describes a single field, extension, or map entry leaf.

type FileDescriptorProto

type FileDescriptorProto struct {
	// Name is not present in source; callers may set it.
	Name             string                   `json:"name,omitempty"`
	Package          string                   `json:"package,omitempty"`
	Dependency       []string                 `json:"dependency"`
	PublicDependency []int                    `json:"publicDependency"`
	WeakDependency   []int                    `json:"weakDependency"`
	MessageType      []DescriptorProto        `json:"messageType"`
	EnumType         []EnumDescriptorProto    `json:"enumType"`
	Service          []ServiceDescriptorProto `json:"service"`
	Extension        []FieldDescriptorProto   `json:"extension"`
	Options          map[string]OptionValue   `json:"options,omitempty"`
	// Syntax is 'proto2' | 'proto3' for syntax files; absent for editions.
	Syntax string `json:"syntax,omitempty"`
	// Edition is 'EDITION_2023' | 'EDITION_2024' for edition files.
	Edition string `json:"edition,omitempty"`
}

FileDescriptorProto is the root descriptor for one parsed .proto file.

func BuildFile added in v0.2.2

func BuildFile(proto map[string]any, version ProtoVersion) FileDescriptorProto

BuildFile turns a parsed `proto` CST root into a FileDescriptorProto for the given (already resolved) version. Most callers want ToDescriptor or Parse, which resolve the version first. Go counterpart of the TS `buildFile` (ts/src/build-descriptor.ts).

func Parse

func Parse(src string, opts *ProtoOptions) (FileDescriptorProto, error)

Parse parses a .proto source string to a FileDescriptorProto in one call. It builds a fresh engine each time; for repeated parsing reuse an engine: build one with tabnas.Make, install Proto, then call ToDescriptor(j.Parse(src)).

func ToDescriptor

func ToDescriptor(cst any, opts *ProtoOptions) (FileDescriptorProto, error)

ToDescriptor turns a parsed proto CST into a FileDescriptorProto, resolving the version from the file's declaration and the supplied options.

type MethodDescriptorProto

type MethodDescriptorProto struct {
	Name            string                 `json:"name"`
	InputType       string                 `json:"inputType"`
	OutputType      string                 `json:"outputType"`
	ClientStreaming bool                   `json:"clientStreaming,omitempty"`
	ServerStreaming bool                   `json:"serverStreaming,omitempty"`
	Options         map[string]OptionValue `json:"options,omitempty"`
}

MethodDescriptorProto describes one rpc in a service.

type OneofDescriptorProto

type OneofDescriptorProto struct {
	Name    string                 `json:"name"`
	Options map[string]OptionValue `json:"options,omitempty"`
}

OneofDescriptorProto describes a oneof declaration.

type OptionValue

type OptionValue = any

OptionValue is a string, float64, bool, or nested map[string]OptionValue — modelled as `any` (the TS union OptionValue).

type ProtoOptions

type ProtoOptions struct {
	// Version forces the protobuf version. "" (the default) auto-detects from
	// the file's syntax/edition declaration.
	Version ProtoVersion
	// Reconcile, when nil (the default) or true, errors if an explicit Version
	// disagrees with the file's declaration; when false the declaration wins.
	Reconcile *bool
}

ProtoOptions configures descriptor construction.

type ProtoVersion

type ProtoVersion = string

ProtoVersion is one of "proto2", "proto3", "2023", "2024".

func DeclaredVersion added in v0.2.2

func DeclaredVersion(syntaxNode map[string]any) (ProtoVersion, error)

DeclaredVersion pulls the declared version out of a `syntaxOrEdition` CST node, or "" if the file has no leading syntax/edition declaration. The node's src is whitespace-stripped, e.g. `syntax="proto3";`. It returns an error for a recognised keyword carrying an unknown version value. Go counterpart of the TS `declaredVersion` (ts/src/detect-version.ts).

func ResolveVersion added in v0.2.2

func ResolveVersion(declared, option ProtoVersion, reconcile bool) (ProtoVersion, error)

ResolveVersion reconciles the version declared in the source with the version supplied via the plugin option. With reconcile true (the default) a mismatch is an error; otherwise the declaration wins when present. Falls back to proto2 (protoc's default for a file with no declaration/option). Go counterpart of the TS `resolveVersion` (ts/src/detect-version.ts).

type Range

type Range struct {
	Start int `json:"start"`
	End   int `json:"end"`
}

Range is a half-closed [start,end] reserved/extension range.

type ServiceDescriptorProto

type ServiceDescriptorProto struct {
	Name    string                  `json:"name"`
	Method  []MethodDescriptorProto `json:"method"`
	Options map[string]OptionValue  `json:"options,omitempty"`
}

ServiceDescriptorProto describes a service definition.

Jump to

Keyboard shortcuts

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