codegen

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

templ: version: v0.3.924

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstructIdent

func ConstructIdent(c Case, parts ...string) string

ConstructIdent combines parts with the given Case and returns a new formatted ident.

Examples:

ConstructIdent(SnakeCase, "get", "user", "response") // "get_user_response"
ConstructIdent(CamelCase, "get", "user", "response") // "getUserResponse"
ConstructIdent(PascalCase, "get", "user", "response") // "GetUserResponse"

func FindNonInlinableSchemas added in v0.8.0

func FindNonInlinableSchemas(format Case, o openapi.OpenAPI) iter.Seq2[string, openapi.Schema]

FindNonInlinableSchemas find all schemas that cannot be inlined and returns an iterator over those schemas and the recommended schema name based on its origin.

When a schema cannot be inlined, a top-level declaration should be generated.

The schema names will be the format case.

func Page

func Page(options options.Options, gen Generation, httpMode bool) templ.Component

func ResolveOperationName added in v0.8.0

func ResolveOperationName(format Case, path, method string, o openapi.Operation) string

ResolveOperationName returns either a formatted operation summary or a combination of method+path.

Examples:

ResolveOperationName(PascalCase, "/users", "get", operation) // "GetUsers"
operation.Summary = "retrieve user entries"
ResolveOperationName(PascalCase, "/users", "get", operation) // "RetrieveUserEntries"

Types

type Case

type Case int
const (
	SnakeCase Case = iota
	CamelCase
	PascalCase
)

type Config added in v0.8.0

type Config struct {
	// Package name for languages like Go, where a top level name is required.
	// PackageName is derived from OpenAPI.Info.Title, and defaults to "api" when a title is not found.
	PackageName string
	// List of top-level schemas, including all schemas from Components.Schemas, and
	// operation request bodies and responses that cannot be inlined in code.
	Schemas    []Schema
	Operations []Operation
	// Language specific generator for access to utility functions.
	Generator Generator
}

Config represent all necessary data for generating API client code.

type Generation

type Generation struct {
	Language string
	Code     string
}

Generation represents the generated code for a specific programming language

func Generate

func Generate(language string, openapi openapi.OpenAPI) (*Generation, error)

type Generator added in v0.8.0

type Generator interface {
	// SchemaType returns a type declaration for the specific schema.
	// It is expected to be recursive for nested schemas, and will automatically handle indentation.
	// By default, the indentation starts at 0 (top-level).
	SchemaType(schema openapi.Schema) string
	// FunctionNameFormat returns the formatting for function names.
	FunctionNameFormat() Case
	// TypeNameFormat returns the formatting for type declaration names.
	TypeNameFormat() Case
	// VariableNameFormat returns the formatting for variable names.
	VariableNameFormat() Case
	// IdentEscape escapes any invalid characters in an identifier name.
	// An identifier may be a function, variable, class, or type name.
	IdentEscape(ident string) string
}

Generator represents a single programming language and it's conventions, and syntax rules.

type Golang

type Golang struct{}

func NewGolang

func NewGolang() *Golang

func (*Golang) FunctionNameFormat added in v0.8.0

func (g *Golang) FunctionNameFormat() Case

func (*Golang) IdentEscape added in v0.8.0

func (g *Golang) IdentEscape(ident string) string

func (*Golang) SchemaType added in v0.8.0

func (g *Golang) SchemaType(schema openapi.Schema) string

func (*Golang) TypeNameFormat added in v0.8.0

func (g *Golang) TypeNameFormat() Case

func (*Golang) VariableNameFormat added in v0.8.0

func (g *Golang) VariableNameFormat() Case

type InvalidLanguageError

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

func (*InvalidLanguageError) Error

func (err *InvalidLanguageError) Error() string

type Operation added in v0.8.0

type Operation struct {
	URL         string
	Method      string
	Name        string
	Description string
	// List of function parameters
	Params []Param
	// The returned schema type of the function.
	// Empty string when there is no expected response body.
	ReturnType            string
	HasRequestBody        bool
	IsRequestBodyRequired bool
}

func CollectOperations added in v0.8.0

func CollectOperations(o openapi.OpenAPI, generator Generator) []Operation

func (*Operation) CookieParams added in v0.8.0

func (o *Operation) CookieParams() []Param

func (*Operation) HasHeaderOrCookieParams added in v0.8.0

func (o *Operation) HasHeaderOrCookieParams() bool

func (*Operation) HasParams added in v0.8.0

func (o *Operation) HasParams() bool

func (*Operation) HeaderParams added in v0.8.0

func (o *Operation) HeaderParams() []Param

func (*Operation) PathParams added in v0.8.0

func (o *Operation) PathParams() []Param

func (*Operation) QueryParams added in v0.8.0

func (o *Operation) QueryParams() []Param

type Param added in v0.8.0

type Param struct {
	// Name of the function parameter
	Name string
	// Name of the header, query, path value, or cookie.
	// May be empty for special parameters, such as request body.
	BaseName string
	Type     string

	HasMore  bool
	Required bool
	// contains filtered or unexported fields
}

Param represents a single function parameter

type Schema added in v0.8.0

type Schema struct {
	Name string
	openapi.Schema
}

func CollectSchemas added in v0.8.0

func CollectSchemas(o openapi.OpenAPI, generator Generator) []Schema

type TypeScript

type TypeScript struct{}

func NewTypeScript

func NewTypeScript() *TypeScript

func (*TypeScript) FunctionNameFormat added in v0.8.0

func (ts *TypeScript) FunctionNameFormat() Case

func (*TypeScript) IdentEscape added in v0.8.0

func (ts *TypeScript) IdentEscape(ident string) string

func (*TypeScript) SchemaType added in v0.8.0

func (ts *TypeScript) SchemaType(schema openapi.Schema) string

func (*TypeScript) TypeNameFormat added in v0.8.0

func (ts *TypeScript) TypeNameFormat() Case

func (*TypeScript) VariableNameFormat added in v0.8.0

func (ts *TypeScript) VariableNameFormat() Case

Jump to

Keyboard shortcuts

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