codegen

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package codegen provides utilities for generating Go code from existing chioas definitions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCode

func GenerateCode[T ItemType](item T, w io.Writer, opts Options) error

GenerateCode writes Go source that reconstructs the given item (a chioas.Definition or chioas.Path) to w using the supplied Options.

Typical use: unmarshal an existing OpenAPI (YAML/JSON) into a chioas.Definition value, then emit equivalent Go code so the spec can live on as code.

Notes:

  • DocOptions are never emitted.
  • Output is deterministic (map keys & slices are sorted)
  • Internal $refs are normalized to bare component names (e.g., "#/components/schemas/User" → "User"). External refs or refs in other areas are left as-is. No dereferencing is performed.
  • This is a bootstrap tool, not production codegen: intended as a starting point, not for CI/CD code generation.
  • If Options.SkipPrologue is false, a package clause and a single chioas import (optionally aliased) are emitted. When true, the caller is responsible for imports.

Errors:

  • Returns the first write error encountered. It does not close w.

func GenerateHandlerStubs added in v1.18.2

func GenerateHandlerStubs[T StubItemType](item T, w io.Writer, opts HandlerStubOptions) error

GenerateHandlerStubs writes Go source for the http handlers for the specified definition (a chioas.Definition, chioas.Path, chioas.Paths or chioas.Method) to w using the supplied Options.

Typical use: unmarshal an existing OpenAPI (YAML/JSON) into a chioas.Definition value, then emit http handler stubs

Errors:

  • Returns the first write error encountered. It does not close w.

func GenerateSchemaStructs added in v1.18.3

func GenerateSchemaStructs[T StructItemType](item T, w io.Writer, opts SchemaStructOptions) error

Types

type HandlerStubOptions added in v1.18.2

type HandlerStubOptions struct {
	Package      string // e.g. "api" (default "api")
	SkipPrologue bool   // don't write package & imports
	// StubNaming is an optional handler func naming (if nil, the default naming is used)
	//
	// If StubNaming is provided but returns an empty string, the default naming is used
	StubNaming  StubNaming
	PublicFuncs bool   // pubic handler funcs
	Receiver    string // sets the receiver for handler funcs (e.g. "(api *MyApi)" - empty string for no receiver)
	PathParams  bool   // if true, writes vars for path params
	GoDoc       bool   // if true, writes a godoc comment for each handler
	// Format if set, formats output in canonical gofmt style (and checks syntax)
	//
	// Note: using this option means the output will be buffered before writing to the final writer
	Format  bool
	UseCRLF bool // true to use \r\n as the line terminator
}

HandlerStubOptions is the options for GenerateHandlerStubs

type Options

type Options struct {
	Package         string // e.g. "api" (default "api")
	VarName         string // e.g. "Spec" (default "definition" - or "Definition" when PublicVars is true)
	ImportAlias     string // e.g. "ch" (default none - i.e. "chioas")
	SkipPrologue    bool   // don't write package & imports
	OmitZeroValues  bool   // skip zero-valued fields
	HoistPaths      bool   // make top-level vars for root paths
	HoistComponents bool   // make top-level vars for named components (Schemas/Parameters/Responses/Requests/Examples)
	PublicVars      bool   // top-level vars are public (for paths & components)
	UseHttpConsts   bool   // if set, "GET" becomes http.MethodGet, 400 becomes http.StatusBadRequest etc.
	InlineHandlers  bool   // if set, generates stub inline handler funcs within the definition code
	// Format if set, formats output in canonical gofmt style (and checks syntax)
	//
	// Note: using this option means the output will be buffered before writing to the final writer
	Format  bool
	UseCRLF bool // true to use \r\n as the line terminator
}

Options is the options for GenerateCode

type SchemaStructOptions added in v1.18.3

type SchemaStructOptions struct {
	Package       string // e.g. "api" (default "api")
	SkipPrologue  bool   // don't write package & imports
	PublicStructs bool   // public schema structs
	NoRequests    bool   // suppresses schema structs for method requests
	NoResponses   bool   // suppresses schema structs for method responses
	OASTags       bool   // if true, writes `oas:"..."` tags for fields
	GoDoc         bool   // if true, writes a godoc comment for each schema struct (indicating its usage)
	// Components is the components to use for $ref resolution
	//
	// Usage:
	//   - if generating schema structs (GenerateSchemaStructs) with chioas.Components, this field is not used
	//   - if generating schema structs (GenerateSchemaStructs) with chioas.Definition, this field (if non-nil) overrides the Definition.Components
	//   - for other types passed to GenerateSchemaStructs, this chioas.Components is used to resolve schema $ref
	//
	Components *chioas.Components
	// KeepComponentProperties if true (and Components supplied), is used to make
	// struct fields for properties that have an object schema - reference another generated struct
	KeepComponentProperties bool
	// Format if set, formats output in canonical gofmt style (and checks syntax)
	//
	// Note: using this option means the output will be buffered before writing to the final writer
	Format  bool
	UseCRLF bool // true to use \r\n as the line terminator

}

type StructItemType added in v1.18.3

type StubItemType added in v1.18.2

type StubItemType interface {
	chioas.Definition | *chioas.Definition |
		chioas.Path | *chioas.Path |
		chioas.Method | *chioas.Method |
		chioas.Paths
}

type StubNaming added in v1.18.2

type StubNaming interface {
	Name(path string, method string, def chioas.Method) string
}

StubNaming is an interface that can be used for HandlerStubOptions.StubNaming

Jump to

Keyboard shortcuts

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