Browse Source

Split pipeline definition to step files

feature/restructure-pipelines
Roman Zipp 3 years ago
parent
commit
dab78ea4e9
  1. 25
      pipelines/executable_step.go
  2. 63
      pipelines/pipeline.go
  3. 56
      pipelines/pipeline_test.go
  4. 39
      pipelines/step.go
  5. 62
      pipelines/step_test.go

25
pipelines/executable_step.go

@ -0,0 +1,25 @@
package pipelines
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
}

63
pipelines/pipeline.go

@ -2,7 +2,6 @@ package pipelines
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"log" "log"
@ -58,68 +57,6 @@ type VideoPipeline struct {
Pipeline Pipeline
} }
// Steps
type StepType int
const (
TypeResizeImageStep StepType = iota
TypeCompressImageStep
)
type Step struct {
Name string `faker:"name"`
Type StepType `faker:"-"`
}
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")
}
return step, nil
}
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 // Deserialization
func DeserializePipelines(pipelines [][]byte) []IPipeline { func DeserializePipelines(pipelines [][]byte) []IPipeline {

56
pipelines/pipeline_test.go

@ -57,59 +57,3 @@ func TestVideoPipelineDeserialization(t *testing.T) {
assert.Equal(t, Video, values[0].GetType()) 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())
translated, err := steps[0].Translate()
assert.Equal(t, err, nil)
assert.IsType(t, ResizeImageStep{}, translated)
})
}
func TestInvalidStepType(t *testing.T) {
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 99999
}
]
}`
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, StepType(99999), steps[0].GetType())
translated, err := steps[0].Translate()
assert.EqualError(t, err, "invalid type")
assert.Nil(t, translated)
})
}

39
pipelines/step.go

@ -0,0 +1,39 @@
package pipelines
import "errors"
type StepType int
const (
TypeResizeImageStep StepType = iota
TypeCompressImageStep
)
type Step struct {
Name string `faker:"name"`
Type StepType `faker:"-"`
}
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")
}
return step, nil
}
func (s Step) GetType() StepType {
return s.Type
}
func (s Step) GetName() string {
return s.Name
}

62
pipelines/step_test.go

@ -0,0 +1,62 @@
package pipelines
import (
"github.com/stretchr/testify/assert"
"testing"
)
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())
translated, err := steps[0].Translate()
assert.Equal(t, err, nil)
assert.IsType(t, ResizeImageStep{}, translated)
})
}
func TestInvalidStepType(t *testing.T) {
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 99999
}
]
}`
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, StepType(99999), steps[0].GetType())
translated, err := steps[0].Translate()
assert.EqualError(t, err, "invalid type")
assert.Nil(t, translated)
})
}
Loading…
Cancel
Save