Repository: goccy/go-yaml
Pull request #195 (2021-02-02 06:43:41)

Support a new feature to help compare error instance
Related to https://github.com/goccy/go-yaml/issues/193




Patch:
diff --git a/error.go b/error.go
new file mode 100644
index 0000000..3dfaf21
--- /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 9d6e3d2..2ba88ba 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


Test patch:
diff --git a/path_test.go b/path_test.go
index 02cd329..d1d398c 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