Documentation
¶
Overview ¶
Package language provides common utilities for language-specific code generation
Index ¶
- Variables
- func ExtractCrossReferenceLinks(doc ast.Node, source []byte) []string
- func FieldIsMap(f *api.Field, model *api.API) bool
- func FilterSlice[T any](slice []T, predicate func(T) bool) []T
- func HasNestedTypes(m *api.Message) bool
- func MapSlice[T, R any](s []T, f func(T) R) []R
- func PathParams(m *api.Method, model *api.API) ([]*api.Field, error)
- func QueryParams(m *api.Method, b *api.PathBinding) []*api.Field
- type GeneratedFile
- type Templates
- func (t *Templates) Execute(name string, data any) (string, error)
- func (t *Templates) GenerateElement(outDir string, element any, gen GeneratedFile) error
- func (t *Templates) GenerateEnum(outDir string, enum *api.Enum, gen GeneratedFile) error
- func (t *Templates) GenerateFromModel(outDir string, model *api.API, generatedFiles []GeneratedFile) error
- func (t *Templates) GenerateMessage(outDir string, message *api.Message, gen GeneratedFile) error
- func (t *Templates) GenerateMethod(outDir string, method *api.Method, gen GeneratedFile) error
- func (t *Templates) GenerateService(outDir string, service *api.Service, gen GeneratedFile) error
- func (t *Templates) Validate() error
Constants ¶
This section is empty.
Variables ¶
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 ¶ added in v0.13.0
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 ¶
FieldIsMap returns true if the field is a map.
func FilterSlice ¶
FilterSlice filters a slice based on a predicate.
func HasNestedTypes ¶
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 ¶
PathParams returns the path parameters for a method.
func QueryParams ¶
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
MustParseTemplates parses all .gotmpl files in fsys under "templates" and panics if parsing fails.
func ParseTemplates ¶ added in v0.47.0
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
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
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
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
GenerateMessage renders a single file using the api.Message model.
func (*Templates) GenerateMethod ¶ added in v0.47.0
GenerateMethod renders a single file using the api.Method model.
func (*Templates) GenerateService ¶ added in v0.47.0
GenerateService renders a single file using the api.Service model.