language

package
v0.0.0-...-75e0045 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package language provides the language-specific options used by the go-swagger code generator. The primary type is Options, which describes formatting, naming, and import resolution rules for a target language.

Index

Constants

View Source
const DefaultIndent = 2

DefaultIndent is the default tab width used for Go source formatting.

Variables

View Source
var DefaultFormatOpts = FormatOpts{
	Options: imports.Options{
		TabIndent: true,
		TabWidth:  DefaultIndent,
		Fragment:  true,
		Comments:  true,
	},
	LocalPrefixes: []string{"github.com/go-openapi"},
}

DefaultFormatOpts is the default set of formatting options.

Functions

func CheckPrefixAndFetchRelativePath

func CheckPrefixAndFetchRelativePath(childpath string, parentpath string) (bool, string)

CheckPrefixAndFetchRelativePath checks if childpath is under parentpath and returns the relative path if so.

func DefaultGoBaseImportErr

func DefaultGoBaseImportErr(target string) (string, error)

DefaultGoBaseImportErr resolves the Go import path for the given target directory.

func FormatLite

func FormatLite(filename string, content []byte, opts ...FormatOption) ([]byte, error)

FormatLite is a fast, AST-based Go formatter that fixes imports (remove unused, add well-known ones) and normalises blank lines in import blocks before handing off to golang.org/x/tools/imports.Process for final sorting.

It is less thorough than a full goimports pass but significantly faster, which makes it a good fit for formatting generated code.

Types

type FormatOption

type FormatOption func(*FormatOpts)

FormatOption allows for more flexible code formatting settings.

func WithFormatLocalPrefixes

func WithFormatLocalPrefixes(prefixes ...string) FormatOption

WithFormatLocalPrefixes adds local prefixes to group imports.

func WithFormatOnly

func WithFormatOnly(enabled bool) FormatOption

WithFormatOnly tells the formatter to skip imports processing.

type FormatOpts

type FormatOpts struct {
	imports.Options

	LocalPrefixes []string
}

FormatOpts holds options for code formatting.

func FormatOptsWithDefault

func FormatOptsWithDefault(opts []FormatOption) FormatOpts

FormatOptsWithDefault applies the given options on top of DefaultFormatOpts.

type FormatterFunc

type FormatterFunc func(filename string, src []byte, opts ...FormatOption) ([]byte, error)

FormatterFunc is a function that processes go code to reformat it, e.g. golang.org/x/tools/imports.Process).

Formatting options allow for injecting a custom formatter for the generated code. See [WithCustomFormatter].

type MangleFunc

type MangleFunc func(string) string

MangleFunc is a function that transforms a name string.

type Options

type Options struct {
	ReservedWords        []string
	BaseImportFunc       MangleFunc                     `json:"-"`
	ImportsFunc          func(map[string]string) string `json:"-"`
	ArrayInitializerFunc func(any) (string, error)      `json:"-"`
	FormatOnly           bool
	ExtraInitialisms     []string
	Mangler              mangling.NameMangler
	// contains filtered or unexported fields
}

Options describes a target language to the code generator.

func GolangOpts

func GolangOpts(extraInitialisms ...string) *Options

GolangOpts returns Options for rendering items as golang code.

func (*Options) ArrayInitializer

func (l *Options) ArrayInitializer(data any) (string, error)

ArrayInitializer builds a literal array.

func (*Options) BaseImport

func (l *Options) BaseImport(tgt string) string

BaseImport figures out the base path to generate import statements.

func (*Options) FormatContent

func (l *Options) FormatContent(name string, content []byte, opts ...FormatOption) ([]byte, error)

FormatContent formats a file with a language specific formatter.

func (*Options) Imports

func (l *Options) Imports(imports map[string]string) string

Imports generates the code to import some external packages, possibly aliased.

func (*Options) Init

func (l *Options) Init()

Init the language option.

func (*Options) MangleFileName

func (l *Options) MangleFileName(name string) string

MangleFileName makes sure a file name gets a safe name.

func (*Options) MangleName

func (l *Options) MangleName(name, suffix string) string

MangleName makes sure a reserved word gets a safe name.

func (*Options) ManglePackageName

func (l *Options) ManglePackageName(name, suffix string) string

ManglePackageName makes sure a package gets a safe name. In case of a file system path (e.g. name contains "/" or "\" on Windows), this return only the last element.

func (*Options) ManglePackagePath

func (l *Options) ManglePackagePath(name string, suffix string) string

ManglePackagePath makes sure a full package path gets a safe name. Only the last part of the path is altered.

func (*Options) MangleVarName

func (l *Options) MangleVarName(name string) string

MangleVarName makes sure a reserved word gets a safe name.

func (*Options) SetFormatFunc

func (l *Options) SetFormatFunc(fn FormatterFunc)

SetFormatFunc sets the formatting function for this language.

Jump to

Keyboard shortcuts

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