Migrating to yaml.v3

This commit is contained in:
Laszlo Fogas 2019-11-14 20:16:03 +01:00
parent ea725f8b11
commit 2889d3e3e6
18 changed files with 92 additions and 80 deletions

View file

@ -7,7 +7,7 @@ import (
"strings" "strings"
json "github.com/ghodss/yaml" 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 // paramsToEnv uses reflection to convert a map[string]interface to a list

View file

@ -6,7 +6,7 @@ import (
"os" "os"
libcompose "github.com/docker/libcompose/yaml" libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
type ( type (

View file

@ -45,7 +45,15 @@ func TestParse(t *testing.T) {
g.Assert(out.SkipClone).Equal(false) 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() { g.It("Should unmarshal variables", func() {
out, err := ParseString(sampleVarYaml) out, err := ParseString(sampleVarYaml)
if err != nil { if err != nil {
@ -108,6 +116,14 @@ runs_on:
- failure - failure
` `
var simpleYamlAnchors = `
vars:
image: &image plugins/slack
pipeline:
notify_success:
image: *image
`
var sampleVarYaml = ` var sampleVarYaml = `
_slack: &SLACK _slack: &SLACK
image: plugins/slack image: plugins/slack

View file

@ -1,11 +1,13 @@
package yaml package yaml
import ( import (
"fmt"
"path/filepath" "path/filepath"
libcompose "github.com/docker/libcompose/yaml" libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend"
"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/types" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/types"
"gopkg.in/yaml.v3"
) )
type ( type (
@ -85,7 +87,7 @@ func (c *Constraint) Excludes(v string) bool {
} }
// UnmarshalYAML unmarshals the constraint. // UnmarshalYAML unmarshals the constraint.
func (c *Constraint) UnmarshalYAML(unmarshal func(interface{}) error) error { func (c *Constraint) UnmarshalYAML(value *yaml.Node) error {
var out1 = struct { var out1 = struct {
Include libcompose.Stringorslice Include libcompose.Stringorslice
Exclude libcompose.Stringorslice Exclude libcompose.Stringorslice
@ -93,14 +95,20 @@ func (c *Constraint) UnmarshalYAML(unmarshal func(interface{}) error) error {
var out2 libcompose.Stringorslice var out2 libcompose.Stringorslice
unmarshal(&out1) err1 := value.Decode(&out1)
unmarshal(&out2) err2 := value.Decode(&out2)
c.Exclude = out1.Exclude c.Exclude = out1.Exclude
c.Include = append( c.Include = append(
out1.Include, out1.Include,
out2..., out2...,
) )
if err1 != nil && err2 != nil {
y, _ := yaml.Marshal(value)
return fmt.Errorf("Could not parse condition: %s", y)
}
return nil return nil
} }

View file

@ -5,7 +5,7 @@ import (
"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
func TestConstraint(t *testing.T) { func TestConstraint(t *testing.T) {

View file

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
libcompose "github.com/docker/libcompose/yaml" libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
type ( type (
@ -62,23 +62,26 @@ type (
) )
// UnmarshalYAML implements the Unmarshaller interface. // UnmarshalYAML implements the Unmarshaller interface.
func (c *Containers) UnmarshalYAML(unmarshal func(interface{}) error) error { func (c *Containers) UnmarshalYAML(value *yaml.Node) error {
slice := yaml.MapSlice{} containers := map[string]Container{}
if err := unmarshal(&slice); err != nil { err := value.Decode(&containers)
if err != nil {
return err return err
} }
for _, s := range slice { for i, n := range value.Content {
container := Container{} if i%2 == 1 {
out, _ := yaml.Marshal(s.Value) container := Container{}
err := n.Decode(&container)
if err != nil {
return err
}
if err := yaml.Unmarshal(out, &container); err != nil { if container.Name == "" {
return err 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 return nil
} }

View file

@ -6,7 +6,7 @@ import (
libcompose "github.com/docker/libcompose/yaml" libcompose "github.com/docker/libcompose/yaml"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
var containerYaml = []byte(` var containerYaml = []byte(`
@ -54,10 +54,6 @@ volumes:
- /var/lib/mysql - /var/lib/mysql
- /opt/data:/var/lib/mysql - /opt/data:/var/lib/mysql
- /etc/configs:/etc/configs/:ro - /etc/configs:/etc/configs/:ro
ulimits:
nofile:
soft: 20000
hard: 40000
tmpfs: tmpfs:
- /var/lib/test - /var/lib/test
when: when:
@ -102,11 +98,6 @@ func TestUnmarshalContainer(t *testing.T) {
Privileged: true, Privileged: true,
ShmSize: libcompose.MemStringorInt(1024), ShmSize: libcompose.MemStringorInt(1024),
Tmpfs: libcompose.Stringorslice{"/var/lib/test"}, Tmpfs: libcompose.Stringorslice{"/var/lib/test"},
Ulimits: libcompose.Ulimits{
Elements: []libcompose.Ulimit{
libcompose.NewUlimit("nofile", 20000, 40000),
},
},
Volumes: libcompose.Volumes{ Volumes: libcompose.Volumes{
Volumes: []*libcompose.Volume{ Volumes: []*libcompose.Volume{
{Source: "", Destination: "/var/lib/mysql"}, {Source: "", Destination: "/var/lib/mysql"},
@ -179,7 +170,8 @@ func TestUnmarshalContainersErr(t *testing.T) {
} }
for _, test := range testdata { for _, test := range testdata {
in := []byte(test) in := []byte(test)
err := yaml.Unmarshal(in, new(Containers)) containers := new(Containers)
err := yaml.Unmarshal(in, &containers)
if err == nil { if err == nil {
t.Errorf("wanted error for containers %q", test) t.Errorf("wanted error for containers %q", test)
} }

View file

@ -3,7 +3,7 @@ package matrix
import ( import (
"strings" "strings"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
const ( const (

View file

@ -3,7 +3,7 @@ package yaml
import ( import (
"fmt" "fmt"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
type ( type (
@ -21,23 +21,13 @@ type (
) )
// UnmarshalYAML implements the Unmarshaller interface. // UnmarshalYAML implements the Unmarshaller interface.
func (n *Networks) UnmarshalYAML(unmarshal func(interface{}) error) error { func (n *Networks) UnmarshalYAML(value *yaml.Node) error {
slice := yaml.MapSlice{} networks := map[string]Network{}
err := unmarshal(&slice) err := value.Decode(&networks)
if err != nil {
return err
}
for _, s := range slice { for key, nn := range networks {
nn := Network{}
out, _ := yaml.Marshal(s.Value)
err = yaml.Unmarshal(out, &nn)
if err != nil {
return err
}
if nn.Name == "" { if nn.Name == "" {
nn.Name = fmt.Sprintf("%v", s.Key) nn.Name = fmt.Sprintf("%v", key)
} }
if nn.Driver == "" { if nn.Driver == "" {
nn.Driver = "bridge" nn.Driver = "bridge"

View file

@ -5,7 +5,7 @@ import (
"testing" "testing"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
func TestUnmarshalNetwork(t *testing.T) { func TestUnmarshalNetwork(t *testing.T) {

View file

@ -1,5 +1,7 @@
package yaml package yaml
import "gopkg.in/yaml.v3"
type ( type (
// Secrets defines a collection of secrets. // Secrets defines a collection of secrets.
Secrets struct { Secrets struct {
@ -14,9 +16,11 @@ type (
) )
// UnmarshalYAML implements the Unmarshaller interface. // 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 var strslice []string
err := unmarshal(&strslice) err := yaml.Unmarshal(y, &strslice)
if err == nil { if err == nil {
for _, str := range strslice { for _, str := range strslice {
s.Secrets = append(s.Secrets, &Secret{ s.Secrets = append(s.Secrets, &Secret{
@ -26,5 +30,5 @@ func (s *Secrets) UnmarshalYAML(unmarshal func(interface{}) error) error {
} }
return nil return nil
} }
return unmarshal(&s.Secrets) return yaml.Unmarshal(y, &s.Secrets)
} }

View file

@ -5,7 +5,7 @@ import (
"testing" "testing"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
func TestUnmarshalSecrets(t *testing.T) { func TestUnmarshalSecrets(t *testing.T) {

View file

@ -1,6 +1,10 @@
package types package types
import "strconv" import (
"strconv"
"gopkg.in/yaml.v3"
)
// BoolTrue is a custom Yaml boolean type that defaults to true. // BoolTrue is a custom Yaml boolean type that defaults to true.
type BoolTrue struct { type BoolTrue struct {
@ -8,16 +12,16 @@ type BoolTrue struct {
} }
// UnmarshalYAML implements custom Yaml unmarshaling. // 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 var s string
err := unmarshal(&s) value.Decode(&s)
if err != nil {
return err
}
value, err := strconv.ParseBool(s) v, err := strconv.ParseBool(s)
if err == nil { if err == nil {
b.value = !value b.value = !v
}
if s != "" && err != nil {
return err
} }
return nil return nil
} }

View file

@ -4,7 +4,7 @@ import (
"testing" "testing"
"github.com/franela/goblin" "github.com/franela/goblin"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
func TestBoolTrue(t *testing.T) { func TestBoolTrue(t *testing.T) {
@ -44,7 +44,7 @@ func TestBoolTrue(t *testing.T) {
}) })
g.It("should throw error when invalid", func() { g.It("should throw error when invalid", func() {
in := []byte("{ }") // string value should fail parse in := []byte("abc") // string value should fail parse
out := BoolTrue{} out := BoolTrue{}
err := yaml.Unmarshal(in, &out) err := yaml.Unmarshal(in, &out)
g.Assert(err != nil).IsTrue("expects error") g.Assert(err != nil).IsTrue("expects error")

View file

@ -3,7 +3,7 @@ package yaml
import ( import (
"fmt" "fmt"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
type ( type (
@ -21,23 +21,18 @@ type (
) )
// UnmarshalYAML implements the Unmarshaller interface. // UnmarshalYAML implements the Unmarshaller interface.
func (v *Volumes) UnmarshalYAML(unmarshal func(interface{}) error) error { func (v *Volumes) UnmarshalYAML(value *yaml.Node) error {
slice := yaml.MapSlice{} y, _ := yaml.Marshal(value)
err := unmarshal(&slice)
volumes := map[string]Volume{}
err := yaml.Unmarshal(y, &volumes)
if err != nil { if err != nil {
return err return err
} }
for _, s := range slice { for key, vv := range volumes {
vv := Volume{}
out, _ := yaml.Marshal(s.Value)
err = yaml.Unmarshal(out, &vv)
if err != nil {
return err
}
if vv.Name == "" { if vv.Name == "" {
vv.Name = fmt.Sprintf("%v", s.Key) vv.Name = fmt.Sprintf("%v", key)
} }
if vv.Driver == "" { if vv.Driver == "" {
vv.Driver = "local" vv.Driver = "local"

View file

@ -5,7 +5,7 @@ import (
"testing" "testing"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/laszlocph/yaml" "gopkg.in/yaml.v3"
) )
func TestUnmarshalVolume(t *testing.T) { func TestUnmarshalVolume(t *testing.T) {

2
go.mod
View file

@ -34,7 +34,6 @@ require (
github.com/joho/godotenv v0.0.0-20150907010228-4ed13390c0ac github.com/joho/godotenv v0.0.0-20150907010228-4ed13390c0ac
github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b
github.com/kr/text v0.0.0-20160504234017-7cafcd837844 // indirect github.com/kr/text v0.0.0-20160504234017-7cafcd837844 // indirect
github.com/laszlocph/yaml v0.0.0-20191114195230-2ec4ce7a1d34
github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect
github.com/mattn/go-sqlite3 v0.0.0-20170901084005-05548ff55570 github.com/mattn/go-sqlite3 v0.0.0-20170901084005-05548ff55570
@ -59,4 +58,5 @@ require (
google.golang.org/grpc v0.0.0-20170626232044-9cb02b885b41 google.golang.org/grpc v0.0.0-20170626232044-9cb02b885b41
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v8 v8.17.1 // indirect gopkg.in/go-playground/validator.v8 v8.17.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20191107175235-0b070bb63a18
) )

4
go.sum
View file

@ -77,8 +77,6 @@ github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b h1:LJ9zj3Zit+pLjAQtA1gxl
github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b/go.mod h1:Bvhd+E3laJ0AVkG0c9rmtZcnhV0HQ3+c3YxxqTvc/gA= github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b/go.mod h1:Bvhd+E3laJ0AVkG0c9rmtZcnhV0HQ3+c3YxxqTvc/gA=
github.com/kr/text v0.0.0-20160504234017-7cafcd837844 h1:kpzneEBeC0dMewP3gr/fADv1OlblH9r1goWVwpOt3TU= github.com/kr/text v0.0.0-20160504234017-7cafcd837844 h1:kpzneEBeC0dMewP3gr/fADv1OlblH9r1goWVwpOt3TU=
github.com/kr/text v0.0.0-20160504234017-7cafcd837844/go.mod h1:sjUstKUATFIcff4qlB53Kml0wQPtJVc/3fWrmuUmcfA= github.com/kr/text v0.0.0-20160504234017-7cafcd837844/go.mod h1:sjUstKUATFIcff4qlB53Kml0wQPtJVc/3fWrmuUmcfA=
github.com/laszlocph/yaml v0.0.0-20191114195230-2ec4ce7a1d34 h1:+4tKButWtRq7Xw8EUpabOmZYAk2gtinHF585AmWu2Qk=
github.com/laszlocph/yaml v0.0.0-20191114195230-2ec4ce7a1d34/go.mod h1:E1nYupUAMCOPyW4ZX78x63SP3/nKFQ5aj8tlwzMdYuo=
github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae h1:rBqRT7VqVLePKGtyV6xDFLXeqD56CvZKEqI0XWzVTxM= github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae h1:rBqRT7VqVLePKGtyV6xDFLXeqD56CvZKEqI0XWzVTxM=
github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE= github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE=
@ -158,3 +156,5 @@ gopkg.in/go-playground/validator.v8 v8.17.1 h1:W1Q1z7rfiJiNhoBkHYqb9TJAdOqPXsyNe
gopkg.in/go-playground/validator.v8 v8.17.1/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/go-playground/validator.v8 v8.17.1/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20191107175235-0b070bb63a18 h1:VaaR1yHVgJQaTGM1DXum4OU6He6gaZXAPII85hHgSzQ=
gopkg.in/yaml.v3 v3.0.0-20191107175235-0b070bb63a18/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=