From f94ac71a973399045d74fdbf503ad32a81f00aff Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 2 Feb 2021 15:40:12 +0900 Subject: [PATCH] Support a new feature to help compare error instance --- error.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ path.go | 8 -------- path_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 error.go diff --git a/error.go b/error.go new file mode 100644 index 00000000..3dfaf213 --- /dev/null +++ b/error.go @@ -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) +} diff --git a/path.go b/path.go index 9d6e3d21..2ba88baf 100644 --- a/path.go +++ b/path.go @@ -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 diff --git a/path_test.go b/path_test.go index 02cd329d..d1d398c2 100644 --- a/path_test.go +++ b/path_test.go @@ -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