Documentation
¶
Overview ¶
Package language provides common utilities for language-specific code generation
Index ¶
- func FieldIsMap(f *api.Field, state *api.APIState) bool
- func FilterSlice[T any](slice []T, predicate func(T) bool) []T
- func GenerateFromModel(outdir string, model *api.API, provider TemplateProvider, ...) error
- func HasNestedTypes(m *api.Message) bool
- func MapSlice[T, R any](s []T, f func(T) R) []R
- func PathParams(m *api.Method, state *api.APIState) ([]*api.Field, error)
- func QueryParams(m *api.Method, b *api.PathBinding) []*api.Field
- type GeneratedFile
- type TemplateProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FieldIsMap ¶
FieldIsMap returns true if the field is a map.
func FilterSlice ¶
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 ¶
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 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 ¶
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).