Skip to content

Commit

Permalink
ref(proxy): rename runner to controller (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 20, 2023
1 parent 0836fea commit a195177
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/snivilised/extendio/xfs/nav"
)

type FullRunner struct {
baseRunner
type FullController struct {
controller
}

func (r *FullRunner) OnNewShrinkItem(item *nav.TraverseItem,
func (c *FullController) OnNewShrinkItem(item *nav.TraverseItem,
positional []string,
thirdPartyCL cobrass.ThirdPartyCommandLine,
) error {
Expand Down
46 changes: 46 additions & 0 deletions src/app/proxy/controller-registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package proxy

import (
"sync"
)

func NewControllerRegistry(shared *SharedControllerInfo) *ControllerRegistry {
return &ControllerRegistry{
pool: sync.Pool{
// see: https://www.sobyte.net/post/2022-03/think-in-sync-pool/
//
New: func() interface{} {
switch shared.Type {
case ControllerTypeFullEn:
return &FullController{
controller: controller{
shared: shared,
},
}

case ControllerTypeSamplerEn:
return &SamplerController{
controller: controller{
shared: shared,
},
}
}

panic("undefined controller type")
},
},
}
}

type ControllerRegistry struct {
pool sync.Pool
}

func (cr *ControllerRegistry) Get() ItemController {
return cr.pool.Get().(ItemController)
}

func (cr *ControllerRegistry) Put(controller ItemController) {
controller.Reset()
cr.pool.Put(controller)
}
33 changes: 33 additions & 0 deletions src/app/proxy/controller-sampler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package proxy

import (
"github.com/snivilised/extendio/xfs/nav"
)

type SamplerController struct {
controller
}

func (c *SamplerController) OnNewShrinkItem(item *nav.TraverseItem,
positional []string,
) error {
_ = positional

profileName := c.shared.Inputs.Root.ProfileFam.Native.Profile
schemeName := c.shared.Inputs.Root.ProfileFam.Native.Scheme

var sequence Sequence

switch {
case profileName != "":
sequence = c.profileSequence(profileName, item.Path)

case schemeName != "":
sequence = c.schemeSequence(schemeName, item.Path)

default:
sequence = c.adhocSequence(item.Path)
}

return c.Run(item, sequence)
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func resetFS(index string, silent bool) (vfs storage.VirtualFS, root string) {
return vfs, root
}

type runnerTE struct {
type controllerTE struct {
given string
should string
args []string
Expand All @@ -169,11 +169,11 @@ type runnerTE struct {
}

type samplerTE struct {
runnerTE
controllerTE
scheme string
}

var _ = Describe("SamplerRunner", Ordered, func() {
var _ = Describe("SamplerController", Ordered, func() {
var (
repo string
l10nPath string
Expand Down Expand Up @@ -280,7 +280,7 @@ var _ = Describe("SamplerRunner", Ordered, func() {
},

Entry(nil, &samplerTE{
runnerTE: runnerTE{
controllerTE: controllerTE{
given: "profile",
should: "sample(first) with glob filter using the defined profile",
relative: backyardWorldsPlanet9Scan01,
Expand All @@ -297,7 +297,7 @@ var _ = Describe("SamplerRunner", Ordered, func() {
}),

Entry(nil, &samplerTE{
runnerTE: runnerTE{
controllerTE: controllerTE{
given: "profile",
should: "sample(last) with glob filter using the defined profile",
relative: backyardWorldsPlanet9Scan01,
Expand All @@ -313,7 +313,7 @@ var _ = Describe("SamplerRunner", Ordered, func() {
}),

Entry(nil, &samplerTE{
runnerTE: runnerTE{
controllerTE: controllerTE{
given: "profile without no-files in args",
should: "sample(first) with glob filter, using no-files from config",
relative: backyardWorldsPlanet9Scan01,
Expand All @@ -327,7 +327,7 @@ var _ = Describe("SamplerRunner", Ordered, func() {
}),

XEntry(nil, &samplerTE{
runnerTE: runnerTE{
controllerTE: controllerTE{
given: "profile",
should: "sample with regex filter using the defined profile",
relative: backyardWorldsPlanet9Scan01,
Expand All @@ -345,7 +345,7 @@ var _ = Describe("SamplerRunner", Ordered, func() {
// ===

Entry(nil, &samplerTE{
runnerTE: runnerTE{
controllerTE: controllerTE{
given: "scheme",
should: "sample all profiles in the scheme",
relative: backyardWorldsPlanet9Scan01,
Expand Down
44 changes: 22 additions & 22 deletions src/app/proxy/runner-base.go → src/app/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
// scheme: adhoc and profile from a scheme
//

type baseRunner struct { // rename to be a controller instead of a runner
shared *SharedRunnerInfo
type controller struct {
shared *SharedControllerInfo
}

func (r *baseRunner) profileSequence(
func (c *controller) profileSequence(
name, itemPath string,
) Sequence {
changed := r.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
cl := r.composeProfileCL(name, changed)
changed := c.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
cl := c.composeProfileCL(name, changed)
step := &magickStep{
shared: r.shared,
shared: c.shared,
thirdPartyCL: cl,
sourcePath: itemPath,
profile: name,
Expand All @@ -34,17 +34,17 @@ func (r *baseRunner) profileSequence(
return Sequence{step}
}

func (r *baseRunner) schemeSequence(
func (c *controller) schemeSequence(
name, itemPath string,
) Sequence {
changed := r.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
schemeCfg, _ := r.shared.sampler.Scheme(name) // scheme already validated
changed := c.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
schemeCfg, _ := c.shared.sampler.Scheme(name) // scheme already validated
sequence := make(Sequence, 0, len(schemeCfg.Profiles))

for _, current := range schemeCfg.Profiles {
cl := r.composeProfileCL(current, changed)
cl := c.composeProfileCL(current, changed)
step := &magickStep{
shared: r.shared,
shared: c.shared,
thirdPartyCL: cl,
sourcePath: itemPath,
scheme: name,
Expand All @@ -59,12 +59,12 @@ func (r *baseRunner) schemeSequence(
return sequence
}

func (r *baseRunner) adhocSequence(
func (c *controller) adhocSequence(
itemPath string,
) Sequence {
changed := r.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
changed := c.shared.Inputs.ParamSet.Native.ThirdPartySet.LongChangedCL
step := &magickStep{
shared: r.shared,
shared: c.shared,
thirdPartyCL: changed,
sourcePath: itemPath,
// outputPath: ,
Expand All @@ -74,28 +74,28 @@ func (r *baseRunner) adhocSequence(
return Sequence{step}
}

func (r *baseRunner) composeProfileCL(
func (c *controller) composeProfileCL(
profileName string,
secondary clif.ThirdPartyCommandLine,
) clif.ThirdPartyCommandLine {
primary, _ := r.shared.profiles.Profile(profileName) // profile already validated
primary, _ := c.shared.profiles.Profile(profileName) // profile already validated

return cobrass.Evaluate(
primary,
r.shared.Inputs.ParamSet.Native.ThirdPartySet.KnownBy,
c.shared.Inputs.ParamSet.Native.ThirdPartySet.KnownBy,
secondary,
)
}

func (r *baseRunner) Run(item *nav.TraverseItem, sequence Sequence) error {
func (c *controller) Run(item *nav.TraverseItem, sequence Sequence) error {
var (
zero Step
resultErr error
)

iterator := collections.ForwardRunIt[Step, error](sequence, zero)
each := func(s Step) error {
return s.Run(r.shared)
return s.Run(c.shared)
}
while := func(_ Step, err error) bool {
if resultErr == nil {
Expand All @@ -115,14 +115,14 @@ func (r *baseRunner) Run(item *nav.TraverseItem, sequence Sequence) error {
// Perhaps we have an error policy including one that implements
// a retry.
//
if err := r.shared.fileManager.Setup(item); err != nil {
if err := c.shared.fileManager.Setup(item); err != nil {
return err
}

iterator.RunAll(each, while)

return r.shared.fileManager.Tidy()
return c.shared.fileManager.Tidy()
}

func (r *baseRunner) Reset() {
func (c *controller) Reset() {
}
16 changes: 8 additions & 8 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func (e *ShrinkEntry) PrincipalOptionsFn(o *nav.TraverseOptions) {
fmt.Sprintf("'%v'", item.Path),
}

runner := e.Registry.Get()
defer e.Registry.Put(runner)
controller := e.Registry.Get()
defer e.Registry.Put(controller)

return runner.OnNewShrinkItem(item, positional)
return controller.OnNewShrinkItem(item, positional)
},
}
}
Expand Down Expand Up @@ -135,8 +135,8 @@ func (e *ShrinkEntry) ConfigureOptions(o *nav.TraverseOptions) {
e.EntryBase.ConfigureOptions(o)

finder := e.createFinder()
e.Registry = NewRunnerRegistry(&SharedRunnerInfo{
Type: RunnerTypeSamplerEn, // TODO: to come from an arg !!!
e.Registry = NewControllerRegistry(&SharedControllerInfo{
Type: ControllerTypeSamplerEn, // TODO: to come from an arg !!!
Options: e.Options,
program: e.Program,
profiles: e.ProfilesCFG,
Expand Down Expand Up @@ -194,10 +194,10 @@ func (e *ShrinkEntry) resumeFn(item *nav.TraverseItem) error {
fmt.Sprintf("'%v'", item.Path),
}

runner := e.Registry.Get()
defer e.Registry.Put(runner)
controller := e.Registry.Get()
defer e.Registry.Put(controller)

return runner.OnNewShrinkItem(item, positional)
return controller.OnNewShrinkItem(item, positional)
}

func (e *ShrinkEntry) run(_ configuration.ViperConfig) error {
Expand Down
8 changes: 4 additions & 4 deletions src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type EntryBase struct {
Program Executor
Config configuration.ViperConfig
Options *nav.TraverseOptions
Registry *RunnerRegistry
Registry *ControllerRegistry
ProfilesCFG ProfilesConfig
SamplerCFG SamplerConfig
Vfs storage.VirtualFS
Expand Down Expand Up @@ -132,12 +132,12 @@ func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) {
}
}

// TODO: get the runner type properly, instead of hard coding to Sampler
// TODO: get the controller type properly, instead of hard coding to Sampler
// This should not be here; move to root
//
if e.Registry == nil {
e.Registry = NewRunnerRegistry(&SharedRunnerInfo{
Type: RunnerTypeSamplerEn, // TODO: to come from an arg !!!
e.Registry = NewControllerRegistry(&SharedControllerInfo{
Type: ControllerTypeSamplerEn, // TODO: to come from an arg !!!
Options: e.Options,
program: e.Program,
profiles: e.ProfilesCFG,
Expand Down
8 changes: 4 additions & 4 deletions src/app/proxy/execution-step.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (

// Step
type Step interface {
Run(*SharedRunnerInfo) error
Run(*SharedControllerInfo) error
}

// Sequence
type Sequence []Step

// magickStep knows how to combine parameters together so that the program
// can be invoked correctly; but it does not know how to compose the input
// and output file names; this is the responsibility of the runner, which uses
// and output file names; this is the responsibility of the controller, which uses
// the path-finder to accomplish that task.
type magickStep struct {
shared *SharedRunnerInfo
shared *SharedControllerInfo
thirdPartyCL clif.ThirdPartyCommandLine
scheme string
profile string
Expand All @@ -27,7 +27,7 @@ type magickStep struct {
}

// Run
func (s *magickStep) Run(*SharedRunnerInfo) error {
func (s *magickStep) Run(*SharedControllerInfo) error {
positional := []string{s.sourcePath}

return s.shared.program.Execute(clif.Expand(positional, s.thirdPartyCL, s.outputPath)...)
Expand Down
4 changes: 2 additions & 2 deletions src/app/proxy/path-finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (tc pfTemplatesCollection) evaluate(
// EJECT: replicate the source directory struct but eject elsewhere
// INLINE: create the file at the same location as the original but rename as required

// The runner is aware of the output strategy moving files accordingly,
// The controller is aware of the output strategy moving files accordingly,
// using the path-finder to create the paths and the file-manager to interact
// with the file system, using a vfs.

Expand Down Expand Up @@ -114,7 +114,7 @@ so we have 3 parameters:
* --output <path> --trash <path> [ous=eject; des=eject]
*/

// PathFinder provides the common paths required, but its the runners that know
// PathFinder provides the common paths required, but its the controller that know
// the specific paths based around this common framework

type strategies struct {
Expand Down
Loading

0 comments on commit a195177

Please sign in to comment.