Browse Source

Merge commit '7717df32e0b46fc7a182729646f155afb1c95a18' into HEAD

feature/improve-pipeline-abstraction
Jenkins 3 years ago
committed by Fabian Vowie
parent
commit
905e24c96c
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 2
      main.go
  2. 71
      pipeline.go
  3. 32
      pipeline_test.go
  4. 15
      pipelines/example.json

2
main.go

@ -21,6 +21,8 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
}
func main() {
LoadPipelines()
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler)

71
pipeline.go

@ -0,0 +1,71 @@
package main
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
)
var Pipelines []Pipeline
type PipelineType int
type PipelineStepType int
const (
Image PipelineType = iota
Video
)
const (
Resize PipelineStepType = iota
Compress
Encode
)
type Pipeline struct {
Name string
Type PipelineType
RemoveMetadata bool
Steps []Step
}
type Step struct {
Name string
Type PipelineStepType
}
func DeserializePipelines(pipelines [][]byte) []Pipeline {
values := make([]Pipeline, len(pipelines))
for index, pipeline := range pipelines {
var deserializedObject Pipeline
json.Unmarshal(pipeline, &deserializedObject)
values[index] = deserializedObject
}
return values
}
func LoadPipelines() {
var files [][]byte
path, _ := os.Getwd()
err := filepath.Walk(path+"/pipelines", func(path string, info fs.FileInfo, err error) error {
if err == nil && info.IsDir() == false {
fmt.Println(path)
data, _ := os.ReadFile(path)
files = append(files, data)
}
return nil
})
if err != nil {
panic(err)
}
Pipelines = DeserializePipelines(files)
}

32
pipeline_test.go

@ -0,0 +1,32 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
const ValidPayload string = `{
"name": "example pipeline",
"type": 0,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}`
func TestPipelineDeserialization(t *testing.T) {
t.Run("Pipelines deserialization is successful", func(t *testing.T) {
values := DeserializePipelines([][]byte{[]byte(ValidPayload)})
assert.Equal(t, 1, len(values), "Output should contain one element")
assert.Equal(t, "example pipeline", values[0].Name)
})
}

15
pipelines/example.json

@ -0,0 +1,15 @@
{
"name": "example pipeline",
"type": 0,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}
Loading…
Cancel
Save