Browse Source

Merge commit 'dbcf08b197b6b08352597588611e489f8188f643' into HEAD

feature/add-storage-layer
Jenkins 3 years ago
parent
commit
842c3750f6
  1. 12
      config/example.json
  2. 15
      main.go
  3. 8
      pipelines/executable_step.go
  4. 80
      pipelines/executable_step_test.go
  5. 2
      pipelines/pipeline.go
  6. 32
      pipelines/step.go
  7. 12
      pipelines/step_test.go

12
config/example.json

@ -6,11 +6,19 @@
"steps": [ "steps": [
{ {
"name": "resize image", "name": "resize image",
"type": 0
"type": 0,
"options": {
"width": 1280,
"height": 720,
"upscale": false
}
}, },
{ {
"name": "compress image", "name": "compress image",
"type": 1
"type": 1,
"options": {
"quality": 80
}
} }
] ]
} }

15
main.go

@ -18,12 +18,18 @@ type Metadata struct {
func PipelineHandler(pipeline pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { func PipelineHandler(pipeline pipelines.IPipeline, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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) { func IndexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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) { func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline) {
@ -42,5 +48,8 @@ func main() {
RegisterPipelineRoutes(r, pipes) RegisterPipelineRoutes(r, pipes)
http.ListenAndServe(":8000", r)
err := http.ListenAndServe(":8000", r)
if err != nil {
panic(err)
}
} }

8
pipelines/executable_step.go

@ -8,6 +8,11 @@ type IExecutableStep interface {
type ResizeImageStep struct { type ResizeImageStep struct {
Step Step
Options struct {
Width int `json:"width"`
Height int `json:"height"`
Upscale bool `json:"upscale"`
} `json:"options"`
} }
func (s ResizeImageStep) Execute() { func (s ResizeImageStep) Execute() {
@ -18,6 +23,9 @@ func (s ResizeImageStep) Execute() {
type CompressImageStep struct { type CompressImageStep struct {
Step Step
Options struct {
Quality int `json:"quality"`
} `json:"options"`
} }
func (s CompressImageStep) Execute() { func (s CompressImageStep) Execute() {

80
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")
})
}

2
pipelines/pipeline.go

@ -30,7 +30,7 @@ type Pipeline struct {
Slug string `json:"slug" faker:"word"` Slug string `json:"slug" faker:"word"`
Type PipelineType `json:"type" faker:"-"` Type PipelineType `json:"type" faker:"-"`
RemoveMetadata bool `json:"remove_metadata" faker:"-"` RemoveMetadata bool `json:"remove_metadata" faker:"-"`
Steps []Step `json:"steps"`
Steps []Step `json:"steps" faker:"-"`
} }
func (p Pipeline) GetName() string { func (p Pipeline) GetName() string {

32
pipelines/step.go

@ -1,6 +1,9 @@
package pipelines package pipelines
import "errors"
import (
"encoding/json"
"errors"
)
type StepType int type StepType int
@ -10,24 +13,29 @@ const (
) )
type Step struct { 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() { switch s.GetType() {
case TypeResizeImageStep: 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 { func (s Step) GetType() StepType {

12
pipelines/step_test.go

@ -13,7 +13,8 @@ func TestTranslateStep(t *testing.T) {
"steps": [ "steps": [
{ {
"name": "resize image", "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, "resize image", steps[0].GetName())
assert.Equal(t, TypeResizeImageStep, steps[0].GetType()) 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) assert.IsType(t, ResizeImageStep{}, translated)
}) })
} }
@ -41,7 +42,8 @@ func TestInvalidStepType(t *testing.T) {
"steps": [ "steps": [
{ {
"name": "resize image", "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, "resize image", steps[0].GetName())
assert.Equal(t, StepType(99999), steps[0].GetType()) 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.EqualError(t, err, "invalid type")
assert.Nil(t, translated) assert.Nil(t, translated)
}) })

Loading…
Cancel
Save