Skip to content

Commit

Permalink
Support a new feature to help compare error instance
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Feb 2, 2021
1 parent d984d1d commit f94ac71
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
48 changes: 48 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package yaml

import (
"github.com/goccy/go-yaml/ast"
"golang.org/x/xerrors"
)

var (
ErrInvalidQuery = xerrors.New("invalid query")
ErrInvalidPath = xerrors.New("invalid path instance")
ErrInvalidPathString = xerrors.New("invalid path string")
ErrNotFoundNode = xerrors.New("node not found")
)

// IsInvalidQueryError whether err is ErrInvalidQuery or not.
func IsInvalidQueryError(err error) bool {
return xerrors.Is(err, ErrInvalidQuery)
}

// IsInvalidPathError whether err is ErrInvalidPath or not.
func IsInvalidPathError(err error) bool {
return xerrors.Is(err, ErrInvalidPath)
}

// IsInvalidPathStringError whether err is ErrInvalidPathString or not.
func IsInvalidPathStringError(err error) bool {
return xerrors.Is(err, ErrInvalidPathString)
}

// IsNotFoundNodeError whether err is ErrNotFoundNode or not.
func IsNotFoundNodeError(err error) bool {
return xerrors.Is(err, ErrNotFoundNode)
}

// IsInvalidTokenTypeError whether err is ast.ErrInvalidTokenType or not.
func IsInvalidTokenTypeError(err error) bool {
return xerrors.Is(err, ast.ErrInvalidTokenType)
}

// IsInvalidAnchorNameError whether err is ast.ErrInvalidAnchorName or not.
func IsInvalidAnchorNameError(err error) bool {
return xerrors.Is(err, ast.ErrInvalidAnchorName)
}

// IsInvalidAliasNameError whether err is ast.ErrInvalidAliasName or not.
func IsInvalidAliasNameError(err error) bool {
return xerrors.Is(err, ast.ErrInvalidAliasName)
}
8 changes: 0 additions & 8 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ import (
"github.com/goccy/go-yaml/internal/errors"
"github.com/goccy/go-yaml/parser"
"github.com/goccy/go-yaml/printer"
"golang.org/x/xerrors"
)

var (
ErrInvalidQuery = xerrors.New("invalid query")
ErrInvalidPath = xerrors.New("invalid path instance")
ErrInvalidPathString = xerrors.New("invalid path string")
ErrNotFoundNode = xerrors.New("node not found")
)

// PathString create Path from string
Expand Down
45 changes: 45 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,51 @@ store:
})
}

func TestPath_Invalid(t *testing.T) {
tests := []struct {
path string
src string
}{
{
path: "$.wrong",
src: "foo: bar",
},
}
for _, test := range tests {
path, err := yaml.PathString(test.path)
if err != nil {
t.Fatal(err)
}
t.Run("path.Read", func(t *testing.T) {
file, err := parser.ParseBytes([]byte(test.src), 0)
if err != nil {
t.Fatal(err)
}
var v interface{}
err = path.Read(file, &v)
if err == nil {
t.Fatal("expected error")
}
if !yaml.IsNotFoundNodeError(err) {
t.Fatalf("unexpected error %s", err)
}
})
t.Run("path.ReadNode", func(t *testing.T) {
file, err := parser.ParseBytes([]byte(test.src), 0)
if err != nil {
t.Fatal(err)
}
_, err = path.ReadNode(file)
if err == nil {
t.Fatal("expected error")
}
if !yaml.IsNotFoundNodeError(err) {
t.Fatalf("unexpected error %s", err)
}
})
}
}

func TestPath_Merge(t *testing.T) {
tests := []struct {
path string
Expand Down

0 comments on commit f94ac71

Please sign in to comment.