From bf2da56730636857ee05e890c598140a9e27eb87 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Mon, 2 May 2022 18:00:24 +0000 Subject: [PATCH] internal/errors: moved typeError to errors package The typeError previously located in decode.go now implements PrettyPrinter, so it's been moved to internal/errors to be colocated with the other PrettyPrinter errors. --- decode.go | 56 +++++++--------------------------------- internal/errors/error.go | 38 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/decode.go b/decode.go index 7ad5661..d519b78 100644 --- a/decode.go +++ b/decode.go @@ -18,7 +18,6 @@ import ( "github.com/goccy/go-yaml/ast" "github.com/goccy/go-yaml/internal/errors" "github.com/goccy/go-yaml/parser" - "github.com/goccy/go-yaml/printer" "github.com/goccy/go-yaml/token" "golang.org/x/xerrors" ) @@ -405,45 +404,8 @@ func errOverflow(dstType reflect.Type, num string) *overflowError { return &overflowError{dstType: dstType, srcNum: num} } -type typeError struct { - dstType reflect.Type - srcType reflect.Type - structFieldName *string - token *token.Token -} - -func (e *typeError) Error() string { - if e.structFieldName != nil { - return fmt.Sprintf("cannot unmarshal %s into Go struct field %s of type %s", e.srcType, *e.structFieldName, e.dstType) - } - return fmt.Sprintf("cannot unmarshal %s into Go value of type %s", e.srcType, e.dstType) -} - -func (e *typeError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error { - return e.FormatError(&errors.FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource}) -} - -func (e *typeError) FormatError(p xerrors.Printer) error { - var pp printer.Printer - - var colored, inclSource bool - if fep, ok := p.(*errors.FormatErrorPrinter); ok { - colored = fep.Colored - inclSource = fep.InclSource - } - - pos := fmt.Sprintf("[%d:%d] ", e.token.Position.Line, e.token.Position.Column) - msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.Error()), colored) - if inclSource { - msg += "\n" + pp.PrintErrorToken(e.token, colored) - } - p.Print(msg) - - return nil -} - -func errTypeMismatch(dstType, srcType reflect.Type, token *token.Token) *typeError { - return &typeError{dstType: dstType, srcType: srcType, token: token} +func errTypeMismatch(dstType, srcType reflect.Type, token *token.Token) *errors.TypeError { + return &errors.TypeError{DstType: dstType, SrcType: srcType, Token: token} } type unknownFieldError struct { @@ -1077,14 +1039,14 @@ func (d *Decoder) decodeStruct(ctx context.Context, dst reflect.Value, src ast.N if foundErr != nil { continue } - var te *typeError + var te *errors.TypeError if xerrors.As(err, &te) { - if te.structFieldName != nil { - fieldName := fmt.Sprintf("%s.%s", structType.Name(), *te.structFieldName) - te.structFieldName = &fieldName + if te.StructFieldName != nil { + fieldName := fmt.Sprintf("%s.%s", structType.Name(), *te.StructFieldName) + te.StructFieldName = &fieldName } else { fieldName := fmt.Sprintf("%s.%s", structType.Name(), field.Name) - te.structFieldName = &fieldName + te.StructFieldName = &fieldName } foundErr = te continue @@ -1113,10 +1075,10 @@ func (d *Decoder) decodeStruct(ctx context.Context, dst reflect.Value, src ast.N if foundErr != nil { continue } - var te *typeError + var te *errors.TypeError if xerrors.As(err, &te) { fieldName := fmt.Sprintf("%s.%s", structType.Name(), field.Name) - te.structFieldName = &fieldName + te.StructFieldName = &fieldName foundErr = te } else { foundErr = err diff --git a/internal/errors/error.go b/internal/errors/error.go index 6b22460..7f1ea9a 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -3,6 +3,7 @@ package errors import ( "bytes" "fmt" + "reflect" "github.com/goccy/go-yaml/printer" "github.com/goccy/go-yaml/token" @@ -220,3 +221,40 @@ func (e *syntaxError) Error() string { e.PrettyPrint(&Sink{&buf}, defaultColorize, defaultIncludeSource) return buf.String() } + +type TypeError struct { + DstType reflect.Type + SrcType reflect.Type + StructFieldName *string + Token *token.Token +} + +func (e *TypeError) Error() string { + if e.StructFieldName != nil { + return fmt.Sprintf("cannot unmarshal %s into Go struct field %s of type %s", e.SrcType, *e.StructFieldName, e.DstType) + } + return fmt.Sprintf("cannot unmarshal %s into Go value of type %s", e.SrcType, e.DstType) +} + +func (e *TypeError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error { + return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource}) +} + +func (e *TypeError) FormatError(p xerrors.Printer) error { + var pp printer.Printer + + var colored, inclSource bool + if fep, ok := p.(*FormatErrorPrinter); ok { + colored = fep.Colored + inclSource = fep.InclSource + } + + pos := fmt.Sprintf("[%d:%d] ", e.Token.Position.Line, e.Token.Position.Column) + msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.Error()), colored) + if inclSource { + msg += "\n" + pp.PrintErrorToken(e.Token, colored) + } + p.Print(msg) + + return nil +}