Browse Source

Add step options

feature/add-executable-steps
Roman Zipp 2 years ago
parent
commit
a4650f6900
  1. 14
      config/example.json
  2. 8
      pipelines/executable_step.go
  3. 55
      pipelines/executable_step_test.go
  4. 2
      pipelines/pipeline.go
  5. 28
      pipelines/step.go
  6. 8
      pipelines/step_test.go

14
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
}
}
]
}
}

8
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() {

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

2
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 {

28
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 {

8
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": {}
}
]
}`

Loading…
Cancel
Save