Skip to content

Commit

Permalink
internal/errors: moved typeError to errors package
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
braydonk committed May 2, 2022
1 parent dad2ded commit bf2da56
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
56 changes: 9 additions & 47 deletions decode.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions internal/errors/error.go
Expand Up @@ -3,6 +3,7 @@ package errors
import (
"bytes"
"fmt"
"reflect"

"github.com/goccy/go-yaml/printer"
"github.com/goccy/go-yaml/token"
Expand Down Expand Up @@ -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
}

0 comments on commit bf2da56

Please sign in to comment.