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/main.go b/main.go index 490b7ee..0b60c73 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,18 @@ type Metadata struct { func PipelineHandler(pipeline pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(pipeline) + err := json.NewEncoder(w).Encode(pipeline) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } } func IndexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(Metadata{Name, Version}) + err := json.NewEncoder(w).Encode(Metadata{Name, Version}) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } } func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline) { @@ -42,5 +48,8 @@ func main() { RegisterPipelineRoutes(r, pipes) - http.ListenAndServe(":8000", r) + err := http.ListenAndServe(":8000", r) + if err != nil { + panic(err) + } } 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..7719951 --- /dev/null +++ b/pipelines/executable_step_test.go @@ -0,0 +1,80 @@ +package pipelines + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestDeserializeOptionsResizeImage(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].GetExecutable() + + assert.Equal(t, nil, err) + }) +} + +func TestDeserializeOptionsCompressImage(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "compress image", + "type": 1, + "options": { + "quality": 80 + } + } + ] + }` + + t.Run("Image pipeline deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + + _, err := values[0].GetSteps()[0].GetExecutable() + + 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].GetExecutable() + + 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 eb186c4..aa80da3 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,29 @@ const ( ) type Step struct { - Name string `faker:"name"` - Type StepType `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 +func (s Step) GetExecutable() (IExecutableStep, error) { switch s.GetType() { case TypeResizeImageStep: - step = ResizeImageStep{s} - case TypeCompressImageStep: - step = CompressImageStep{s} - } + step := ResizeImageStep{} + if err := json.Unmarshal(s.Options, &step.Options); err != nil { + return nil, err + } + return step, nil - if step == nil { - return nil, errors.New("invalid type") + case TypeCompressImageStep: + step := CompressImageStep{} + if err := json.Unmarshal(s.Options, &step.Options); err != nil { + return nil, err + } + return step, nil } - 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..f731f5f 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": {} } ] }` @@ -27,8 +28,8 @@ func TestTranslateStep(t *testing.T) { 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) + translated, err := steps[0].GetExecutable() + 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": {} } ] }` @@ -55,7 +57,7 @@ func TestInvalidStepType(t *testing.T) { assert.Equal(t, "resize image", steps[0].GetName()) assert.Equal(t, StepType(99999), steps[0].GetType()) - translated, err := steps[0].Translate() + translated, err := steps[0].GetExecutable() assert.EqualError(t, err, "invalid type") assert.Nil(t, translated) })