Browse Source

Return error on invalid pipeline type

feature/improve-pipeline-abstraction
Roman Zipp 3 years ago
parent
commit
74800196c0
  1. 7
      pipeline.go
  2. 12
      pipeline_test.go

7
pipeline.go

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"log" "log"
@ -65,7 +66,7 @@ type Step struct {
Type StepType Type StepType
} }
func (s Step) Translate() IExecutableStep {
func (s Step) Translate() (IExecutableStep, error) {
var step IExecutableStep var step IExecutableStep
switch s.GetType() { switch s.GetType() {
case TypeResizeImageStep: case TypeResizeImageStep:
@ -75,10 +76,10 @@ func (s Step) Translate() IExecutableStep {
} }
if step == nil { if step == nil {
panic("invalid step")
return nil, errors.New("invalid type")
} }
return step
return step, nil
} }
func (s Step) GetType() StepType { func (s Step) GetType() StepType {

12
pipeline_test.go

@ -78,6 +78,10 @@ func TestTranslateStep(t *testing.T) {
assert.Equal(t, 1, len(steps), "Output steps should contain one element") assert.Equal(t, 1, len(steps), "Output steps should contain one element")
assert.Equal(t, "resize image", steps[0].GetName()) assert.Equal(t, "resize image", steps[0].GetName())
assert.Equal(t, TypeResizeImageStep, steps[0].GetType()) assert.Equal(t, TypeResizeImageStep, steps[0].GetType())
translated, err := steps[0].Translate()
assert.Equal(t, err, nil)
assert.IsType(t, ResizeImageStep{}, translated)
}) })
} }
@ -89,7 +93,7 @@ func TestInvalidStepType(t *testing.T) {
"steps": [ "steps": [
{ {
"name": "resize image", "name": "resize image",
"type": 0
"type": 99999
} }
] ]
}` }`
@ -101,6 +105,10 @@ func TestInvalidStepType(t *testing.T) {
assert.Equal(t, 1, len(steps), "Output steps should contain one element") assert.Equal(t, 1, len(steps), "Output steps should contain one element")
assert.Equal(t, "resize image", steps[0].GetName()) assert.Equal(t, "resize image", steps[0].GetName())
assert.Equal(t, TypeResizeImageStep, steps[0].GetType())
assert.Equal(t, StepType(99999), steps[0].GetType())
translated, err := steps[0].Translate()
assert.EqualError(t, err, "invalid type")
assert.Nil(t, translated)
}) })
} }
Loading…
Cancel
Save