language

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package language provides common utilities for language-specific code generation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldIsMap

func FieldIsMap(f *api.Field, state *api.APIState) 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 GenerateFromModel

func GenerateFromModel(outdir string, model *api.API, provider TemplateProvider, generatedFiles []GeneratedFile) error

GenerateFromModel generates a number of files always using the api.API model as the input to the mustache template.

This works well for languages like Go and Rust, where a single file can contain all the clients or messages generated by an API. We anticipate that other functions will be needed for languages like Java or C++, where it is conventional to have a single class per file.

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, state *api.APIState) ([]*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 mustache templates can encode the name of the output file, for example, `src/foo.go.mustache` 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.mustache`) are assumed to be used internally and are skipped.

type TemplateProvider

type TemplateProvider func(templateName string) (string, error)

TemplateProvider is a provider for Mustache template contents.

The function is expected to accept a template name, including its full path and the `.mustache` extension, such as `rust/crate/src/lib.rs.mustache` and then return the full contents of the template (or an error).

Jump to

Keyboard shortcuts

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