Skip to content

Commit

Permalink
Update to gopkg.in/yaml.v3 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Jan 25, 2025
1 parent fe714e4 commit 65ed6a9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
28 changes: 23 additions & 5 deletions forbidigo/patterns.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package forbidigo

import (
"bytes"
"fmt"
"io"
"regexp"
"regexp/syntax"
"strings"

"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

// pattern matches code that is not supposed to be used.
Expand All @@ -33,15 +35,15 @@ type pattern struct {
// patterns).
type yamlPattern pattern

func (p *yamlPattern) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (p *yamlPattern) UnmarshalYAML(value *yaml.Node) error {
// Try struct first. It's unlikely that a regular expression string
// is valid YAML for a struct.
var ptrn pattern
if err := unmarshal(&ptrn); err != nil {
if err := unmarshalStrict(&ptrn, value); err != nil && err != io.EOF {
errStr := err.Error()
// Didn't work, try plain string.
var ptrn string
if err := unmarshal(&ptrn); err != nil {
if err := unmarshalStrict(&ptrn, value); err != nil && err != io.EOF {
return fmt.Errorf("pattern is neither a regular expression string (%s) nor a Pattern struct (%s)", err.Error(), errStr)
}
p.Pattern = ptrn
Expand All @@ -51,6 +53,20 @@ func (p *yamlPattern) UnmarshalYAML(unmarshal func(interface{}) error) error {
return ((*pattern)(p)).validate()
}

// unmarshalStrict implements missing yaml.UnmarshalStrict in gopkg.in/yaml.v3.
// See https://github.com/go-yaml/yaml/issues/639.
// Inspired by https://github.com/ffenix113/zigbee_home/pull/68
func unmarshalStrict(to any, node *yaml.Node) error {
buf := &bytes.Buffer{}
if err := yaml.NewEncoder(buf).Encode(node); err != nil {
return err
}

decoder := yaml.NewDecoder(buf)
decoder.KnownFields(true)
return decoder.Decode(to)
}

var _ yaml.Unmarshaler = &yamlPattern{}

// parse accepts a regular expression or, if the string starts with { or contains a line break, a
Expand All @@ -61,7 +77,9 @@ func parse(ptrn string) (*pattern, error) {
if strings.HasPrefix(strings.TrimSpace(ptrn), "{") ||
strings.Contains(ptrn, "\n") {
// Embedded JSON or YAML. We can decode both with the YAML decoder.
if err := yaml.UnmarshalStrict([]byte(ptrn), pattern); err != nil {
decoder := yaml.NewDecoder(strings.NewReader(ptrn))
decoder.KnownFields(true)
if err := decoder.Decode(pattern); err != nil && err != io.EOF {
return nil, fmt.Errorf("parsing as JSON or YAML failed: %v", err)
}
} else {
Expand Down
17 changes: 9 additions & 8 deletions forbidigo/patterns_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package forbidigo

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

func TestParseValidPatterns(t *testing.T) {
Expand Down Expand Up @@ -113,9 +114,8 @@ func TestUnmarshalYAML(t *testing.T) {
expectedPattern: `fmt\.Errorf`,
},
{
name: "match import with YAML",
yaml: `p: ^fmt\.Println$
`,
name: "match import with YAML",
yaml: `p: ^fmt\.Println$ `,
expectedPattern: `^fmt\.Println$`,
},
{
Expand All @@ -124,9 +124,8 @@ func TestUnmarshalYAML(t *testing.T) {
expectedErr: "unable to compile source code pattern `fmt\\`: error parsing regexp: trailing backslash at end of expression: ``",
},
{
name: "struct: invalid regexp",
yaml: `p: fmt\
`,
name: "struct: invalid regexp",
yaml: `p: fmt\ `,
expectedErr: "unable to compile source code pattern `fmt\\`: error parsing regexp: trailing backslash at end of expression: ``",
},
{
Expand All @@ -139,7 +138,9 @@ func TestUnmarshalYAML(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
var p yamlPattern
err := yaml.UnmarshalStrict([]byte(tc.yaml), &p)
decoder := yaml.NewDecoder(bytes.NewReader([]byte(tc.yaml)))
decoder.KnownFields(true)
err := decoder.Decode(&p)
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.18

require (
github.com/google/go-cmp v0.5.6
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.0
golang.org/x/tools v0.13.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
Expand All @@ -20,6 +20,6 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 65ed6a9

Please sign in to comment.