From a4650f6900662bff228119b998de749f3467321c Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sat, 15 Jan 2022 14:37:18 +0100 Subject: [PATCH] Add step options --- config/example.json | 14 ++++++-- pipelines/executable_step.go | 8 +++++ pipelines/executable_step_test.go | 55 +++++++++++++++++++++++++++++++ pipelines/pipeline.go | 2 +- pipelines/step.go | 28 +++++++++------- pipelines/step_test.go | 8 +++-- 6 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 pipelines/executable_step_test.go diff --git a/config/example.json b/config/example.json index a8720a7..072d863 100644 --- a/config/example.json +++ b/config/example.json @@ -6,11 +6,19 @@ "steps": [ { "name": "resize image", - "type": 0 + "type": 0, + "options": { + "width": 1280, + "height": 720, + "upscale": false + } }, { "name": "compress image", - "type": 1 + "type": 1, + "options": { + "quality": 80 + } } ] -} \ No newline at end of file +} diff --git a/pipelines/executable_step.go b/pipelines/executable_step.go index 1ef14b7..371f0b9 100644 --- a/pipelines/executable_step.go +++ b/pipelines/executable_step.go @@ -8,6 +8,11 @@ type IExecutableStep interface { type ResizeImageStep struct { Step + Options struct { + Width int `json:"width"` + Height int `json:"height"` + Upscale bool `json:"upscale"` + } `json:"options"` } func (s ResizeImageStep) Execute() { @@ -18,6 +23,9 @@ func (s ResizeImageStep) Execute() { type CompressImageStep struct { Step + Options struct { + Quality int `json:"quality"` + } `json:"options"` } func (s CompressImageStep) Execute() { diff --git a/pipelines/executable_step_test.go b/pipelines/executable_step_test.go new file mode 100644 index 0000000..f7cddf9 --- /dev/null +++ b/pipelines/executable_step_test.go @@ -0,0 +1,55 @@ +package pipelines + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestDeserializeOptions(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0, + "options": { + "width": 1280, + "height": 720, + "upscale": false + } + } + ] + }` + + t.Run("Image pipeline deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + + _, err := values[0].GetSteps()[0].Translate() + + assert.Equal(t, nil, err) + }) +} + +func TestDeserializeMissingOptions(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + } + ] + }` + + t.Run("Image pipeline deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + + _, err := values[0].GetSteps()[0].Translate() + + assert.EqualError(t, err, "unexpected end of JSON input") + }) +} diff --git a/pipelines/pipeline.go b/pipelines/pipeline.go index 76d6e10..2e243e3 100644 --- a/pipelines/pipeline.go +++ b/pipelines/pipeline.go @@ -30,7 +30,7 @@ type Pipeline struct { Slug string `json:"slug" faker:"word"` Type PipelineType `json:"type" faker:"-"` RemoveMetadata bool `json:"remove_metadata" faker:"-"` - Steps []Step `json:"steps"` + Steps []Step `json:"steps" faker:"-"` } func (p Pipeline) GetName() string { diff --git a/pipelines/step.go b/pipelines/step.go index 407b514..99a95ad 100644 --- a/pipelines/step.go +++ b/pipelines/step.go @@ -1,6 +1,9 @@ package pipelines -import "errors" +import ( + "encoding/json" + "errors" +) type StepType int @@ -10,24 +13,25 @@ const ( ) type Step struct { - Name string `json:"name" faker:"name"` - Type StepType `json:"type" faker:"-"` + Name string `json:"name" faker:"name"` + Type StepType `json:"type" faker:"-"` + Options json.RawMessage `json:"options"` } 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") + step := ResizeImageStep{} + if err := json.Unmarshal(s.Options, &step.Options); err != nil { + return nil, err + } + + return step, nil + //case TypeCompressImageStep: + // step = CompressImageStep{s} } - return step, nil + return nil, errors.New("invalid type") } func (s Step) GetType() StepType { diff --git a/pipelines/step_test.go b/pipelines/step_test.go index a61eca2..d1c1c4e 100644 --- a/pipelines/step_test.go +++ b/pipelines/step_test.go @@ -13,7 +13,8 @@ func TestTranslateStep(t *testing.T) { "steps": [ { "name": "resize image", - "type": 0 + "type": 0, + "options": {} } ] }` @@ -28,7 +29,7 @@ func TestTranslateStep(t *testing.T) { assert.Equal(t, TypeResizeImageStep, steps[0].GetType()) translated, err := steps[0].Translate() - assert.Equal(t, err, nil) + assert.Equal(t, nil, err) assert.IsType(t, ResizeImageStep{}, translated) }) } @@ -41,7 +42,8 @@ func TestInvalidStepType(t *testing.T) { "steps": [ { "name": "resize image", - "type": 99999 + "type": 99999, + "options": {} } ] }`