Skip to content

Commit fd8a11d

Browse files
authored
Allow implicit ResourceListProcessorFunc (#760)
* allow explicit ResourceListProcessorFunc, add test Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * return to using switch-case, add test for lambda function Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * address copilot comment Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * expose global `make test` Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * add nil check in Run Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> --------- Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com>
1 parent 755e3f8 commit fd8a11d

4 files changed

Lines changed: 64 additions & 14 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ GO_MOD_DIRS = $(shell find . -name 'go.mod' -exec sh -c 'echo \"$$(dirname "{}")
99

1010
.PHONY: tidy
1111
tidy:
12-
@for f in $(GO_MOD_DIRS); do (cd $$f; echo "Tidying $$f"; go mod tidy) || exit 1; done
12+
@for f in $(GO_MOD_DIRS); do (cd $$f; echo "Tidying $$f"; go mod tidy) || exit 1; done
13+
14+
.PHONY: test
15+
test:
16+
make -C go test

go/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ lint: $(MODULES)
4040

4141
.PHONY: test
4242
test: $(MODULES)
43-
@for f in $(^D); do (cd $$f; echo "Testing $$f"; go test ./...) || exit 1; done
43+
@for f in $(^D); do (cd $$f; echo "Testing $$f"; go test -v ./...) || exit 1; done
4444

4545
.PHONY: vet
4646
vet: $(MODULES)

go/fn/run.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ func AsMain(input any, opts ...Option) error {
8787

8888
err := func() error {
8989
var p ResourceListProcessor
90-
switch input := input.(type) {
91-
case runnerProcessor:
92-
p = input
93-
case ResourceListProcessorFunc:
94-
p = input
90+
switch cast := input.(type) {
91+
case ResourceListProcessor:
92+
p = cast
93+
case func(*ResourceList) (bool, error):
94+
p = ResourceListProcessorFunc(cast)
9595
default:
9696
return fmt.Errorf("unknown input type %T", input)
9797
}
@@ -206,13 +206,8 @@ func readFilesAsResourceList(paths []string) (*ResourceList, error) {
206206
// Run evaluates the function. input must be a resourceList in yaml format. An
207207
// updated resourceList will be returned.
208208
func Run(p ResourceListProcessor, input []byte) ([]byte, error) {
209-
switch input := p.(type) {
210-
case runnerProcessor:
211-
p = input
212-
case ResourceListProcessorFunc:
213-
p = input
214-
default:
215-
return nil, fmt.Errorf("unknown input type %T", input)
209+
if p == nil {
210+
return nil, fmt.Errorf("nil ResourceListProcessor")
216211
}
217212
rl, err := ParseResourceList(input)
218213
if err != nil {

go/fn/run_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package fn
1616

1717
import (
1818
"fmt"
19+
"os"
1920
"testing"
2021

2122
"github.com/stretchr/testify/assert"
@@ -33,3 +34,53 @@ func TestRunEmptyInputBytes(t *testing.T) {
3334
expected := fmt.Appendf(nil, "apiVersion: %s\nkind: %s\n", kio.ResourceListAPIVersion, kio.ResourceListKind)
3435
assert.Equal(t, expected, output)
3536
}
37+
38+
type myRLP struct{}
39+
40+
func (*myRLP) Process(*ResourceList) (bool, error) {
41+
return true, nil
42+
}
43+
44+
func myRLPF(*ResourceList) (bool, error) {
45+
return true, nil
46+
}
47+
48+
type myFR struct{}
49+
50+
func (*myFR) Run(*Context, *KubeObject, KubeObjects, *Results) bool {
51+
return true
52+
}
53+
54+
var _ ResourceListProcessor = &myRLP{}
55+
var _ ResourceListProcessorFunc = myRLPF
56+
var _ Runner = &myFR{}
57+
58+
func TestAsMainTypes(t *testing.T) {
59+
oldArgs := os.Args
60+
t.Cleanup(func() { os.Args = oldArgs })
61+
os.Args = []string{"cmd"}
62+
63+
oldStdin := os.Stdin
64+
devNull, err := os.Open(os.DevNull)
65+
require.NoError(t, err)
66+
t.Cleanup(func() {
67+
os.Stdin = oldStdin
68+
_ = devNull.Close()
69+
})
70+
os.Stdin = devNull
71+
72+
testCases := map[string]any{
73+
"ResourceListProcessor": &myRLP{},
74+
"Implicit ResourceListProcessorFunc": myRLPF,
75+
"Explicit ResourceListProcessorFunc": ResourceListProcessorFunc(myRLPF),
76+
"RunnerProcessor": runnerProcessor{ctx: t.Context(), fnRunner: &myFR{}},
77+
"Anonymous": func(*ResourceList) (bool, error) { return true, nil },
78+
}
79+
80+
for name, input := range testCases {
81+
t.Run(name, func(t *testing.T) {
82+
err := AsMain(input)
83+
assert.NoError(t, err)
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)