Documentation
¶
Index ¶
- Variables
- type EntityDecl
- func (d *EntityDecl) HasModelAnnotation() bool
- func (d *EntityDecl) HasParameterAnnotation() bool
- func (d *EntityDecl) HasResponseAnnotation() bool
- func (d *EntityDecl) Names() (name, goName string)
- func (d *EntityDecl) Obj() *types.TypeName
- func (d *EntityDecl) ObjType() types.Type
- func (d *EntityDecl) OperationIDs() (result []string)
- func (d *EntityDecl) ResponseNames() (name, goName string)
- type Options
- type ScanCtx
- func (s *ScanCtx) AddDiscoveredModel(decl *EntityDecl)
- func (s *ScanCtx) Debug() bool
- func (s *ScanCtx) DeclForType(t types.Type) (*EntityDecl, bool)
- func (s *ScanCtx) DescWithRef() bool
- func (s *ScanCtx) ExtraModels() iter.Seq2[*ast.Ident, *EntityDecl]
- func (s *ScanCtx) FileSet() *token.FileSet
- func (s *ScanCtx) FindComments(pkg *packages.Package, name string) (*ast.CommentGroup, bool)
- func (s *ScanCtx) FindDecl(pkgPath, name string) (*EntityDecl, bool)
- func (s *ScanCtx) FindEnumValues(pkg *packages.Package, enumName string) (list []any, descList []string, _ bool)
- func (s *ScanCtx) FindModel(pkgPath, name string) (*EntityDecl, bool)deprecated
- func (s *ScanCtx) GetModel(pkgPath, name string) (*EntityDecl, bool)
- func (s *ScanCtx) Meta() iter.Seq[*ast.CommentGroup]
- func (s *ScanCtx) Models() iter.Seq2[*ast.Ident, *EntityDecl]
- func (s *ScanCtx) MoveExtraToModel(k *ast.Ident)
- func (s *ScanCtx) NumExtraModels() int
- func (s *ScanCtx) OnDiagnostic() func(grammar.Diagnostic)
- func (s *ScanCtx) Operations() iter.Seq[parsers.ParsedPathContent]
- func (s *ScanCtx) Parameters() iter.Seq[*EntityDecl]
- func (s *ScanCtx) PkgForType(t types.Type) (*packages.Package, bool)
- func (s *ScanCtx) PosOf(p token.Pos) token.Position
- func (s *ScanCtx) RefAliases() bool
- func (s *ScanCtx) Responses() iter.Seq[*EntityDecl]
- func (s *ScanCtx) Routes() iter.Seq[parsers.ParsedPathContent]
- func (s *ScanCtx) SetXNullableForPointers() bool
- func (s *ScanCtx) SkipExtensions() bool
- func (s *ScanCtx) TransparentAliases() bool
- type TypeIndex
- type TypeIndexOption
- func WithDebug(enabled bool) TypeIndexOption
- func WithExcludeDeps(excluded bool) TypeIndexOption
- func WithExcludePkgs(excluded []string) TypeIndexOption
- func WithExcludeTags(excluded map[string]bool) TypeIndexOption
- func WithIncludePkgs(included []string) TypeIndexOption
- func WithIncludeTags(included map[string]bool) TypeIndexOption
- func WithRefAliases(enabled bool) TypeIndexOption
- func WithTransparentAliases(enabled bool) TypeIndexOption
- func WithXNullableForPointers(enabled bool) TypeIndexOption
Constants ¶
This section is empty.
Variables ¶
var ErrScanner = errors.New("codescan:scanner")
ErrScanner is the sentinel error for all errors originating from the scanner package.
Functions ¶
This section is empty.
Types ¶
type EntityDecl ¶
type EntityDecl struct {
Comments *ast.CommentGroup
Type *types.Named
Alias *types.Alias // added to supplement Named, after go1.22
Ident *ast.Ident
Spec *ast.TypeSpec
File *ast.File
Pkg *packages.Package
// contains filtered or unexported fields
}
func (*EntityDecl) HasModelAnnotation ¶
func (d *EntityDecl) HasModelAnnotation() bool
func (*EntityDecl) HasParameterAnnotation ¶
func (d *EntityDecl) HasParameterAnnotation() bool
func (*EntityDecl) HasResponseAnnotation ¶
func (d *EntityDecl) HasResponseAnnotation() bool
func (*EntityDecl) Names ¶
func (d *EntityDecl) Names() (name, goName string)
func (*EntityDecl) Obj ¶
func (d *EntityDecl) Obj() *types.TypeName
Obj returns the type name for the declaration defining the named type or alias t.
func (*EntityDecl) ObjType ¶
func (d *EntityDecl) ObjType() types.Type
func (*EntityDecl) OperationIDs ¶
func (d *EntityDecl) OperationIDs() (result []string)
func (*EntityDecl) ResponseNames ¶
func (d *EntityDecl) ResponseNames() (name, goName string)
type Options ¶
type Options struct {
Packages []string
InputSpec *spec.Swagger
ScanModels bool
WorkDir string
BuildTags string
ExcludeDeps bool
Include []string
Exclude []string
IncludeTags []string
ExcludeTags []string
SetXNullableForPointers bool
RefAliases bool // aliases result in $ref, otherwise aliases are expanded
TransparentAliases bool // aliases are completely transparent, never creating definitions
// DescWithRef controls description preservation on $ref'd fields
// in the description-only-decoration case: when a struct field's
// Go type resolves to a named type ($ref) and its only
// field-level decoration is a description (no validations, no
// user-authored extensions).
//
// - false (default): the description is dropped and the field
// emits as a bare `{$ref: ...}`.
// - true: the description is preserved by wrapping the $ref in
// a single-arm `allOf` compound — `{description: "...",
// allOf: [{$ref}]}` — the JSON-Schema-draft-4 correct shape
// for sibling description.
//
// When the field also carries validation overrides (pattern,
// enum, example, etc.) or user-authored vendor extensions, the
// allOf compound is mandatory regardless of this flag — the
// override would be lost otherwise.
//
// See [§descwithref](./README.md#descwithref).
DescWithRef bool
SkipExtensions bool // skip generating x-go-* vendor extensions in the spec
Debug bool // enable verbose debug logging during scanning
// OnDiagnostic, when non-nil, is invoked for every diagnostic the
// builder layer records (lexer/parser warnings, semantic-validation
// failures from the validations package, etc.). The callback fires
// once per diagnostic in source order; diagnostics never block the
// build — invalid constructs are silently dropped from the output
// spec while their explanation flows through this channel.
//
// Experimental: the public API surface for diagnostics is subject
// to change while LSP integration matures. See
// [§diagnostics](./README.md#diagnostics).
OnDiagnostic func(grammar.Diagnostic)
}
Options configures a scan. The zero value is a valid configuration: every flag defaults to false and every slice/map defaults to nil.
Details ¶
See [§options](./README.md#options) for the field overview, and [§descwithref](./README.md#descwithref) and [§diagnostics](./README.md#diagnostics) for the two fields with non-trivial semantics (DescWithRef and OnDiagnostic).
type ScanCtx ¶
type ScanCtx struct {
// contains filtered or unexported fields
}
func NewScanCtx ¶
func (*ScanCtx) AddDiscoveredModel ¶ added in v0.34.1
func (s *ScanCtx) AddDiscoveredModel(decl *EntityDecl)
AddDiscoveredModel registers decl in the ExtraModels index so the spec orchestrator emits a top-level definition for it.
No-op when decl is already an annotated swagger:model (in Models); annotated decls are emitted unconditionally and re-registering them as "discovered" would create a Models↔ExtraModels bouncing loop in joinExtraModels. Nil and Ident-less decls are silently ignored.
Use only at sites that explicitly intend the registration — pure-read lookups should use GetModel. See [§model-lookup](./README.md#model-lookup).
func (*ScanCtx) DeclForType ¶
func (s *ScanCtx) DeclForType(t types.Type) (*EntityDecl, bool)
func (*ScanCtx) DescWithRef ¶
func (*ScanCtx) ExtraModels ¶
func (*ScanCtx) FileSet ¶ added in v0.34.1
FileSet returns the shared *token.FileSet used by the scan's loaded packages.
Callers that construct a grammar.Parser for comment groups not owned by a single EntityDecl's *packages.Package (notably operation and route path-level annotations aggregated across packages) read the FileSet from here so the produced positions resolve against the same file table the rest of the scan uses.
func (*ScanCtx) FindComments ¶
func (*ScanCtx) FindEnumValues ¶
func (*ScanCtx) FindModel
deprecated
func (s *ScanCtx) FindModel(pkgPath, name string) (*EntityDecl, bool)
FindModel returns the model decl for (pkgPath, name) and, when the hit comes from FindDecl fallback, registers it in ExtraModels as a side effect.
Deprecated: prefer the explicit pair GetModel (pure read) and AddDiscoveredModel (explicit registration). The implicit registration side effect surprises readers and pulls stdlib types (notably time.Time, json.RawMessage) into the spec's top-level definitions when they should be inlined where referenced. See [§model-lookup](./README.md#model-lookup).
func (*ScanCtx) GetModel ¶ added in v0.34.1
func (s *ScanCtx) GetModel(pkgPath, name string) (*EntityDecl, bool)
GetModel is a pure read: it returns the model decl for (pkgPath, name) without any side effect.
Details ¶
See [§model-lookup](./README.md#model-lookup) — the three-source lookup order (Models, ExtraModels, FindDecl), and how this differs from FindModel.
Returns (nil, false) when no matching decl exists in any of the three sources. Callers that want the lookup hit registered as a discovered model must follow up with AddDiscoveredModel explicitly.
func (*ScanCtx) MoveExtraToModel ¶
func (*ScanCtx) NumExtraModels ¶
func (*ScanCtx) OnDiagnostic ¶ added in v0.34.1
func (s *ScanCtx) OnDiagnostic() func(grammar.Diagnostic)
OnDiagnostic returns the user-supplied diagnostic sink, or nil when the consumer has not opted into diagnostic delivery.
Details ¶
See [§diagnostics](./README.md#diagnostics) — callback contract, ordering guarantee, experimental-API caveat.
func (*ScanCtx) Operations ¶
func (s *ScanCtx) Operations() iter.Seq[parsers.ParsedPathContent]
func (*ScanCtx) Parameters ¶
func (s *ScanCtx) Parameters() iter.Seq[*EntityDecl]
func (*ScanCtx) PosOf ¶ added in v0.34.1
PosOf resolves p to a token.Position via the active FileSet. Returns the zero token.Position when p is invalid or no FileSet is available. Useful for attaching a source location to a Diagnostic without each caller re-deriving the FileSet.
func (*ScanCtx) RefAliases ¶
func (*ScanCtx) SetXNullableForPointers ¶
func (*ScanCtx) SkipExtensions ¶
func (*ScanCtx) TransparentAliases ¶
type TypeIndex ¶
type TypeIndex struct {
AllPackages map[string]*packages.Package
Models map[*ast.Ident]*EntityDecl
ExtraModels map[*ast.Ident]*EntityDecl
Meta []*ast.CommentGroup
Routes []parsers.ParsedPathContent
Operations []parsers.ParsedPathContent
Parameters []*EntityDecl
Responses []*EntityDecl
// contains filtered or unexported fields
}
func NewTypeIndex ¶
func NewTypeIndex(pkgs []*packages.Package, opts ...TypeIndexOption) (*TypeIndex, error)
type TypeIndexOption ¶
type TypeIndexOption func(*TypeIndex)
func WithDebug ¶
func WithDebug(enabled bool) TypeIndexOption
func WithExcludeDeps ¶
func WithExcludeDeps(excluded bool) TypeIndexOption
func WithExcludePkgs ¶
func WithExcludePkgs(excluded []string) TypeIndexOption
func WithExcludeTags ¶
func WithExcludeTags(excluded map[string]bool) TypeIndexOption
func WithIncludePkgs ¶
func WithIncludePkgs(included []string) TypeIndexOption
func WithIncludeTags ¶
func WithIncludeTags(included map[string]bool) TypeIndexOption
func WithRefAliases ¶
func WithRefAliases(enabled bool) TypeIndexOption
func WithTransparentAliases ¶
func WithTransparentAliases(enabled bool) TypeIndexOption
func WithXNullableForPointers ¶
func WithXNullableForPointers(enabled bool) TypeIndexOption
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package classify provides small classification predicates used by the scanner and by builders to decide whether a given name or comment line belongs to a particular Swagger-annotation family.
|
Package classify provides small classification predicates used by the scanner and by builders to decide whether a given name or comment line belongs to a particular Swagger-annotation family. |