depgraph

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package depgraph builds a Zero's dependeny injection type graph.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	// Position is the position of the function declaration.
	Position token.Position
	// Pattern is the parsed HTTP mux pattern
	Pattern *directiveparser.DirectiveAPI
	// Function is the function that handles the API
	Function *types.Func
	// Documentation is the extracted function comments
	Documentation string
	// Package is the package that contains the function
	Package *packages.Package
	// OpenAPI is the OpenAPI operation spec for this endpoint
	OpenAPI *spec.Operation
}

API represents a method that is an exposed API endpoint. API endpoints are annotated like so:

//zero:api [<method>] [<host>]/[<path>] [<option>[=<value>] ...]

func (*API) GenerateOpenAPIOperation added in v0.14.0

func (a *API) GenerateOpenAPIOperation(definitions spec.Definitions) *spec.Operation

GenerateOpenAPIOperation creates an OpenAPI operation spec for this API endpoint

func (*API) Label added in v0.1.0

func (a *API) Label(name string) string

type Config added in v0.5.0

type Config struct {
	// Position of the type declaration.
	Position  token.Position
	Type      types.Type
	Directive *directiveparser.DirectiveConfig
	// IsGeneric indicates if this config is a generic type
	IsGeneric bool
	// TypeParams holds the type parameters for generic configs
	TypeParams *types.TypeParamList
}

Config represents command-line/file configuration. Config structs are annotated like so:

//zero:config [prefix="<prefix>"]

type CronJob added in v0.12.0

type CronJob struct {
	// Position is the position of the function declaration.
	Position token.Position
	// Schedule is the parsed cron schedule directive
	Schedule *directiveparser.DirectiveCron
	// Function is the function that handles the cron job
	Function *types.Func
	// Package is the package that contains the function
	Package *packages.Package
}

CronJob represents a cron job method in the graph.

//zero:cron <schedule>

type Graph

type Graph struct {
	Dest           *types.Package
	Providers      map[string][]*Provider // All providers including multi and generic
	Configs        map[string]*Config
	GenericConfigs map[string][]*Config // Generic configs by base type name
	APIs           []*API
	CronJobs       []*CronJob
	Subscriptions  []*Subscription
	Middleware     []*Middleware
	Missing        map[*types.Func][]types.Type
}

func Analyse

func Analyse(ctx context.Context, dest string, options ...Option) (*Graph, error)

Analyse statically loads Go packages, then analyses them for //zero:... annotations in order to build the Zero's dependency injection graph.

func (*Graph) FunctionRef added in v0.1.0

func (g *Graph) FunctionRef(fn *types.Func) Ref

FunctionRef returns a reference to a function, including import information if needed.

func (*Graph) GenerateOpenAPISpec added in v0.14.0

func (g *Graph) GenerateOpenAPISpec(title, version string) *spec.Swagger

GenerateOpenAPISpec creates a complete OpenAPI specification from all API endpoints

func (*Graph) GetProviders added in v0.7.0

func (g *Graph) GetProviders(typeStr string) []*Provider

GetProviders returns all providers for a given type (both single and multi).

func (*Graph) Graph

func (g *Graph) Graph() map[string][]string

Graph returns the dependency graph as a map where keys are type strings and values are slices of their dependency type strings.

func (*Graph) ImportAlias

func (g *Graph) ImportAlias(pkg string) string

ImportAlias returns an alias for the given package path, or "" if the package is the destination package.

func (*Graph) TypeRef added in v0.1.0

func (g *Graph) TypeRef(t types.Type) Ref

TypeRef splits a type into its import alias+path and type reference.

eg. *database/sql.DB would become

impc112c3711fba7de3 "database/sql"
*sql.DB

type Middleware added in v0.1.0

type Middleware struct {
	// Position is the position of the function declaration.
	Position token.Position
	// Directive is the parsed middleware directive
	Directive *directiveparser.DirectiveMiddleware
	// Function is the function that implements the middleware
	Function *types.Func
	// Package is the package that contains the function
	Package *packages.Package
	// Requires are the dependencies required by this middleware
	Requires []types.Type
	// Factory represents whether the middleware is a factory, or direct middleware function
	Factory bool
}

Middleware represents a function that is an HTTP middleware. Middleware functions are annotated like so:

//zero:middleware [<label>]

func (*Middleware) Match added in v0.1.0

func (m *Middleware) Match(api *API) bool

type Option

type Option func(*graphOptions) error

func WithDebug added in v0.0.2

func WithDebug(enable bool) Option

WithDebug enables debug logging.

func WithOptions added in v0.0.2

func WithOptions(options ...Option) Option

func WithPatterns

func WithPatterns(patterns ...string) Option

WithPatterns adds additional package patterns to search for annotations.

func WithProviders

func WithProviders(pick ...string) Option

WithProviders selects a provider for a type if multiple are available.

func WithRoots

func WithRoots(roots ...string) Option

WithRoots selects a set of root types that will always be included in the graph.

func WithTags added in v0.2.0

func WithTags(tags ...string) Option

WithTags adds build tags to the Go toolchain flags.

type Provider

type Provider struct {
	// Position is the position of the function declaration.
	Position  token.Position
	Directive *directiveparser.DirectiveProvider
	// Function is the function that provides the type.
	Function *types.Func
	// Package is the package that contains the function.
	Package  *packages.Package
	Provides types.Type
	Requires []types.Type
	// IsGeneric indicates if this provider is a generic function
	IsGeneric bool
	// TypeParams holds the type parameters for generic providers
	TypeParams *types.TypeParamList
}

A Provider represents a constructor for a type.

type Ref added in v0.1.0

type Ref struct {
	Pkg    string // database/sql
	Import string // "database/sql" or impe1d11ad6baa4124f "database/sql"
	Ref    string // *sql.DB or *impe1d11ad6baa4124f.DB
}

Ref represents a reference to a symbol.

func (Ref) String added in v0.12.0

func (r Ref) String() string

type Subscription added in v0.21.0

type Subscription struct {
	// Position is the position of the function declaration.
	Position token.Position
	// Function is the function that handles the subscription
	Function *types.Func
	// Package is the package that contains the function
	Package *packages.Package
	// TopicType is the event type extracted from pubsub.Event[T]
	TopicType types.Type
}

Jump to

Keyboard shortcuts

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