From dab78ea4e94e3deabc81c1c44cd42cd3de706d77 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sat, 15 Jan 2022 14:00:21 +0100 Subject: [PATCH] Split pipeline definition to step files --- pipelines/executable_step.go | 25 ++++++++++++++ pipelines/pipeline.go | 63 ------------------------------------ pipelines/pipeline_test.go | 56 -------------------------------- pipelines/step.go | 39 ++++++++++++++++++++++ pipelines/step_test.go | 62 +++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 119 deletions(-) create mode 100644 pipelines/executable_step.go create mode 100644 pipelines/step.go create mode 100644 pipelines/step_test.go diff --git a/pipelines/executable_step.go b/pipelines/executable_step.go new file mode 100644 index 0000000..1ef14b7 --- /dev/null +++ b/pipelines/executable_step.go @@ -0,0 +1,25 @@ +package pipelines + +type IExecutableStep interface { + Execute() +} + +// Resize image + +type ResizeImageStep struct { + Step +} + +func (s ResizeImageStep) Execute() { + // TODO +} + +// Compress image + +type CompressImageStep struct { + Step +} + +func (s CompressImageStep) Execute() { + // TODO +} diff --git a/pipelines/pipeline.go b/pipelines/pipeline.go index b67f22b..76d6e10 100644 --- a/pipelines/pipeline.go +++ b/pipelines/pipeline.go @@ -2,7 +2,6 @@ package pipelines import ( "encoding/json" - "errors" "fmt" "io/fs" "log" @@ -58,68 +57,6 @@ type VideoPipeline struct { Pipeline } -// Steps - -type StepType int - -const ( - TypeResizeImageStep StepType = iota - TypeCompressImageStep -) - -type Step struct { - Name string `faker:"name"` - Type StepType `faker:"-"` -} - -func (s Step) Translate() (IExecutableStep, error) { - var step IExecutableStep - switch s.GetType() { - case TypeResizeImageStep: - step = ResizeImageStep{s} - case TypeCompressImageStep: - step = CompressImageStep{s} - } - - if step == nil { - return nil, errors.New("invalid type") - } - - return step, nil -} - -func (s Step) GetType() StepType { - return s.Type -} - -func (s Step) GetName() string { - return s.Name -} - -type IExecutableStep interface { - Execute() -} - -// Resize image - -type ResizeImageStep struct { - Step -} - -func (s ResizeImageStep) Execute() { - // TODO -} - -// Compress image - -type CompressImageStep struct { - Step -} - -func (s CompressImageStep) Execute() { - // TODO -} - // Deserialization func DeserializePipelines(pipelines [][]byte) []IPipeline { diff --git a/pipelines/pipeline_test.go b/pipelines/pipeline_test.go index 4acf90b..abd03c4 100644 --- a/pipelines/pipeline_test.go +++ b/pipelines/pipeline_test.go @@ -57,59 +57,3 @@ func TestVideoPipelineDeserialization(t *testing.T) { assert.Equal(t, Video, values[0].GetType()) }) } - -func TestTranslateStep(t *testing.T) { - const Payload string = `{ - "name": "example pipeline", - "type": 1, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - } - ] - }` - - t.Run("step type deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(Payload)}) - pipeline := values[0] - steps := pipeline.GetSteps() - - 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) - }) -} - -func TestInvalidStepType(t *testing.T) { - const Payload string = `{ - "name": "example pipeline", - "type": 1, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 99999 - } - ] - }` - - t.Run("Video pipelines deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(Payload)}) - pipeline := values[0] - steps := pipeline.GetSteps() - - assert.Equal(t, 1, len(steps), "Output steps should contain one element") - assert.Equal(t, "resize image", steps[0].GetName()) - assert.Equal(t, StepType(99999), steps[0].GetType()) - - translated, err := steps[0].Translate() - assert.EqualError(t, err, "invalid type") - assert.Nil(t, translated) - }) -} diff --git a/pipelines/step.go b/pipelines/step.go new file mode 100644 index 0000000..eb186c4 --- /dev/null +++ b/pipelines/step.go @@ -0,0 +1,39 @@ +package pipelines + +import "errors" + +type StepType int + +const ( + TypeResizeImageStep StepType = iota + TypeCompressImageStep +) + +type Step struct { + Name string `faker:"name"` + Type StepType `faker:"-"` +} + +func (s Step) Translate() (IExecutableStep, error) { + var step IExecutableStep + switch s.GetType() { + case TypeResizeImageStep: + step = ResizeImageStep{s} + case TypeCompressImageStep: + step = CompressImageStep{s} + } + + if step == nil { + return nil, errors.New("invalid type") + } + + return step, nil +} + +func (s Step) GetType() StepType { + return s.Type +} + +func (s Step) GetName() string { + return s.Name +} diff --git a/pipelines/step_test.go b/pipelines/step_test.go new file mode 100644 index 0000000..a61eca2 --- /dev/null +++ b/pipelines/step_test.go @@ -0,0 +1,62 @@ +package pipelines + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestTranslateStep(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + } + ] + }` + + t.Run("step type deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + pipeline := values[0] + steps := pipeline.GetSteps() + + 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) + }) +} + +func TestInvalidStepType(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 99999 + } + ] + }` + + t.Run("Video pipelines deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + pipeline := values[0] + steps := pipeline.GetSteps() + + assert.Equal(t, 1, len(steps), "Output steps should contain one element") + assert.Equal(t, "resize image", steps[0].GetName()) + assert.Equal(t, StepType(99999), steps[0].GetType()) + + translated, err := steps[0].Translate() + assert.EqualError(t, err, "invalid type") + assert.Nil(t, translated) + }) +}