analyzer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Analyze added in v0.1.4

func Analyze(projectPath string, cfg *config.Config) (*model.APIModel, error)

func HumanizeSummary added in v1.0.0

func HumanizeSummary(summary, funcName string) string

HumanizeSummary turns a Go doc comment's opening line into something that reads as an API summary.

Go convention is for a comment to begin with the identifier it documents, so handlers yield "ListBooks returns the catalogue." Rendered next to a path in an API browser that is noise, and every operation ends up prefixed with a name the reader can already see. Dropping the leading identifier gives "Returns the catalogue."

Types

type Diagnostic added in v1.0.0

type Diagnostic struct {
	Severity Severity
	Pos      token.Pos
	Message  string
	// Location is the resolved "file:line:col", filled in by Render.
	Location string
}

Diagnostic is a single message about the analysis, anchored to source.

Every branch where the analyzer gives up on a route emits one of these. Before they existed, an unresolvable handler produced an empty spec with no explanation, which is what made github.com/Zachacious/go-respec issue #1 hard to act on: the reporter could see the routes were missing but not why.

type ParsedComment

type ParsedComment struct {
	// Summary is a brief summary of the comment.
	Summary string
	// Description is a longer description of the comment.
	Description string
	// Tags are a list of tags extracted from the comment.
	Tags []string
	// SummaryExplicit reports whether Summary came from an @summary line rather
	// than from the first line of prose. An explicit summary is used verbatim;
	// a derived one is reworded for documentation.
	SummaryExplicit bool
}

ParsedComment holds the structured data extracted from a doc comment.

type ResolvedType added in v0.1.4

type ResolvedType struct {
	// The canonical representation of the type. This is what we use for reliable comparisons.
	Object *types.Named
	// A pointer to the original definition from the config file.
	Definition *config.RouterDefinition
}

ResolvedType represents a type from the config that has been resolved to its canonical go/types object.

type SchemaGenerator

type SchemaGenerator struct {

	// The final map of named components that will be added to the spec.
	Components map[string]*openapi3.SchemaRef
	// contains filtered or unexported fields
}

SchemaGenerator turns Go types into OpenAPI schema definitions.

func NewSchemaGenerator

func NewSchemaGenerator() *SchemaGenerator

NewSchemaGenerator returns a new SchemaGenerator instance.

func (*SchemaGenerator) GenerateSchema added in v0.1.4

func (sg *SchemaGenerator) GenerateSchema(t types.Type) *openapi3.SchemaRef

GenerateSchema is the main public entry point for creating a schema from a Go type.

type Severity added in v1.0.0

type Severity int

Severity classifies how much a diagnostic affects the generated spec.

const (
	// SeverityWarning marks something respec could not fully resolve. The spec
	// is still produced, but part of it is missing or less precise.
	SeverityWarning Severity = iota
	// SeverityInfo reports a resolution respec made that the user may want to
	// verify, such as following a handler through a factory function.
	SeverityInfo
)

func (Severity) String added in v1.0.0

func (s Severity) String() string

type SpecSite added in v1.0.0

type SpecSite struct {
	// Metadata parsed from the builder chain.
	Metadata *meta.HandlerMetadata
	// HandlerExpr is the argument given to respec.Handler.
	HandlerExpr ast.Expr
	// HandlerObj is the handler's object, when it is a named function. Nil for
	// function literals.
	HandlerObj types.Object
	// HandlerBody is the code to scan for schemas and parameters, from either a
	// declaration or a literal.
	HandlerBody *ast.BlockStmt
	// Name is a human-readable handler name used for summaries and operation IDs.
	Name string
	// Pos anchors diagnostics to the builder chain.
	Pos token.Pos
	// Linked records whether any route resolved to this site. An unlinked site
	// means the user wrote metadata that never reached the spec.
	Linked bool
}

SpecSite is one `respec.Handler(...)....Unwrap()` chain found in the source.

Metadata is keyed by the site rather than by the handler's types.Object, because a handler object is not a unique key: an anonymous function has none, and a handler mounted on two routes would otherwise share (and overwrite) a single metadata entry.

type State added in v0.1.4

type State struct {
	Fset *token.FileSet

	// A map of fully-qualified type names to their resolved canonical types.
	// This is populated by the resolver (Phase 1).
	// Example key: "github.com/go-chi/chi/v5.Mux"
	ResolvedRouterTypes map[string]*ResolvedType
	// ResolvedRouterNames lists the router types actually found in the project,
	// for reporting.
	ResolvedRouterNames []string

	// The discovered universe of all relevant declarations in the project.
	// This is populated by the universe discoverer (Phase 2).
	Universe *Universe

	// --- Data Flow Analysis State ---
	// A map to link variable/parameter objects to the tracked value they hold.
	VarValues map[types.Object]*TrackedValue

	// Diagnostics collected during analysis. Every place the analyzer gives up
	// on a route records one; silence used to be the only signal that something
	// had been dropped.
	Diagnostics []Diagnostic

	// The root of the final constructed API route graph.
	RouteGraph *model.RouteNode

	// --- Schema Generation State ---
	// The schema generator instance.
	SchemaGen *SchemaGenerator

	Config *config.Config

	GroupMetadata model.GroupMetadataMap

	// SpecSites holds every respec.Handler builder chain found in the source.
	SpecSites []*SpecSite
	// contains filtered or unexported fields
}

State is the central data structure that holds all information gathered during the multi-phase analysis of the target project.

func NewState added in v0.1.4

func NewState(pkgs []*packages.Package, cfg *config.Config) (*State, error)

NewState creates a new State instance.

func (*State) FindAndParseRouteMetadata added in v0.2.8

func (s *State) FindAndParseRouteMetadata()

FindAndParseRouteMetadata locates every builder chain and indexes it by the ways a route can refer to it: inline, through a variable, or through a factory function.

func (*State) FindGroupMetadata added in v0.2.2

func (s *State) FindGroupMetadata()

FindGroupMetadata scans the project for `respec.Meta(r)` calls and builds a map associating the router variable `r` with its chained metadata.

func (*State) Render added in v1.0.0

func (s *State) Render() []Diagnostic

Render resolves positions to file locations and returns the diagnostics in a stable order, so output does not churn between runs.

func (*State) SprintNode added in v0.3.0

func (s *State) SprintNode(node ast.Node) string

SprintNode converts an AST node back to its string representation.

type TrackedValue added in v0.1.4

type TrackedValue struct {
	// The expression where this value was created (e.g., the `chi.NewRouter()` call).
	Source ast.Expr
	// The specific router definition this value corresponds to.
	RouterDef *config.RouterDefinition

	// --- Chaining and Hierarchy ---
	Parent     *TrackedValue
	PathPrefix string
	// The node in our API graph that corresponds to this specific router or group.
	Node *model.RouteNode
}

TrackedValue represents a value (e.g., a router instance) that we are tracing through the program's data flow.

type Universe added in v0.1.4

type Universe struct {
	// A map of function objects to their AST declaration nodes.
	Functions map[types.Object]*ast.FuncDecl

	// A map of constant objects to their value specifications.
	// This helps in resolving path segments that are defined as constants.
	Constants map[types.Object]*ast.ValueSpec
}

Universe contains maps of all relevant top-level declarations in the project. This serves as a quick lookup table for the rest of the analysis.

Jump to

Keyboard shortcuts

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