Help us shape the future of Go! Take the 2026 Go Developer Survey

language

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package language provides common utilities for language-specific code generation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrVariableMutation indicates a template mutates an existing variable ($x = ...).
	ErrVariableMutation = errors.New("variable mutation is banned")
	// ErrLoopControl indicates a template uses break or continue inside a range loop.
	ErrLoopControl = errors.New("loop control statement is banned")
	// ErrBannedBuiltin indicates a template invokes a forbidden computational built-in.
	ErrBannedBuiltin = errors.New("computational built-in is banned")
	// ErrParameterizedMethod indicates a template calls a method with arguments.
	ErrParameterizedMethod = errors.New("parameterized method call is banned")
)

Functions

func ExtractCrossReferenceLinks(doc ast.Node, source []byte) []string

ExtractCrossReferenceLinks returns the cross-reference links found in `doc` and `source`.

Google Cloud documentation comments include cross-reference links:

https://google.aip.dev/192#cross-references

That is, markdown reference-style links in the form `[Title][Definition]` where `Title` is the text that should appear in the documentation and `Definition` is the name of a Protobuf entity, e.g., `google.longrunning.Operation`.

The cross reference links can be of the form `[Title][]` when both the title and definitions match. And may also be relative to the current entity (e.g. `Operation` instead of the fully qualified `google.longrunning.Operation`).

func FieldIsMap

func FieldIsMap(f *api.Field, model *api.API) bool

FieldIsMap returns true if the field is a map.

func FilterSlice

func FilterSlice[T any](slice []T, predicate func(T) bool) []T

FilterSlice filters a slice based on a predicate.

func HasNestedTypes

func HasNestedTypes(m *api.Message) bool

HasNestedTypes returns true if the message has nested types.

func MapSlice

func MapSlice[T, R any](s []T, f func(T) R) []R

MapSlice maps a slice from one type to another.

func PathParams

func PathParams(m *api.Method, model *api.API) ([]*api.Field, error)

PathParams returns the path parameters for a method.

func QueryParams

func QueryParams(m *api.Method, b *api.PathBinding) []*api.Field

QueryParams returns the query parameters for a method.

Types

type GeneratedFile

type GeneratedFile struct {
	// The name of the template file, relative to the Codec's filesystem root.
	TemplatePath string
	// The name of the output file, relative to the output directory.
	OutputPath string
}

GeneratedFile represents an input template and its corresponding output file.

func WalkTemplatesDir

func WalkTemplatesDir(fsys fs.FS, root string) []GeneratedFile

WalkTemplatesDir handles a common case for Codecs: the templates filenames can encode the output filenames.

For some languages (e.g. Go and Rust) the name of the Go templates can encode the name of the output file, for example, `src/foo.go.gotmpl` can generate `src/foo.go`.

This is not true for all languages. For example, in Java it would be more idiomatic to generate one file per (generated) class.

Even in Rust, we may want to skip some files if the crate does not have any services.

Files whose output names would do not have an extension (e.g. `enum.gotmpl`) are assumed to be used internally and are skipped.

type Templates added in v0.47.0

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

Templates holds a parsed tree of Go templates (.gotmpl).

func MustParseTemplates added in v0.47.0

func MustParseTemplates(fsys fs.FS) *Templates

MustParseTemplates parses all .gotmpl files in fsys under "templates" and panics if parsing fails.

func ParseTemplates added in v0.47.0

func ParseTemplates(fsys fs.FS) (*Templates, error)

ParseTemplates parses all .gotmpl files in fsys under "templates" into a shared template tree configured with missingkey=error and standard helpers ("include" and "indent").

func ParseTemplatesDir added in v0.47.0

func ParseTemplatesDir(fsys fs.FS, root string) (*Templates, error)

ParseTemplatesDir parses all .gotmpl files in fsys under root into a shared template tree configured with missingkey=error and standard helpers ("include" and "indent").

func (*Templates) Execute added in v0.47.0

func (t *Templates) Execute(name string, data any) (string, error)

Execute renders the named template (with or without ".gotmpl" suffix) and trims a single leading newline emitted after the template license comment.

func (*Templates) GenerateElement added in v0.47.0

func (t *Templates) GenerateElement(outDir string, element any, gen GeneratedFile) error

GenerateElement renders a single template file for element and writes it to outDir.

func (*Templates) GenerateEnum added in v0.47.0

func (t *Templates) GenerateEnum(outDir string, enum *api.Enum, gen GeneratedFile) error

GenerateEnum renders a single file using the api.Enum model.

func (*Templates) GenerateFromModel added in v0.47.0

func (t *Templates) GenerateFromModel(outDir string, model *api.API, generatedFiles []GeneratedFile) error

GenerateFromModel renders a slice of files using model as the root template context.

func (*Templates) GenerateMessage added in v0.47.0

func (t *Templates) GenerateMessage(outDir string, message *api.Message, gen GeneratedFile) error

GenerateMessage renders a single file using the api.Message model.

func (*Templates) GenerateMethod added in v0.47.0

func (t *Templates) GenerateMethod(outDir string, method *api.Method, gen GeneratedFile) error

GenerateMethod renders a single file using the api.Method model.

func (*Templates) GenerateService added in v0.47.0

func (t *Templates) GenerateService(outDir string, service *api.Service, gen GeneratedFile) error

GenerateService renders a single file using the api.Service model.

func (*Templates) Validate added in v0.47.0

func (t *Templates) Validate() error

Validate inspects the parsed AST of all templates in t and returns an error if any template uses banned constructs (variable mutation, break/continue, computational built-ins, or parameterized method calls).

Jump to

Keyboard shortcuts

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