internal: Annotate all components with groups

This commit ensures that the compiler has to check every component
against the groups they implement by explicitly annotating the
appropriate interfaces.
This commit is contained in:
Tom Wiesing 2022-11-30 11:08:46 +01:00
parent 3455f491ca
commit 890022ae64
No known key found for this signature in database
29 changed files with 163 additions and 33 deletions

View file

@ -2,6 +2,8 @@ package lazy
import (
"reflect"
"github.com/tkw1536/goprogram/lib/reflectx"
)
// Pool represents a pool of laziliy initialized and potentially referencing Component instances.
@ -18,12 +20,23 @@ type Pool[Component any, InitParams any] struct {
// Init is called on every component to be initialized.
Init func(Component, InitParams) Component
// Analytics are written on the first retrieval operation on this Pool
Analytics PoolAnalytics
// Analytics are written on the first retrieval operation on this Pool.
//
// Contains all groups and structs that are referenced during initialization.
// To add extra groups, call RegisterPoolGroup.
Analytics PoolAnalytics
extraGroups []reflect.Type
all Lazy[[]Component]
}
// RegisterPoolGroup registers the given group type to be added to the pools' analytics.
//
// Only groups not referenced during initialization need to be registered explicitly.
func RegisterPoolGroup[Group any, Component any, InitParams any](p *Pool[Component, InitParams]) {
p.extraGroups = append(p.extraGroups, reflectx.TypeOf[Group]())
}
// All initializes or returns all components stored in this pool.
//
// The All function should return an array of calls to [Make] with the provided context.
@ -48,7 +61,7 @@ func (p *Pool[Component, InitParams]) All(Params InitParams, All func(context *P
context.process(all)
// write out analytics
context.anal(&p.Analytics)
context.anal(&p.Analytics, p.extraGroups)
return all
})
}
@ -58,10 +71,6 @@ type PoolContext[Component any] struct {
init func(Component) Component // initializes a new component
all func(context *PoolContext[Component]) []Component // initializes all components
// cache for metas
// function to return all components
metaCache map[reflect.Type]meta[Component]
cache map[string]Component // cached components
queue []delayedInit[Component] // init queue

View file

@ -29,7 +29,7 @@ type PoolAnalyticsGroup struct {
}
// anal writes analytics about this context to anal
func (context *PoolContext[Component]) anal(anal *PoolAnalytics) {
func (context *PoolContext[Component]) anal(anal *PoolAnalytics, groups []reflect.Type) {
anal.Components = make(map[string]*PoolAnalyticsComponent, len(context.metaCache))
anal.Groups = make(map[string]*PoolAnalyticsGroup)
@ -51,6 +51,10 @@ func (context *PoolContext[Component]) anal(anal *PoolAnalytics) {
}
}
// collect interfaces to analyze
ifaces := make([]reflect.Type, len(groups))
copy(ifaces, groups)
// take all of the components out of the cache
for _, meta := range context.metaCache {
anal.Components[meta.Name].Type = meta.Name
@ -59,34 +63,39 @@ func (context *PoolContext[Component]) anal(anal *PoolAnalytics) {
})
anal.Components[meta.Name].IFields = collection.MapValues(meta.IFields, func(key string, iface reflect.Type) string {
name := nameOf(iface)
if _, ok := anal.Groups[name]; !ok {
types := collection.FilterClone(tpPointers, func(tp reflect.Type) bool {
return tp.AssignableTo(iface)
})
anal.Groups[name] = &PoolAnalyticsGroup{
Type: name,
Components: collection.MapSlice(types, func(tp reflect.Type) string {
cname := nameOf(tp.Elem())
anal.Components[cname].Groups = append(anal.Components[cname].Groups, name)
return cname
}),
}
mcount := iface.NumMethod()
anal.Groups[name].Methods = make(map[string]string, mcount)
for i := 0; i < mcount; i++ {
method := iface.Method(i)
anal.Groups[name].Methods[method.Name] = method.Type.String()
}
}
return name
ifaces = append(ifaces, iface)
return nameOf(iface)
})
}
// and analyze all ifaces
for _, iface := range ifaces {
name := nameOf(iface)
if _, ok := anal.Groups[name]; ok {
continue
}
types := collection.FilterClone(tpPointers, func(tp reflect.Type) bool {
return tp.AssignableTo(iface)
})
anal.Groups[name] = &PoolAnalyticsGroup{
Type: name,
Components: collection.MapSlice(types, func(tp reflect.Type) string {
cname := nameOf(tp.Elem())
anal.Components[cname].Groups = append(anal.Components[cname].Groups, name)
return cname
}),
}
mcount := iface.NumMethod()
anal.Groups[name].Methods = make(map[string]string, mcount)
for i := 0; i < mcount; i++ {
method := iface.Method(i)
anal.Groups[name].Methods[method.Name] = method.Type.String()
}
}
for _, comp := range anal.Components {
slices.Sort(comp.Groups)
}