From 74800196c067061f3fc1ce5195efeefa58971bb0 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sat, 15 Jan 2022 13:12:39 +0100 Subject: [PATCH] Return error on invalid pipeline type --- pipeline.go | 7 ++++--- pipeline_test.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pipeline.go b/pipeline.go index 55894ae..0fd1ca3 100644 --- a/pipeline.go +++ b/pipeline.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "fmt" "io/fs" "log" @@ -65,7 +66,7 @@ type Step struct { Type StepType } -func (s Step) Translate() IExecutableStep { +func (s Step) Translate() (IExecutableStep, error) { var step IExecutableStep switch s.GetType() { case TypeResizeImageStep: @@ -75,10 +76,10 @@ func (s Step) Translate() IExecutableStep { } if step == nil { - panic("invalid step") + return nil, errors.New("invalid type") } - return step + return step, nil } func (s Step) GetType() StepType { diff --git a/pipeline_test.go b/pipeline_test.go index 83bb355..38c16e6 100644 --- a/pipeline_test.go +++ b/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, "resize image", steps[0].GetName()) 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": [ { "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, "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) }) }