Browse Source

Add abstract pipeline steps

feature/improve-pipeline-abstraction
Roman Zipp 3 years ago
parent
commit
f2418104bc
  1. 48
      pipeline.go
  2. 116
      pipeline_test.go

48
pipeline.go

@ -53,30 +53,66 @@ type VideoPipeline struct {
// Steps // Steps
type IStep interface {
Run()
GetName() string
}
type StepType int
const (
TypeResizeImageStep StepType = iota
TypeCompressImageStep
)
type Step struct { type Step struct {
Name string Name string
Type int
Type StepType
}
func (s Step) Translate() IExecutableStep {
var step IExecutableStep
switch s.GetType() {
case TypeResizeImageStep:
step = ResizeImageStep{s}
case TypeCompressImageStep:
step = CompressImageStep{s}
}
if step == nil {
panic("invalid step")
}
return step
} }
func (s Step) Run() {}
func (s Step) GetType() StepType {
return s.Type
}
func (s Step) GetName() string { func (s Step) GetName() string {
return s.Name return s.Name
} }
type IExecutableStep interface {
Execute()
}
// Resize image
type ResizeImageStep struct { type ResizeImageStep struct {
Step Step
} }
func (s ResizeImageStep) Execute() {
// TODO
}
// Compress image
type CompressImageStep struct { type CompressImageStep struct {
Step Step
} }
func (s CompressImageStep) Execute() {
// TODO
}
// Deserialization // Deserialization
func DeserializePipelines(pipelines [][]byte) []IPipeline { func DeserializePipelines(pipelines [][]byte) []IPipeline {

116
pipeline_test.go

@ -5,25 +5,25 @@ import (
"testing" "testing"
) )
const ValidImagePayload string = `{
"name": "example pipeline",
"type": 0,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}`
func TestImagePipelineDeserialization(t *testing.T) { func TestImagePipelineDeserialization(t *testing.T) {
const Payload string = `{
"name": "example pipeline",
"type": 0,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}`
t.Run("Image pipeline deserialization is successful", func(t *testing.T) { t.Run("Image pipeline deserialization is successful", func(t *testing.T) {
values := DeserializePipelines([][]byte{[]byte(ValidImagePayload)})
values := DeserializePipelines([][]byte{[]byte(Payload)})
assert.Equal(t, 1, len(values), "Output should contain one element") assert.Equal(t, 1, len(values), "Output should contain one element")
assert.Equal(t, "example pipeline", values[0].GetName()) assert.Equal(t, "example pipeline", values[0].GetName())
@ -31,28 +31,76 @@ func TestImagePipelineDeserialization(t *testing.T) {
}) })
} }
const ValidVideoPayload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}`
func TestVideoPipelineDeserialization(t *testing.T) { func TestVideoPipelineDeserialization(t *testing.T) {
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}`
t.Run("Video pipelines deserialization is successful", func(t *testing.T) { t.Run("Video pipelines deserialization is successful", func(t *testing.T) {
values := DeserializePipelines([][]byte{[]byte(ValidVideoPayload)})
values := DeserializePipelines([][]byte{[]byte(Payload)})
assert.Equal(t, 1, len(values), "Output should contain one element") assert.Equal(t, 1, len(values), "Output should contain one element")
assert.Equal(t, "example pipeline", values[0].GetName()) assert.Equal(t, "example pipeline", values[0].GetName())
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())
})
}
func TestInvalidStepType(t *testing.T) {
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
}
]
}`
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, TypeResizeImageStep, steps[0].GetType())
})
}
Loading…
Cancel
Save