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
- Variables
- func CheckPrefixAndFetchRelativePath(childpath string, parentpath string) (bool, string)
- func DefaultGoBaseImportErr(target string) (string, error)
- func FormatLite(filename string, content []byte, opts ...FormatOption) ([]byte, error)
- type FormatOption
- type FormatOpts
- type FormatterFunc
- type MangleFunc
- type Options
- func (l *Options) ArrayInitializer(data any) (string, error)
- func (l *Options) BaseImport(tgt string) string
- func (l *Options) FormatContent(name string, content []byte, opts ...FormatOption) ([]byte, error)
- func (l *Options) Imports(imports map[string]string) string
- func (l *Options) Init()
- func (l *Options) MangleFileName(name string) string
- func (l *Options) MangleName(name, suffix string) string
- func (l *Options) ManglePackageName(name, suffix string) string
- func (l *Options) ManglePackagePath(name string, suffix string) string
- func (l *Options) MangleVarName(name string) string
- func (l *Options) SetFormatFunc(fn FormatterFunc)
Constants ¶
const DefaultIndent = 2
DefaultIndent is the default tab width used for Go source formatting.
Variables ¶
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 ¶
CheckPrefixAndFetchRelativePath checks if childpath is under parentpath and returns the relative path if so.
func DefaultGoBaseImportErr ¶
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 ¶
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 ¶
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 ¶
GolangOpts returns Options for rendering items as golang code.
func (*Options) ArrayInitializer ¶
ArrayInitializer builds a literal array.
func (*Options) BaseImport ¶
BaseImport figures out the base path to generate import statements.
func (*Options) FormatContent ¶
FormatContent formats a file with a language specific formatter.
func (*Options) Imports ¶
Imports generates the code to import some external packages, possibly aliased.
func (*Options) MangleFileName ¶
MangleFileName makes sure a file name gets a safe name.
func (*Options) MangleName ¶
MangleName makes sure a reserved word gets a safe name.
func (*Options) ManglePackageName ¶
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 ¶
ManglePackagePath makes sure a full package path gets a safe name. Only the last part of the path is altered.
func (*Options) MangleVarName ¶
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.