Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
refactor: swap yaml libraries (#91)
Browse files Browse the repository at this point in the history
* refactor: swap yaml libraries

* refactor(compiler): update yaml package

* fix: clean up compliler tests

* fix(deps): update go mod type

* fix: remove dead code

* fix: sync package alias

* test: fix tests

Co-authored-by: David May <[email protected]>
  • Loading branch information
Neal and wass3rw3rk authored Jan 11, 2021
1 parent 3f2f8a7 commit 3a76ad2
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 688 deletions.
6 changes: 1 addition & 5 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ type Engine interface {

// Parse defines a function that converts
// an object to a yaml configuration.
Parse(interface{}) (*yaml.Build, error)

// Validate defines a function that verifies
// the yaml configuration is accurate.
Validate(*yaml.Build) error
Parse(interface{}) (*yaml.Build, []byte, error)

// Clone Compiler Interface Functions

Expand Down
26 changes: 19 additions & 7 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"time"

yml "github.com/goccy/go-yaml"

"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/types/yaml"
Expand All @@ -32,15 +34,15 @@ type ModifyRequest struct {
// Compile produces an executable pipeline from a yaml configuration.
func (c *client) Compile(v interface{}) (*pipeline.Build, error) {
// parse the object into a yaml configuration
p, err := c.Parse(v)
p, raw, err := c.Parse(v)
if err != nil {
return nil, err
}

// validate the yaml configuration
err = c.Validate(p)
err = p.Validate(raw)
if err != nil {
return nil, fmt.Errorf("invalid pipeline: %w", err)
return nil, err
}

// create map of templates for easy lookup
Expand Down Expand Up @@ -94,12 +96,17 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, error) {
if err != nil {
return nil, err
}

raw, err = yml.Marshal(p)
if err != nil {
return nil, err
}
}

// validate the yaml configuration
err = c.Validate(p)
err = p.Validate(raw)
if err != nil {
return nil, fmt.Errorf("invalid pipeline: %w", err)
return nil, err
}

// inject the environment variables into the stages
Expand Down Expand Up @@ -148,12 +155,17 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, error) {
if err != nil {
return nil, err
}

raw, err = yml.Marshal(p)
if err != nil {
return nil, err
}
}

// validate the yaml configuration
err = c.Validate(p)
err = p.Validate(raw)
if err != nil {
return nil, fmt.Errorf("invalid pipeline: %w", err)
return nil, err
}

// inject the environment variables into the steps
Expand Down
5 changes: 5 additions & 0 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
},
Stages: pipeline.StageSlice{
Expand Down Expand Up @@ -427,6 +428,7 @@ func TestNative_Compile_StepsPipeline(t *testing.T) {
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
},
Steps: pipeline.ContainerSlice{
Expand Down Expand Up @@ -616,6 +618,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
},
Stages: pipeline.StageSlice{
Expand Down Expand Up @@ -839,6 +842,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
},
Steps: pipeline.ContainerSlice{
Expand Down Expand Up @@ -1011,6 +1015,7 @@ func TestNative_Compile_InvalidType(t *testing.T) {
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
},
Steps: pipeline.ContainerSlice{
Expand Down
24 changes: 12 additions & 12 deletions compiler/native/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

types "github.com/go-vela/types/yaml"

"github.com/buildkite/yaml"
"github.com/goccy/go-yaml"
)

// Parse converts an object to a yaml configuration.
func (c *client) Parse(v interface{}) (*types.Build, error) {
func (c *client) Parse(v interface{}) (*types.Build, []byte, error) {
switch v := v.(type) {
case []byte:
return ParseBytes(v)
Expand All @@ -35,34 +35,34 @@ func (c *client) Parse(v interface{}) (*types.Build, error) {
// parse string as yaml configuration
return ParseString(v)
default:
return nil, fmt.Errorf("unable to parse yaml: unrecognized type %T", v)
return nil, nil, fmt.Errorf("unable to parse yaml: unrecognized type %T", v)
}
}

// ParseBytes converts a byte slice to a yaml configuration.
func ParseBytes(b []byte) (*types.Build, error) {
func ParseBytes(b []byte) (*types.Build, []byte, error) {
config := new(types.Build)

// unmarshal the bytes into the yaml configuration
err := yaml.Unmarshal(b, config)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal yaml: %v", err)
return nil, nil, fmt.Errorf("unable to unmarshal yaml: %v", err)
}

return config, nil
return config, b, nil
}

// ParseFile converts an os.File into a yaml configuration.
func ParseFile(f *os.File) (*types.Build, error) {
func ParseFile(f *os.File) (*types.Build, []byte, error) {
return ParseReader(f)
}

// ParsePath converts a file path into a yaml configuration.
func ParsePath(p string) (*types.Build, error) {
func ParsePath(p string) (*types.Build, []byte, error) {
// open the file for reading
f, err := os.Open(p)
if err != nil {
return nil, fmt.Errorf("unable to open yaml file %s: %v", p, err)
return nil, nil, fmt.Errorf("unable to open yaml file %s: %v", p, err)
}

defer f.Close()
Expand All @@ -71,17 +71,17 @@ func ParsePath(p string) (*types.Build, error) {
}

// ParseReader converts an io.Reader into a yaml configuration.
func ParseReader(r io.Reader) (*types.Build, error) {
func ParseReader(r io.Reader) (*types.Build, []byte, error) {
// read all the bytes from the reader
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("unable to read bytes for yaml: %v", err)
return nil, nil, fmt.Errorf("unable to read bytes for yaml: %v", err)
}

return ParseBytes(b)
}

// ParseString converts a string into a yaml configuration.
func ParseString(s string) (*types.Build, error) {
func ParseString(s string) (*types.Build, []byte, error) {
return ParseBytes([]byte(s))
}
42 changes: 21 additions & 21 deletions compiler/native/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestNative_Parse_Metadata_Bytes(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestNative_Parse_Metadata_File(t *testing.T) {

defer f.Close()

got, err := client.Parse(f)
got, _, err := client.Parse(f)
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand All @@ -78,7 +78,7 @@ func TestNative_Parse_Metadata_Invalid(t *testing.T) {
client, _ := New(cli.NewContext(nil, flag.NewFlagSet("test", 0), nil))

// run test
got, err := client.Parse(nil)
got, _, err := client.Parse(nil)

if err == nil {
t.Error("Parse should have returned err")
Expand All @@ -100,7 +100,7 @@ func TestNative_Parse_Metadata_Path(t *testing.T) {
}

// run test
got, err := client.Parse("testdata/metadata.yml")
got, _, err := client.Parse("testdata/metadata.yml")
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand All @@ -126,7 +126,7 @@ func TestNative_Parse_Metadata_Reader(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(bytes.NewReader(b))
got, _, err := client.Parse(bytes.NewReader(b))
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand All @@ -152,7 +152,7 @@ func TestNative_Parse_Metadata_String(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(string(b))
got, _, err := client.Parse(string(b))
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestNative_Parse_Parameters(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestNative_Parse_StagesPipeline(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand Down Expand Up @@ -404,7 +404,7 @@ func TestNative_Parse_StepsPipeline(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)
if err != nil {
t.Errorf("Parse returned err: %v", err)
}
Expand Down Expand Up @@ -464,7 +464,7 @@ func TestNative_Parse_Secrets(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)

if err != nil {
t.Errorf("Parse returned err: %v", err)
Expand Down Expand Up @@ -537,7 +537,7 @@ func TestNative_Parse_Stages(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)

if err != nil {
t.Errorf("Parse returned err: %v", err)
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestNative_Parse_Steps(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := client.Parse(b)
got, _, err := client.Parse(b)

if err != nil {
t.Errorf("Parse returned err: %v", err)
Expand All @@ -618,7 +618,7 @@ func TestNative_ParseBytes_Metadata(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := ParseBytes(b)
got, _, err := ParseBytes(b)

if err != nil {
t.Errorf("ParseBytes returned err: %v", err)
Expand All @@ -636,7 +636,7 @@ func TestNative_ParseBytes_Invalid(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := ParseBytes(b)
got, _, err := ParseBytes(b)

if err == nil {
t.Error("ParseBytes should have returned err")
Expand Down Expand Up @@ -664,7 +664,7 @@ func TestNative_ParseFile_Metadata(t *testing.T) {

defer f.Close()

got, err := ParseFile(f)
got, _, err := ParseFile(f)

if err != nil {
t.Errorf("ParseFile returned err: %v", err)
Expand All @@ -684,7 +684,7 @@ func TestNative_ParseFile_Invalid(t *testing.T) {

f.Close()

got, err := ParseFile(f)
got, _, err := ParseFile(f)

if err == nil {
t.Error("ParseFile should have returned err")
Expand All @@ -705,7 +705,7 @@ func TestNative_ParsePath_Metadata(t *testing.T) {
}

// run test
got, err := ParsePath("testdata/metadata.yml")
got, _, err := ParsePath("testdata/metadata.yml")

if err != nil {
t.Errorf("ParsePath returned err: %v", err)
Expand All @@ -718,7 +718,7 @@ func TestNative_ParsePath_Metadata(t *testing.T) {

func TestNative_ParsePath_Invalid(t *testing.T) {
// run test
got, err := ParsePath("testdata/foobar.yml")
got, _, err := ParsePath("testdata/foobar.yml")

if err == nil {
t.Error("ParsePath should have returned err")
Expand All @@ -744,7 +744,7 @@ func TestNative_ParseReader_Metadata(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := ParseReader(bytes.NewReader(b))
got, _, err := ParseReader(bytes.NewReader(b))

if err != nil {
t.Errorf("ParseReader returned err: %v", err)
Expand All @@ -757,7 +757,7 @@ func TestNative_ParseReader_Metadata(t *testing.T) {

func TestNative_ParseReader_Invalid(t *testing.T) {
// run test
got, err := ParseReader(FailReader{})
got, _, err := ParseReader(FailReader{})

if err == nil {
t.Error("ParseFile should have returned err")
Expand All @@ -783,7 +783,7 @@ func TestNative_ParseString_Metadata(t *testing.T) {
t.Errorf("Reading file returned err: %v", err)
}

got, err := ParseString(string(b))
got, _, err := ParseString(string(b))

if err != nil {
t.Errorf("ParseString returned err: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/substitute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"strings"

"github.com/buildkite/yaml"
"github.com/goccy/go-yaml"

"github.com/drone/envsubst"

Expand Down
3 changes: 1 addition & 2 deletions compiler/native/testdata/invalid.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---
!@#$%^&*()
``
Loading

0 comments on commit 3a76ad2

Please sign in to comment.