Skip to content

Commit 761c5aa

Browse files
committed
provide dedicated decision factories
1 parent 3e4f76f commit 761c5aa

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

api/ocm/plugin/ppi/utils.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,17 @@ func ComponentVersionQuestionFunc(f func(p Plugin, question *ComponentVersionQue
193193
}
194194
}
195195

196-
func ComponentReferenceQuestionFunc(f func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)) QuestionResultFunc {
196+
type ComponentReferenceQuestionFunc = func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)
197+
198+
func ForComponentReferenceQuestion(f func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)) QuestionResultFunc {
197199
return func(p Plugin, question QuestionArguments) (bool, error) {
198200
return f(p, question.(*ComponentReferenceQuestionArguments))
199201
}
200202
}
201203

202-
func ArtifactQuestionFunc(f func(p Plugin, question *ArtifactQuestionArguments) (bool, error)) QuestionResultFunc {
204+
type ArtifactQuestionFunc = func(p Plugin, question *ArtifactQuestionArguments) (bool, error)
205+
206+
func ForArtifactQuestion(f ArtifactQuestionFunc) QuestionResultFunc {
203207
return func(p Plugin, question QuestionArguments) (bool, error) {
204208
return f(p, question.(*ArtifactQuestionArguments))
205209
}
@@ -212,7 +216,7 @@ type defaultDecisionHandler struct {
212216

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

230+
////////////////////////////////////////////////////////////////////////////////
231+
// specialized handler creation
232+
233+
func NewTransferResourceDecision(desc string, h ArtifactQuestionFunc, labels ...string) DecisionHandler {
234+
return NewDecisionHandler(Q_TRANSFER_RESOURCE, desc, ForArtifactQuestion(h))
235+
}
236+
237+
func NewTransferSourceDecision(desc string, h ArtifactQuestionFunc, labels ...string) DecisionHandler {
238+
return NewDecisionHandler(Q_TRANSFER_SOURCE, desc, ForArtifactQuestion(h))
239+
}
240+
241+
func NewEnforceTransportDesision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
242+
return NewDecisionHandler(Q_ENFORCE_TRANSPORT, desc, ForComponentReferenceQuestion(h))
243+
}
244+
245+
func NewTransferversionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
246+
return NewDecisionHandler(Q_TRANSFER_VERSION, desc, ForComponentReferenceQuestion(h))
247+
}
248+
249+
func NewOverwriteVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
250+
return NewDecisionHandler(Q_OVERWRITE_VERSION, desc, ForComponentReferenceQuestion(h))
251+
}
252+
253+
func NewUpdateVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
254+
return NewDecisionHandler(Q_UPDATE_VERSION, desc, ForComponentReferenceQuestion(h))
255+
}
256+
257+
func NewOUpdateVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler {
258+
return NewDecisionHandler(Q_UPDATE_VERSION, desc, ForComponentReferenceQuestion(h))
259+
}
260+
226261
////////////////////////////////////////////////////////////////////////////////
227262

228263
// Config is a generic structured config stored in a string map.

cmds/transferplugin/transferhandlers/demo.go

+27-38
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package transferhandlers
33
import (
44
"slices"
55

6-
"github.com/mandelsoft/goutils/general"
76
"github.com/mandelsoft/goutils/optionutils"
87
"ocm.software/ocm/api/ocm/plugin/ppi"
98
"ocm.software/ocm/cmds/transferplugin/config"
@@ -15,51 +14,41 @@ const (
1514

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

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

30-
var _ ppi.DecisionHandler = (*DecisionHandler)(nil)
31-
32-
func NewDecision(typ string, optfunc func(opts *ppi.TransferOptions) bool, desc ...string) ppi.DecisionHandler {
33-
return &DecisionHandler{
34-
DecisionHandlerBase: ppi.NewDecisionHandlerBase(typ, general.Optional(desc...)),
35-
optfunc: optfunc,
36-
}
21+
h.RegisterDecision(ppi.NewTransferSourceDecision(`value transport only for dedicated access types and service hosts`,
22+
ForOptions(func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.SourcesByValue) })))
23+
return h
3724
}
3825

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

42-
var cfg *config.Config
28+
func ForOptions(f OptionFunc) ppi.ArtifactQuestionFunc {
29+
return func(p ppi.Plugin, question *ppi.ArtifactQuestionArguments) (bool, error) {
30+
var cfg *config.Config
4331

44-
if q.Options.Special == nil {
45-
c, err := p.GetConfig()
46-
if c == nil || err != nil {
47-
return false, err
48-
}
49-
cfg = c.(*config.Config)
50-
} else {
51-
c, err := config.GetConfig(*q.Options.Special)
52-
if err != nil {
53-
return false, err
54-
}
55-
if c != nil {
32+
if question.Options.Special == nil {
33+
c, err := p.GetConfig()
34+
if c == nil || err != nil {
35+
return false, err
36+
}
5637
cfg = c.(*config.Config)
38+
} else {
39+
c, err := config.GetConfig(*question.Options.Special)
40+
if err != nil {
41+
return false, err
42+
}
43+
if c != nil {
44+
cfg = c.(*config.Config)
45+
}
5746
}
58-
}
5947

60-
if list := cfg.TransferRepositories.Types[q.Artifact.AccessInfo.Kind]; list != nil {
61-
host := q.Artifact.AccessInfo.Host
62-
return slices.Contains(list, host), nil
48+
if list := cfg.TransferRepositories.Types[question.Artifact.AccessInfo.Kind]; list != nil {
49+
host := question.Artifact.AccessInfo.Host
50+
return slices.Contains(list, host), nil
51+
}
52+
return f(&question.Options), nil
6353
}
64-
return d.optfunc(&q.Options), nil
6554
}

0 commit comments

Comments
 (0)