Skip to content

Commit

Permalink
provide dedicated decision factories
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Nov 22, 2024
1 parent 3e4f76f commit 761c5aa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
41 changes: 38 additions & 3 deletions api/ocm/plugin/ppi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,17 @@ func ComponentVersionQuestionFunc(f func(p Plugin, question *ComponentVersionQue
}
}

func ComponentReferenceQuestionFunc(f func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)) QuestionResultFunc {
type ComponentReferenceQuestionFunc = func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)

func ForComponentReferenceQuestion(f func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)) QuestionResultFunc {
return func(p Plugin, question QuestionArguments) (bool, error) {
return f(p, question.(*ComponentReferenceQuestionArguments))
}
}

func ArtifactQuestionFunc(f func(p Plugin, question *ArtifactQuestionArguments) (bool, error)) QuestionResultFunc {
type ArtifactQuestionFunc = func(p Plugin, question *ArtifactQuestionArguments) (bool, error)

func ForArtifactQuestion(f ArtifactQuestionFunc) QuestionResultFunc {
return func(p Plugin, question QuestionArguments) (bool, error) {
return f(p, question.(*ArtifactQuestionArguments))
}
Expand All @@ -212,7 +216,7 @@ type defaultDecisionHandler struct {

// NewDecisionHandler provides a default decision handler based on its standard
// fields and a handler function.
func NewDecisionHandler(q, desc string, h func(p Plugin, question QuestionArguments) (bool, error), labels ...string) DecisionHandler {
func NewDecisionHandler(q, desc string, h QuestionResultFunc, labels ...string) DecisionHandler {
return &defaultDecisionHandler{
DecisionHandlerBase: NewDecisionHandlerBase(q, desc, labels...),
handler: h,
Expand All @@ -223,6 +227,37 @@ func (d *defaultDecisionHandler) DecideOn(p Plugin, question QuestionArguments)
return d.handler(p, question)
}

////////////////////////////////////////////////////////////////////////////////
// specialized handler creation

func NewTransferResourceDecision(desc string, h ArtifactQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_TRANSFER_RESOURCE, desc, ForArtifactQuestion(h))
}

func NewTransferSourceDecision(desc string, h ArtifactQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_TRANSFER_SOURCE, desc, ForArtifactQuestion(h))
}

func NewEnforceTransportDesision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_ENFORCE_TRANSPORT, desc, ForComponentReferenceQuestion(h))
}

func NewTransferversionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_TRANSFER_VERSION, desc, ForComponentReferenceQuestion(h))
}

func NewOverwriteVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_OVERWRITE_VERSION, desc, ForComponentReferenceQuestion(h))
}

func NewUpdateVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_UPDATE_VERSION, desc, ForComponentReferenceQuestion(h))
}

func NewOUpdateVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
return NewDecisionHandler(Q_UPDATE_VERSION, desc, ForComponentReferenceQuestion(h))
}

////////////////////////////////////////////////////////////////////////////////

// Config is a generic structured config stored in a string map.
Expand Down
65 changes: 27 additions & 38 deletions cmds/transferplugin/transferhandlers/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package transferhandlers
import (
"slices"

"github.com/mandelsoft/goutils/general"
"github.com/mandelsoft/goutils/optionutils"
"ocm.software/ocm/api/ocm/plugin/ppi"
"ocm.software/ocm/cmds/transferplugin/config"
Expand All @@ -15,51 +14,41 @@ const (

func New() ppi.TransferHandler {
h := ppi.NewTransferHandler(NAME, "enable value transport for dedicated external repositories")
h.RegisterDecision(NewDecision(ppi.Q_TRANSFER_RESOURCE, func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.ResourcesByValue) },
`value transport only for dedicated access types and service hosts`))
h.RegisterDecision(NewDecision(ppi.Q_TRANSFER_SOURCE, func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.SourcesByValue) },
`value transport only for dedicated access types and service hosts`))
return h
}

type DecisionHandler struct {
ppi.DecisionHandlerBase
optfunc func(opts *ppi.TransferOptions) bool
}
h.RegisterDecision(ppi.NewTransferResourceDecision(`value transport only for dedicated access types and service hosts`,
ForOptions(func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.ResourcesByValue) })))

var _ ppi.DecisionHandler = (*DecisionHandler)(nil)

func NewDecision(typ string, optfunc func(opts *ppi.TransferOptions) bool, desc ...string) ppi.DecisionHandler {
return &DecisionHandler{
DecisionHandlerBase: ppi.NewDecisionHandlerBase(typ, general.Optional(desc...)),
optfunc: optfunc,
}
h.RegisterDecision(ppi.NewTransferSourceDecision(`value transport only for dedicated access types and service hosts`,
ForOptions(func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.SourcesByValue) })))
return h
}

func (d DecisionHandler) DecideOn(p ppi.Plugin, question ppi.QuestionArguments) (bool, error) {
q := question.(*ppi.ArtifactQuestionArguments)
type OptionFunc func(opts *ppi.TransferOptions) bool

var cfg *config.Config
func ForOptions(f OptionFunc) ppi.ArtifactQuestionFunc {
return func(p ppi.Plugin, question *ppi.ArtifactQuestionArguments) (bool, error) {
var cfg *config.Config

if q.Options.Special == nil {
c, err := p.GetConfig()
if c == nil || err != nil {
return false, err
}
cfg = c.(*config.Config)
} else {
c, err := config.GetConfig(*q.Options.Special)
if err != nil {
return false, err
}
if c != nil {
if question.Options.Special == nil {
c, err := p.GetConfig()
if c == nil || err != nil {
return false, err
}
cfg = c.(*config.Config)
} else {
c, err := config.GetConfig(*question.Options.Special)
if err != nil {
return false, err
}
if c != nil {
cfg = c.(*config.Config)
}
}
}

if list := cfg.TransferRepositories.Types[q.Artifact.AccessInfo.Kind]; list != nil {
host := q.Artifact.AccessInfo.Host
return slices.Contains(list, host), nil
if list := cfg.TransferRepositories.Types[question.Artifact.AccessInfo.Kind]; list != nil {
host := question.Artifact.AccessInfo.Host
return slices.Contains(list, host), nil
}
return f(&question.Options), nil
}
return d.optfunc(&q.Options), nil
}

0 comments on commit 761c5aa

Please sign in to comment.