Documentation
¶
Overview ¶
Package sentinel provides struct metadata extraction and relationship discovery for Go.
Index ¶
Constants ¶
const ( RelationshipReference = "reference" // Direct field reference (e.g., Profile *Profile) RelationshipCollection = "collection" // Slice/array of types (e.g., Orders []Order) RelationshipEmbedding = "embedding" // Anonymous field embedding RelationshipMap = "map" // Map with struct values )
RelationshipKind constants for different relationship types.
Variables ¶
var ErrNotStruct = errors.New("sentinel: only struct types are supported")
ErrNotStruct is returned when a non-struct type is passed to Try* functions.
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache stores extracted metadata permanently. Since types are immutable at runtime, entries never expire.
func (*Cache) Clear ¶
func (c *Cache) Clear()
Clear removes all entries from the cache. This should only be used in tests.
type FieldKind ¶
type FieldKind string
FieldKind represents the category of a field's type.
const ( KindScalar FieldKind = "scalar" // Basic types: string, int, float, bool, etc. KindPointer FieldKind = "pointer" // Pointer to any type KindSlice FieldKind = "slice" // Slice or array KindStruct FieldKind = "struct" // Struct type KindMap FieldKind = "map" // Map type KindInterface FieldKind = "interface" // Interface type )
FieldKind constants for type categorization.
type FieldMetadata ¶
type FieldMetadata struct {
ReflectType reflect.Type `json:"-"`
Tags map[string]string `json:"tags,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
Kind FieldKind `json:"kind"`
Index []int `json:"index"`
}
FieldMetadata captures field-level information and all struct tags.
type Metadata ¶
type Metadata struct {
ReflectType reflect.Type `json:"-"`
FQDN string `json:"fqdn"` // Fully qualified type name (e.g., "github.com/app/models.User")
TypeName string `json:"type_name"` // Simple type name (e.g., "User")
PackageName string `json:"package_name"` // Package path (e.g., "github.com/app/models")
Fields []FieldMetadata `json:"fields"`
Relationships []TypeRelationship `json:"relationships,omitempty"`
}
Metadata contains comprehensive information about a user model.
func Lookup ¶
Lookup returns cached metadata for a type name if it exists. This allows external packages to access metadata that has already been extracted.
func Scan ¶
Scan performs recursive inspection of a type and all related types within the same module. Unlike Inspect which only processes a single type, Scan will follow relationships and automatically inspect any related types that share the same module root. Panics if T is not a struct type.
func TryInspect ¶
TryInspect returns comprehensive metadata for a type. Returns ErrNotStruct if T is not a struct type.
func TryScan ¶
TryScan performs recursive inspection of a type and all related types within the same module. Unlike TryInspect which only processes a single type, TryScan will follow relationships and automatically inspect any related types that share the same module root. Returns ErrNotStruct if T is not a struct type.
type Sentinel ¶
type Sentinel struct {
// contains filtered or unexported fields
}
Sentinel is the main type intelligence orchestrator. It provides metadata extraction and caching.
type TypeRelationship ¶
type TypeRelationship struct {
From string `json:"from"` // Source type name
To string `json:"to"` // Target type name
Field string `json:"field"` // Field creating the relationship
Kind string `json:"kind"` // "reference", "collection", "embedding", "map"
ToPackage string `json:"to_package"` // Target type's package path
}
TypeRelationship represents a relationship between two types.
func GetReferencedBy ¶
func GetReferencedBy[T any]() []TypeRelationship
GetReferencedBy returns all types that reference the given type. This performs a reverse lookup across all cached metadata.
func GetRelationships ¶
func GetRelationships[T any]() []TypeRelationship
GetRelationships returns all relationships from a type to other types.