Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating to yaml.v3 #106

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/compiler/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

json "github.com/ghodss/yaml"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

// paramsToEnv uses reflection to convert a map[string]interface to a list
Expand Down
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"

libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

type (
Expand Down
18 changes: 17 additions & 1 deletion cncd/pipeline/pipeline/frontend/yaml/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ func TestParse(t *testing.T) {
g.Assert(out.SkipClone).Equal(false)
})

// Check to make sure variable expansion works in yaml.MapSlice
g.It("Should handle simple yaml anchors", func() {
out, err := ParseString(simpleYamlAnchors)
if err != nil {
g.Fail(err)
}
g.Assert(out.Pipeline.Containers[0].Name).Equal("notify_success")
g.Assert(out.Pipeline.Containers[0].Image).Equal("plugins/slack")
})

g.It("Should unmarshal variables", func() {
out, err := ParseString(sampleVarYaml)
if err != nil {
Expand Down Expand Up @@ -108,6 +116,14 @@ runs_on:
- failure
`

var simpleYamlAnchors = `
vars:
image: &image plugins/slack
pipeline:
notify_success:
image: *image
`

var sampleVarYaml = `
_slack: &SLACK
image: plugins/slack
Expand Down
14 changes: 11 additions & 3 deletions cncd/pipeline/pipeline/frontend/yaml/constraint.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package yaml

import (
"fmt"
"path/filepath"

libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend"
"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/types"
"gopkg.in/yaml.v3"
)

type (
Expand Down Expand Up @@ -85,22 +87,28 @@ func (c *Constraint) Excludes(v string) bool {
}

// UnmarshalYAML unmarshals the constraint.
func (c *Constraint) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (c *Constraint) UnmarshalYAML(value *yaml.Node) error {
var out1 = struct {
Include libcompose.Stringorslice
Exclude libcompose.Stringorslice
}{}

var out2 libcompose.Stringorslice

unmarshal(&out1)
unmarshal(&out2)
err1 := value.Decode(&out1)
err2 := value.Decode(&out2)

c.Exclude = out1.Exclude
c.Include = append(
out1.Include,
out2...,
)

if err1 != nil && err2 != nil {
y, _ := yaml.Marshal(value)
return fmt.Errorf("Could not parse condition: %s", y)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend"

"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

func TestConstraint(t *testing.T) {
Expand Down
29 changes: 16 additions & 13 deletions cncd/pipeline/pipeline/frontend/yaml/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

type (
Expand Down Expand Up @@ -62,23 +62,26 @@ type (
)

// UnmarshalYAML implements the Unmarshaller interface.
func (c *Containers) UnmarshalYAML(unmarshal func(interface{}) error) error {
slice := yaml.MapSlice{}
if err := unmarshal(&slice); err != nil {
func (c *Containers) UnmarshalYAML(value *yaml.Node) error {
containers := map[string]Container{}
err := value.Decode(&containers)
if err != nil {
return err
}

for _, s := range slice {
container := Container{}
out, _ := yaml.Marshal(s.Value)
for i, n := range value.Content {
if i%2 == 1 {
container := Container{}
err := n.Decode(&container)
if err != nil {
return err
}

if err := yaml.Unmarshal(out, &container); err != nil {
return err
if container.Name == "" {
container.Name = fmt.Sprintf("%v", value.Content[i-1].Value)
}
c.Containers = append(c.Containers, &container)
}
if container.Name == "" {
container.Name = fmt.Sprintf("%v", s.Key)
}
c.Containers = append(c.Containers, &container)
}
return nil
}
14 changes: 3 additions & 11 deletions cncd/pipeline/pipeline/frontend/yaml/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

libcompose "github.com/docker/libcompose/yaml"
"github.com/kr/pretty"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

var containerYaml = []byte(`
Expand Down Expand Up @@ -54,10 +54,6 @@ volumes:
- /var/lib/mysql
- /opt/data:/var/lib/mysql
- /etc/configs:/etc/configs/:ro
ulimits:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropped support for ulimits, man gotta focus..

nofile:
soft: 20000
hard: 40000
tmpfs:
- /var/lib/test
when:
Expand Down Expand Up @@ -102,11 +98,6 @@ func TestUnmarshalContainer(t *testing.T) {
Privileged: true,
ShmSize: libcompose.MemStringorInt(1024),
Tmpfs: libcompose.Stringorslice{"/var/lib/test"},
Ulimits: libcompose.Ulimits{
Elements: []libcompose.Ulimit{
libcompose.NewUlimit("nofile", 20000, 40000),
},
},
Volumes: libcompose.Volumes{
Volumes: []*libcompose.Volume{
{Source: "", Destination: "/var/lib/mysql"},
Expand Down Expand Up @@ -179,7 +170,8 @@ func TestUnmarshalContainersErr(t *testing.T) {
}
for _, test := range testdata {
in := []byte(test)
err := yaml.Unmarshal(in, new(Containers))
containers := new(Containers)
err := yaml.Unmarshal(in, &containers)
if err == nil {
t.Errorf("wanted error for containers %q", test)
}
Expand Down
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package matrix
import (
"strings"

"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

const (
Expand Down
22 changes: 6 additions & 16 deletions cncd/pipeline/pipeline/frontend/yaml/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package yaml
import (
"fmt"

"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

type (
Expand All @@ -21,23 +21,13 @@ type (
)

// UnmarshalYAML implements the Unmarshaller interface.
func (n *Networks) UnmarshalYAML(unmarshal func(interface{}) error) error {
slice := yaml.MapSlice{}
err := unmarshal(&slice)
if err != nil {
return err
}

for _, s := range slice {
nn := Network{}
out, _ := yaml.Marshal(s.Value)
func (n *Networks) UnmarshalYAML(value *yaml.Node) error {
networks := map[string]Network{}
err := value.Decode(&networks)

err = yaml.Unmarshal(out, &nn)
if err != nil {
return err
}
for key, nn := range networks {
if nn.Name == "" {
nn.Name = fmt.Sprintf("%v", s.Key)
nn.Name = fmt.Sprintf("%v", key)
}
if nn.Driver == "" {
nn.Driver = "bridge"
Expand Down
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/kr/pretty"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

func TestUnmarshalNetwork(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions cncd/pipeline/pipeline/frontend/yaml/secret.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package yaml

import "gopkg.in/yaml.v3"

type (
// Secrets defines a collection of secrets.
Secrets struct {
Expand All @@ -14,9 +16,11 @@ type (
)

// UnmarshalYAML implements the Unmarshaller interface.
func (s *Secrets) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (s *Secrets) UnmarshalYAML(value *yaml.Node) error {
y, _ := yaml.Marshal(value)

var strslice []string
err := unmarshal(&strslice)
err := yaml.Unmarshal(y, &strslice)
if err == nil {
for _, str := range strslice {
s.Secrets = append(s.Secrets, &Secret{
Expand All @@ -26,5 +30,5 @@ func (s *Secrets) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
return nil
}
return unmarshal(&s.Secrets)
return yaml.Unmarshal(y, &s.Secrets)
}
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/kr/pretty"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

func TestUnmarshalSecrets(t *testing.T) {
Expand Down
20 changes: 12 additions & 8 deletions cncd/pipeline/pipeline/frontend/yaml/types/bool.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package types

import "strconv"
import (
"strconv"

"gopkg.in/yaml.v3"
)

// BoolTrue is a custom Yaml boolean type that defaults to true.
type BoolTrue struct {
value bool
}

// UnmarshalYAML implements custom Yaml unmarshaling.
func (b *BoolTrue) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (b *BoolTrue) UnmarshalYAML(value *yaml.Node) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
value.Decode(&s)

value, err := strconv.ParseBool(s)
v, err := strconv.ParseBool(s)
if err == nil {
b.value = !value
b.value = !v
}
if s != "" && err != nil {
return err
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cncd/pipeline/pipeline/frontend/yaml/types/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/franela/goblin"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

func TestBoolTrue(t *testing.T) {
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestBoolTrue(t *testing.T) {
})

g.It("should throw error when invalid", func() {
in := []byte("{ }") // string value should fail parse
in := []byte("abc") // string value should fail parse
Copy link
Member Author

@laszlocph laszlocph Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaml.v3 interprets {} as a map

out := BoolTrue{}
err := yaml.Unmarshal(in, &out)
g.Assert(err != nil).IsTrue("expects error")
Expand Down
21 changes: 8 additions & 13 deletions cncd/pipeline/pipeline/frontend/yaml/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package yaml
import (
"fmt"

"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

type (
Expand All @@ -21,23 +21,18 @@ type (
)

// UnmarshalYAML implements the Unmarshaller interface.
func (v *Volumes) UnmarshalYAML(unmarshal func(interface{}) error) error {
slice := yaml.MapSlice{}
err := unmarshal(&slice)
func (v *Volumes) UnmarshalYAML(value *yaml.Node) error {
y, _ := yaml.Marshal(value)

volumes := map[string]Volume{}
err := yaml.Unmarshal(y, &volumes)
if err != nil {
return err
}

for _, s := range slice {
vv := Volume{}
out, _ := yaml.Marshal(s.Value)

err = yaml.Unmarshal(out, &vv)
if err != nil {
return err
}
for key, vv := range volumes {
if vv.Name == "" {
vv.Name = fmt.Sprintf("%v", s.Key)
vv.Name = fmt.Sprintf("%v", key)
}
if vv.Driver == "" {
vv.Driver = "local"
Expand Down
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/frontend/yaml/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/kr/pretty"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)

func TestUnmarshalVolume(t *testing.T) {
Expand Down
Loading