From f2418104bcaced2f9a73804ffe5d3257c9eb7d67 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sat, 15 Jan 2022 13:01:04 +0100 Subject: [PATCH] Add abstract pipeline steps --- pipeline.go | 48 +++++++++++++++++--- pipeline_test.go | 116 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 124 insertions(+), 40 deletions(-) diff --git a/pipeline.go b/pipeline.go index 8bc386f..55894ae 100644 --- a/pipeline.go +++ b/pipeline.go @@ -53,30 +53,66 @@ type VideoPipeline struct { // Steps -type IStep interface { - Run() - GetName() string -} +type StepType int + +const ( + TypeResizeImageStep StepType = iota + TypeCompressImageStep +) type Step struct { Name string - Type int + Type StepType +} + +func (s Step) Translate() IExecutableStep { + var step IExecutableStep + switch s.GetType() { + case TypeResizeImageStep: + step = ResizeImageStep{s} + case TypeCompressImageStep: + step = CompressImageStep{s} + } + + if step == nil { + panic("invalid step") + } + + return step } -func (s Step) Run() {} +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/pipeline_test.go b/pipeline_test.go index ef6bbfa..83bb355 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -5,25 +5,25 @@ import ( "testing" ) -const ValidImagePayload string = `{ - "name": "example pipeline", - "type": 0, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] -}` - func TestImagePipelineDeserialization(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] + }` + t.Run("Image pipeline deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(ValidImagePayload)}) + values := DeserializePipelines([][]byte{[]byte(Payload)}) assert.Equal(t, 1, len(values), "Output should contain one element") assert.Equal(t, "example pipeline", values[0].GetName()) @@ -31,28 +31,76 @@ func TestImagePipelineDeserialization(t *testing.T) { }) } -const ValidVideoPayload string = `{ - "name": "example pipeline", - "type": 1, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] -}` - func TestVideoPipelineDeserialization(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] + }` + t.Run("Video pipelines deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(ValidVideoPayload)}) + values := DeserializePipelines([][]byte{[]byte(Payload)}) assert.Equal(t, 1, len(values), "Output should contain one element") assert.Equal(t, "example pipeline", values[0].GetName()) 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()) + }) +} + +func TestInvalidStepType(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + } + ] + }` + + 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, TypeResizeImageStep, steps[0].GetType()) + }) +}