analyzer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package analyzer provides AST analysis for struct types and converter registrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dereference

func Dereference(t types.Type) types.Type

Dereference removes pointer indirection from a type.

func IsPointer

func IsPointer(t types.Type) bool

IsPointer returns true if the type is a pointer type.

func IsSlice

func IsSlice(t types.Type) bool

IsSlice returns true if the type is a slice type.

func IsStruct

func IsStruct(t types.Type) bool

IsStruct returns true if the type is a struct (or pointer to struct).

func NormalizeTypeKey

func NormalizeTypeKey(typeStr string, pkg *packages.Package) string

NormalizeTypeKey normalizes a type key for consistent comparison.

func QualifiedTypeName

func QualifiedTypeName(t types.Type) string

QualifiedTypeName returns a fully qualified type name for a types.Type.

func ShortTypeName

func ShortTypeName(t types.Type) string

ShortTypeName returns a short type name suitable for function naming.

func SliceElem

func SliceElem(t types.Type) types.Type

SliceElem returns the element type of a slice, or nil if not a slice.

func TypeKeyFromTypes

func TypeKeyFromTypes(t types.Type) string

TypeKeyFromTypes creates a type key string from types.Type for registry lookup.

func TypeName

func TypeName(t types.Type) string

TypeName returns the base type name for a type, stripping pointers and slices.

func TypePkgPath

func TypePkgPath(t types.Type) string

TypePkgPath returns the package path for a named type, or empty string.

Types

type Analyzer

type Analyzer struct {
	// contains filtered or unexported fields
}

Analyzer loads and analyzes Go packages.

func New

func New() *Analyzer

New creates a new Analyzer.

func (*Analyzer) FindStruct

func (a *Analyzer) FindStruct(pkg *packages.Package, name string) (*StructInfo, error)

FindStruct finds a struct type by name in a package.

func (*Analyzer) LoadPackage

func (a *Analyzer) LoadPackage(pattern string) (*packages.Package, error)

LoadPackage loads a package by its import path or directory.

func (*Analyzer) ResolveType

func (a *Analyzer) ResolveType(typeStr string, basePkg *packages.Package) (*StructInfo, error)

ResolveType resolves a type string like "userpb.User" to its full information.

type ConverterInfo

type ConverterInfo struct {
	// SourceType is the source type (A in RegisterTo[A, B])
	SourceType string
	// TargetType is the target type (B in RegisterTo[A, B])
	TargetType string
	// FuncName is the qualified function name being registered
	FuncName string
	// FuncPkg is the package path where the function is defined
	FuncPkg string
	// HasError indicates if the converter returns an error
	HasError bool
	// Name is the converter name for named converters (empty for unnamed)
	Name string
	// Direction indicates "to" or "from"
	Direction string
}

ConverterInfo holds information about a discovered converter registration.

func DiscoverConverters

func DiscoverConverters(pkg *packages.Package) ([]ConverterInfo, error)

DiscoverConverters finds all automapper.Register* calls in a package.

type FieldInfo

type FieldInfo struct {
	// Name is the field name
	Name string
	// Type is the field's Go type
	Type types.Type
	// TypeStr is the string representation of the type
	TypeStr string
	// Tag is the raw struct tag (including backticks)
	Tag string
	// Exported indicates if the field is exported
	Exported bool
}

FieldInfo holds information about a struct field.

type MapTag

type MapTag struct {
	// Ignore indicates map:"-" was specified
	Ignore bool
	// TargetName is the target field name if specified (e.g., map:"TargetName")
	TargetName string
	// Converter is the named converter to use (e.g., map:",conv=name")
	Converter string
}

MapTag represents a parsed map:"..." struct tag.

func ParseMapTag

func ParseMapTag(rawTag string) MapTag

ParseMapTag parses the map:"..." tag from a raw struct tag string. The raw tag includes backticks, e.g., `json:"name" map:"TargetName,conv=foo"`.

type StructInfo

type StructInfo struct {
	// Name is the type name (e.g., "User")
	Name string
	// PkgPath is the import path (e.g., "myproject/model")
	PkgPath string
	// PkgName is the package name (e.g., "model")
	PkgName string
	// Fields contains information about each exported field
	Fields []FieldInfo
}

StructInfo holds information about a struct type.

func (*StructInfo) QualifiedName

func (s *StructInfo) QualifiedName() string

QualifiedName returns the fully qualified type name (e.g., "model.User").

Jump to

Keyboard shortcuts

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