Migrate pkg/lazy => pkglib/{lazy,lifetime}

This commit is contained in:
Tom Wiesing 2023-02-26 10:00:47 +01:00
parent c3ca8e2974
commit 5e89fadeeb
No known key found for this signature in database
21 changed files with 54 additions and 612 deletions

View file

@ -2,8 +2,8 @@ package dis
import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/collection"
"github.com/tkw1536/pkglib/lifetime"
)
//
@ -11,18 +11,19 @@ import (
//
func (dis *Distillery) init() {
dis.poolInit.Do(func() {
dis.pool.Init = component.Init
lazy.RegisterPoolGroup[component.Backupable](&dis.pool)
lazy.RegisterPoolGroup[component.Snapshotable](&dis.pool)
lazy.RegisterPoolGroup[component.DistilleryFetcher](&dis.pool)
lazy.RegisterPoolGroup[component.Installable](&dis.pool)
lazy.RegisterPoolGroup[component.Provisionable](&dis.pool)
lazy.RegisterPoolGroup[component.Routeable](&dis.pool)
lazy.RegisterPoolGroup[component.Cronable](&dis.pool)
lazy.RegisterPoolGroup[component.UserDeleteHook](&dis.pool)
lazy.RegisterPoolGroup[component.Table](&dis.pool)
lazy.RegisterPoolGroup[component.Menuable](&dis.pool)
dis.lifetimeInit.Do(func() {
dis.lifetime.Init = component.Init
lifetime.RegisterGroup[component.Backupable](&dis.lifetime)
lifetime.RegisterGroup[component.Snapshotable](&dis.lifetime)
lifetime.RegisterGroup[component.DistilleryFetcher](&dis.lifetime)
lifetime.RegisterGroup[component.Installable](&dis.lifetime)
lifetime.RegisterGroup[component.Provisionable](&dis.lifetime)
lifetime.RegisterGroup[component.Routeable](&dis.lifetime)
lifetime.RegisterGroup[component.Cronable](&dis.lifetime)
lifetime.RegisterGroup[component.UserDeleteHook](&dis.lifetime)
lifetime.RegisterGroup[component.Table](&dis.lifetime)
lifetime.RegisterGroup[component.Menuable](&dis.lifetime)
})
}
@ -33,13 +34,13 @@ func (dis *Distillery) init() {
// manual initializes a component from the provided distillery.
func manual[C component.Component](init func(component C)) initFunc {
return func(context ctx) component.Component {
return lazy.Make(context, init)
return lifetime.Make(context, init)
}
}
// use is like r, but does not provided additional initialization
func auto[C component.Component](context ctx) component.Component {
return lazy.Make[component.Component, C](context, nil)
return lifetime.Make[component.Component, C](context, nil)
}
// register returns all components of the distillery
@ -53,7 +54,7 @@ func (dis *Distillery) register(context ctx) []component.Component {
}
// ctx is a context for component initialization
type ctx = *lazy.PoolContext[component.Component]
type ctx = *lifetime.InjectorContext[component.Component]
//
// ==== export ====
@ -62,12 +63,12 @@ type ctx = *lazy.PoolContext[component.Component]
// export is a convenience function to export a single component
func export[C component.Component](dis *Distillery) C {
dis.init()
return lazy.ExportComponent[component.Component, component.Still, C](&dis.pool, dis.Still, dis.register)
return lifetime.ExportComponent[component.Component, component.Still, C](&dis.lifetime, dis.Still, dis.register)
}
func exportAll[C component.Component](dis *Distillery) []C {
dis.init()
return lazy.ExportComponents[component.Component, component.Still, C](&dis.pool, dis.Still, dis.register)
return lifetime.ExportComponents[component.Component, component.Still, C](&dis.lifetime, dis.Still, dis.register)
}
type initFunc = func(context ctx) component.Component

View file

@ -7,9 +7,9 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/gorilla/sessions"
"github.com/julienschmidt/httprouter"
"github.com/tkw1536/pkglib/lazy"
)
type Auth struct {

View file

@ -11,7 +11,7 @@ import (
// Components represents a logical subsystem of the distillery.
// A Component should be implemented as a pointer to a struct.
// Every component must embed [Base] and should be initialized using [Init] inside a [lazy.Pool].
// Every component must embed [Base] and should be initialized using [Init] inside a [lifetime.Lifetime].
//
// By convention these are defined within their corresponding subpackage.
// This subpackage also contains all required resources.
@ -41,7 +41,7 @@ func (cb *Base) getBase() *Base {
}
// Init initialzes a new componeont Component with the provided still.
// Init is only initended to be used within a lazy.Pool[Component,Still].
// Init is only initended to be used within a lifetime.Lifetime[Component,Still].
func Init(component Component, core Still) Component {
base := component.getBase() // pointer to a struct
base.Still = core

View file

@ -15,8 +15,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/lazy"
_ "embed"
)

View file

@ -11,10 +11,10 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/julienschmidt/httprouter"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/lifetime"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
)
type Admin struct {
@ -33,7 +33,7 @@ type Admin struct {
Sockets *socket.Sockets
}
Analytics *lazy.PoolAnalytics
Analytics *lifetime.Analytics
}
var (

View file

@ -12,8 +12,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/julienschmidt/httprouter"
"github.com/tkw1536/pkglib/lifetime"
)
//go:embed "html/anal.html"
@ -27,7 +27,7 @@ var analTemplate = templating.Parse[analContext](
type analContext struct {
templating.RuntimeFlags
Analytics lazy.PoolAnalytics
Analytics lifetime.Analytics
}
func (admin *Admin) components(ctx context.Context) http.Handler {

View file

@ -9,7 +9,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/lazy"
)
type Home struct {

View file

@ -2,7 +2,7 @@ package templating
import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/lazy"
)
// Templating implements templating customization

View file

@ -7,7 +7,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/lazy"
)
type SQL struct {

View file

@ -34,7 +34,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/ssh2/sshkeys"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/triplestore"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/web"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/lifetime"
)
// Distillery represents a WissKI Distillery
@ -54,9 +54,9 @@ type Distillery struct {
// But for now this will just hold upstream configuration.
Upstream Upstream
// pool holds all components
pool lazy.Pool[component.Component, component.Still]
poolInit sync.Once
// lifetime holds all components
lifetime lifetime.Lifetime[component.Component, component.Still]
lifetimeInit sync.Once
}
// Upstream contains the configuration for accessing remote configuration.
@ -186,7 +186,7 @@ func (dis *Distillery) allComponents() []initFunc {
resolver.RefreshInterval = time.Minute
}),
manual(func(admin *admin.Admin) {
admin.Analytics = &dis.pool.Analytics
admin.Analytics = &dis.lifetime.Analytics
}),
auto[*socket.Sockets],
auto[*legal.Legal],

View file

@ -11,10 +11,10 @@ import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/goprogram/stream"
"github.com/tkw1536/pkglib/collection"
"github.com/tkw1536/pkglib/contextx"
"github.com/tkw1536/pkglib/lazy"
"github.com/tkw1536/pkglib/nobufio"
)

View file

@ -7,7 +7,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/lifetime"
"golang.org/x/sync/errgroup"
)
@ -18,7 +18,7 @@ type Info struct {
Fetchers []ingredient.WissKIFetcher
}
Analytics *lazy.PoolAnalytics
Analytics *lifetime.Analytics
}
var (

View file

@ -9,7 +9,7 @@ import (
// Ingredients represent a part of a WissKI instance.
// An Ingredient should be implemented as a pointer to a struct.
// Every ingredient must embed [Base] and should be initialized using [Init] inside a [lazy.Pool].
// Every ingredient must embed [Base] and should be initialized using [Init] inside a [lifetime.Lifetime].
//
// By convention these are defined within their corresponding subpackage.
// This subpackage also contains all required resources.
@ -35,7 +35,7 @@ func (cb *Base) getBase() *Base {
}
// Init initializes a new Ingredient.
// Init is only intended to be used within a lazy.Pool[Ingredient,*Liquid].
// Init is only intended to be used within a lifetime.Lifetime[Ingredient,*Liquid].
func Init(ingredient Ingredient, liquid *liquid.Liquid) Ingredient {
base := ingredient.getBase() // pointer to a struct
base.Liquid = liquid

View file

@ -3,8 +3,8 @@ package wisski
import (
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/liquid"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/collection"
"github.com/tkw1536/pkglib/lifetime"
)
//
@ -12,9 +12,9 @@ import (
//
func (wisski *WissKI) init() {
wisski.poolInit.Do(func() {
wisski.pool.Init = ingredient.Init
lazy.RegisterPoolGroup[ingredient.WissKIFetcher](&wisski.pool)
wisski.lifetimeInit.Do(func() {
wisski.lifetime.Init = ingredient.Init
lifetime.RegisterGroup[ingredient.WissKIFetcher](&wisski.lifetime)
})
}
@ -25,13 +25,13 @@ func (wisski *WissKI) init() {
// manual initializes a component from the provided distillery.
func manual[I ingredient.Ingredient](init func(ingredient I)) initFunc {
return func(context ctx) ingredient.Ingredient {
return lazy.Make(context, init)
return lifetime.Make(context, init)
}
}
// use is like r, but does not provided additional initialization
func auto[I ingredient.Ingredient](context ctx) ingredient.Ingredient {
return lazy.Make[ingredient.Ingredient, I](context, nil)
return lifetime.Make[ingredient.Ingredient, I](context, nil)
}
// register returns all components of the distillery
@ -45,7 +45,7 @@ func (wisski *WissKI) register(context ctx) []ingredient.Ingredient {
}
// ctx is a context for component initialization
type ctx = *lazy.PoolContext[ingredient.Ingredient]
type ctx = *lifetime.InjectorContext[ingredient.Ingredient]
//
// ==== export ====
@ -54,13 +54,13 @@ type ctx = *lazy.PoolContext[ingredient.Ingredient]
// export is a convenience function to export a single component
func export[I ingredient.Ingredient](wisski *WissKI) I {
wisski.init()
return lazy.ExportComponent[ingredient.Ingredient, *liquid.Liquid, I](&wisski.pool, &wisski.Liquid, wisski.register)
return lifetime.ExportComponent[ingredient.Ingredient, *liquid.Liquid, I](&wisski.lifetime, &wisski.Liquid, wisski.register)
}
//lint:ignore U1000 for future use
func exportAll[I ingredient.Ingredient](wisski *WissKI) []I {
wisski.init()
return lazy.ExportComponents[ingredient.Ingredient, *liquid.Liquid, I](&wisski.pool, &wisski.Liquid, wisski.register)
return lifetime.ExportComponents[ingredient.Ingredient, *liquid.Liquid, I](&wisski.lifetime, &wisski.Liquid, wisski.register)
}
type initFunc = func(context ctx) ingredient.Ingredient

View file

@ -18,7 +18,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php/users"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/reserve"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/liquid"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/pkglib/lifetime"
)
// WissKI represents a single WissKI Instance.
@ -26,8 +26,8 @@ import (
type WissKI struct {
liquid.Liquid
poolInit sync.Once
pool lazy.Pool[ingredient.Ingredient, *liquid.Liquid]
lifetimeInit sync.Once
lifetime lifetime.Lifetime[ingredient.Ingredient, *liquid.Liquid]
}
//
@ -110,7 +110,7 @@ func (wisski *WissKI) allIngredients() []initFunc {
// info
manual(func(info *info.Info) {
info.Analytics = &wisski.pool.Analytics
info.Analytics = &wisski.lifetime.Analytics
}),
auto[*barrel.LastRebuildFetcher],
auto[*barrel.RunningFetcher],