Merge pull request #106 from laszlocph/yaml-v3

Migrating to yaml.v3
This commit is contained in:
Laszlo Fogas 2019-11-15 10:24:00 +01:00 committed by GitHub
commit e2801e8273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 92 additions and 80 deletions

View file

@ -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

View file

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

View file

@ -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 {
@ -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

View file

@ -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 (
@ -85,7 +87,7 @@ 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
@ -93,14 +95,20 @@ func (c *Constraint) UnmarshalYAML(unmarshal func(interface{}) error) error {
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
}

View file

@ -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) {

View file

@ -4,7 +4,7 @@ import (
"fmt"
libcompose "github.com/docker/libcompose/yaml"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)
type (
@ -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 {
for i, n := range value.Content {
if i%2 == 1 {
container := Container{}
out, _ := yaml.Marshal(s.Value)
if err := yaml.Unmarshal(out, &container); err != nil {
err := n.Decode(&container)
if err != nil {
return err
}
if container.Name == "" {
container.Name = fmt.Sprintf("%v", s.Key)
container.Name = fmt.Sprintf("%v", value.Content[i-1].Value)
}
c.Containers = append(c.Containers, &container)
}
}
return nil
}

View file

@ -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(`
@ -54,10 +54,6 @@ volumes:
- /var/lib/mysql
- /opt/data:/var/lib/mysql
- /etc/configs:/etc/configs/:ro
ulimits:
nofile:
soft: 20000
hard: 40000
tmpfs:
- /var/lib/test
when:
@ -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"},
@ -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)
}

View file

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

View file

@ -3,7 +3,7 @@ package yaml
import (
"fmt"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)
type (
@ -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
}
func (n *Networks) UnmarshalYAML(value *yaml.Node) error {
networks := map[string]Network{}
err := value.Decode(&networks)
for _, s := range slice {
nn := Network{}
out, _ := yaml.Marshal(s.Value)
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"

View file

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

View file

@ -1,5 +1,7 @@
package yaml
import "gopkg.in/yaml.v3"
type (
// Secrets defines a collection of secrets.
Secrets struct {
@ -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{
@ -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)
}

View file

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

View file

@ -1,6 +1,10 @@
package types
import "strconv"
import (
"strconv"
"gopkg.in/yaml.v3"
)
// BoolTrue is a custom Yaml boolean type that defaults to true.
type BoolTrue struct {
@ -8,16 +12,16 @@ type BoolTrue struct {
}
// 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
}

View file

@ -4,7 +4,7 @@ import (
"testing"
"github.com/franela/goblin"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)
func TestBoolTrue(t *testing.T) {
@ -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
out := BoolTrue{}
err := yaml.Unmarshal(in, &out)
g.Assert(err != nil).IsTrue("expects error")

View file

@ -3,7 +3,7 @@ package yaml
import (
"fmt"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)
type (
@ -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"

View file

@ -5,7 +5,7 @@ import (
"testing"
"github.com/kr/pretty"
"github.com/laszlocph/yaml"
"gopkg.in/yaml.v3"
)
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/kr/pretty v0.0.0-20160708215748-737b74a46c4b
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/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect
github.com/mattn/go-sqlite3 v0.0.0-20170901084005-05548ff55570
@ -59,4 +58,5 @@ require (
google.golang.org/grpc v0.0.0-20170626232044-9cb02b885b41
gopkg.in/go-playground/assert.v1 v1.2.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/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/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/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
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/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
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=