typegraph

package
v0.0.0-...-5b643b5 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

typegraph forms a graph by drawing edges between a named type and other named type.

Index

Constants

View Source
const (
	MatchKindMatched = MatchKind(1 << iota)
	MatchKindDependant
	MatchKindExternal
)
View Source
const (
	EdgeKindAlias = EdgeKind(1 << iota)
	EdgeKindArray
	EdgeKindChan
	EdgeKindInterface
	EdgeKindMap
	EdgeKindNamed
	EdgeKindPointer
	EdgeKindSlice
	EdgeKindStruct
)

Variables

This section is empty.

Functions

func FirstTypeIdent

func FirstTypeIdent(m map[Ident][]Edge) (Ident, Edge)

func TraverseToNamed

func TraverseToNamed(
	ty types.Type,
	cb func(named *types.Named, stack []EdgeRouteNode) error,
	stack []EdgeRouteNode,
) error

func TraverseTypes

func TraverseTypes(
	ty types.Type,
	stopper func(ty types.Type, currentStack []EdgeRouteNode) bool,
	cb func(ty types.Type, named *types.Named, stack []EdgeRouteNode) error,
	stack []EdgeRouteNode,
) error

Types

type Edge

type Edge struct {
	Stack    []EdgeRouteNode
	TypeArgs []TypeArg
	// non-instantiated parent
	ParentNode *Node
	// instantiated child
	ChildType *types.Named
	// non-instantiated child node.
	ChildNode *Node
}

func (Edge) HasSingleNamedTypeArg

func (e Edge) HasSingleNamedTypeArg(additionalCond func(named *types.Named) bool) (ok bool, pointer bool)

func (Edge) IsChildMatched

func (e Edge) IsChildMatched() bool

func (Edge) IsTypeArgMatched

func (e Edge) IsTypeArgMatched() bool

func (Edge) LastPointer

func (e Edge) LastPointer() option.Option[EdgeRouteNode]

func (Edge) PrintChildArg

func (e Edge) PrintChildArg(i int, importMap imports.ImportMap) string

func (Edge) PrintChildArgConverted

func (e Edge) PrintChildArgConverted(converter func(ty *types.Named, isMatched bool) (*types.Named, bool), importMap imports.ImportMap) string

func (Edge) PrintChildType

func (e Edge) PrintChildType(importMap imports.ImportMap) string

type EdgeKind

type EdgeKind uint64

type EdgeMap

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

func (EdgeMap) ByFieldName

func (em EdgeMap) ByFieldName(fieldName string) (Edge, *types.Var, reflect.StructTag, bool)

ByFieldName is like EdgeMap.ByFieldPos but queries for fieldName.

func (EdgeMap) ByFieldPos

func (em EdgeMap) ByFieldPos(pos int) (Edge, *types.Var, reflect.StructTag, bool)

ByFieldPos returns the edge, the field var and the struct tag for the field positioned at pos in source code order, It assumes node's underlying is struct type, otherwise panics.

func (EdgeMap) Fields

func (em EdgeMap) Fields() iter.Seq2[int, Edge]

Fields enumerates its children edges as iter.Seq2[int, typeDependencyEdge] assuming node's underlying type is struct. The key of the iterator is position of field in source code order.

func (EdgeMap) FieldsName

func (em EdgeMap) FieldsName() iter.Seq2[string, Edge]

FieldsName is like EdgeMap.Fields but the key of the pair is field name.

func (EdgeMap) First

func (em EdgeMap) First() (Ident, Edge, bool)

type EdgeRouteNode

type EdgeRouteNode struct {
	Kind EdgeKind
	Pos  option.Option[int]
}

type Graph

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

Graph enumerates type decls in given []*packages.Package and forms a type-dependency graph. It lists types which matches input matcher. Callers can traverse graph from a node upwards and downwards.

. ... +──────+ . ┠─... │ node │────... +──────+ ┠─(as struct member)──────────────+──────+ │ node │──╂─(as struct member map elem)─────│ node │────... +──────+ ┠─(as struct member elt of [][]T)─+──────+ . ┠─... │ node │────... . ... +──────+

Nodes are connected by [typeDependencyEdge].

func New

func New(
	pkgs []*packages.Package,
	matcher func(node *Node, external bool) (bool, error),
	genDeclFilter func(*ast.GenDecl) (bool, error),
	typeSpecFilter func(*ast.TypeSpec, types.Object) (bool, error),
	opts ...Option,
) (*Graph, error)

func (*Graph) EnumerateTypes

func (g *Graph) EnumerateTypes() iter.Seq2[Ident, *Node]

func (*Graph) EnumerateTypesKeys

func (g *Graph) EnumerateTypesKeys(keys iter.Seq[Ident]) iter.Seq2[Ident, *Node]

func (*Graph) GatherReplaceData

func (g *Graph) GatherReplaceData(
	parser *imports.ImportParser,
	seqFactory func(g *Graph) iter.Seq2[Ident, *Node],
) (data map[*ast.File]*ReplaceData, err error)

GatherReplaceData converts g into a map of *ast.File to *ReplaceData. The data can be later used to modify ast.

func (*Graph) Get

func (g *Graph) Get(i Ident) (*Node, bool)

func (*Graph) GetByType

func (g *Graph) GetByType(ty types.Type) (*Node, bool)

func (*Graph) IterUpward

func (g *Graph) IterUpward(includeMatched bool, edgeFilter func(edge Edge) bool) iter.Seq2[Ident, *Node]

func (*Graph) MarkDependant

func (g *Graph) MarkDependant(edgeFilter func(edge Edge) bool)

type Ident

type Ident struct {
	PkgPath  string
	TypeName string
}

func IdentFromTypesObject

func IdentFromTypesObject(obj types.Object) Ident

func (Ident) TargetType

func (t Ident) TargetType() imports.TargetType

type MatchKind

type MatchKind uint64

func (MatchKind) IsDependant

func (k MatchKind) IsDependant() bool

func (MatchKind) IsExternal

func (k MatchKind) IsExternal() bool

func (MatchKind) IsMatched

func (k MatchKind) IsMatched() bool

type Node

type Node struct {
	Parent   map[Ident][]Edge
	Children map[Ident][]Edge

	Matched MatchKind

	Pkg  *packages.Package
	File *ast.File
	// nth type spec in the file.
	Pos  int
	Ts   *ast.TypeSpec
	Type *types.Named

	Priv any
}

func (*Node) ByFieldName

func (n *Node) ByFieldName(name string) (Edge, *types.Var, reflect.StructTag, bool)

func (*Node) ChildEdgeMap

func (n *Node) ChildEdgeMap(edgeFilter func(edge Edge) bool) EdgeMap

func (*Node) Fields

func (n *Node) Fields() iter.Seq2[int, Edge]

Fields enumerates its children edges as iter.Seq2[int, typeDependencyEdge] assuming n's underlying type is struct. The key of the iterator is position of field in source code order.

func (*Node) FieldsName

func (n *Node) FieldsName() iter.Seq2[string, Edge]

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithPrivParser

func WithPrivParser(privParser PrivParser) Option

type PrivParser

type PrivParser func(n *Node) (any, error)

type ReplaceData

type ReplaceData struct {
	Filename    string
	Dec         *decorator.Decorator
	DstFile     *dst.File
	ImportMap   imports.ImportMap
	TargetNodes []*Node
}

ReplaceData is used to replace types in each file.

type TypeArg

type TypeArg struct {
	Route []EdgeRouteNode
	Node  *Node
	Ty    *types.Named
	Org   types.Type
}

Jump to

Keyboard shortcuts

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