Skip to content

Commit

Permalink
fix: add error msg to UnmarshalText to make the error clearer (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
quagmt authored Jan 4, 2025
1 parent c313c7c commit 751d5a2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bint.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (u bint) Cmp(v bint) int {
}

func errInvalidFormat(s []byte) error {
return fmt.Errorf("%w: can't parse '%s' to Decimal", ErrInvalidFormat, s)
return fmt.Errorf("%w: can't parse '%s'", ErrInvalidFormat, s)
}

func parseBint(s []byte) (bool, bint, uint8, error) {
Expand Down
4 changes: 4 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ func (d Decimal) MarshalText() ([]byte, error) {
func (d *Decimal) UnmarshalText(data []byte) error {
var err error
*d, err = parseBytes(data)
if err != nil {
return fmt.Errorf("error unmarshaling to Decimal: %w", err)
}

return err
}

Expand Down
25 changes: 15 additions & 10 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,30 @@ func TestMarshalText(t *testing.T) {

func TestUnmarshalText(t *testing.T) {
testcases := []struct {
in string
wantErr error
in string
wantErr string
wantErrType error
}{
{"", ErrEmptyString},
{" ", ErrInvalidFormat},
{"abc", ErrInvalidFormat},
{"1234567890123.1234567890123", nil},
{"1234567890123.12345678901234567899", ErrPrecOutOfRange},
{"", "error unmarshaling to Decimal: can't parse empty string", ErrEmptyString},
{" ", "error unmarshaling to Decimal: invalid format: can't parse ' '", ErrInvalidFormat},
{"abc", "error unmarshaling to Decimal: invalid format: can't parse 'abc'", ErrInvalidFormat},
{"1234567890123.12345678901234567899", "error unmarshaling to Decimal: precision out of range. Only support maximum 19 digits after the decimal point", ErrPrecOutOfRange},
{"1234567890123.1234567890123", "", nil},
}

for _, tc := range testcases {
t.Run(tc.in, func(t *testing.T) {
var d Decimal
err := d.UnmarshalText([]byte(tc.in))
require.ErrorIs(t, err, tc.wantErr)

if tc.wantErr == nil {
require.Equal(t, MustParse(tc.in), d)
if tc.wantErr != "" {
require.EqualError(t, err, tc.wantErr)
require.ErrorIs(t, err, tc.wantErrType)
return
}

require.NoError(t, err)
require.Equal(t, MustParse(tc.in), d)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var (
ErrPrecOutOfRange = fmt.Errorf("precision out of range. Only support maximum %d digits after the decimal point", defaultPrec)

// ErrEmptyString is returned when the input string is empty
ErrEmptyString = fmt.Errorf("parse empty string")
ErrEmptyString = fmt.Errorf("can't parse empty string")

// ErrMaxStrLen is returned when the input string exceeds the maximum length
// Maximum length is arbitrarily set to 200 so string length value can fit in 1 byte (for MarshalBinary).
Expand Down
62 changes: 31 additions & 31 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,31 @@ func TestParse(t *testing.T) {
{"340282366920938463463374607431768211459", "340282366920938463463374607431768211459", nil},
{"340282366920938463463374607431768211459.123", "340282366920938463463374607431768211459.123", nil},
{"+340282366920938463463374607431768211459", "340282366920938463463374607431768211459", nil},
{"340282366920938463463374607431768211459.", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.' to Decimal", ErrInvalidFormat)},
{"--340282366920938463463374607431768211459", "", fmt.Errorf("%w: can't parse '--340282366920938463463374607431768211459' to Decimal", ErrInvalidFormat)},
{".1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '.1234567890123456789012345678901234567890123456' to Decimal", ErrInvalidFormat)},
{"+.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '+.1234567890123456789012345678901234567890123456' to Decimal", ErrInvalidFormat)},
{"-.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '-.1234567890123456789012345678901234567890123456' to Decimal", ErrInvalidFormat)},
{"1.12345678901234567890123.45678901234567890123456", "", fmt.Errorf("%w: can't parse '1.12345678901234567890123.45678901234567890123456' to Decimal", ErrInvalidFormat)},
{"340282366920938463463374607431768211459.123+--", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.123+--' to Decimal", ErrInvalidFormat)},
{"340282366920938463463374607431768211459.", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.'", ErrInvalidFormat)},
{"--340282366920938463463374607431768211459", "", fmt.Errorf("%w: can't parse '--340282366920938463463374607431768211459'", ErrInvalidFormat)},
{".1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)},
{"+.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '+.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)},
{"-.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '-.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)},
{"1.12345678901234567890123.45678901234567890123456", "", fmt.Errorf("%w: can't parse '1.12345678901234567890123.45678901234567890123456'", ErrInvalidFormat)},
{"340282366920938463463374607431768211459.123+--", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.123+--'", ErrInvalidFormat)},
{"", "", ErrEmptyString},
{"1.234567890123456789012348901", "", ErrPrecOutOfRange},
{"1.123456789012345678912345678901234567890123456", "", ErrPrecOutOfRange},
{".", "", fmt.Errorf("%w: can't parse '.' to Decimal", ErrInvalidFormat)},
{"123.", "", fmt.Errorf("%w: can't parse '123.' to Decimal", ErrInvalidFormat)},
{"-123.", "", fmt.Errorf("%w: can't parse '-123.' to Decimal", ErrInvalidFormat)},
{"-.123456", "", fmt.Errorf("%w: can't parse '-.123456' to Decimal", ErrInvalidFormat)},
{"12c45.123456", "", fmt.Errorf("%w: can't parse '12c45.123456' to Decimal", ErrInvalidFormat)},
{"1245.-123456", "", fmt.Errorf("%w: can't parse '1245.-123456' to Decimal", ErrInvalidFormat)},
{"1245.123.456", "", fmt.Errorf("%w: can't parse '1245.123.456' to Decimal", ErrInvalidFormat)},
{"12345..123456", "", fmt.Errorf("%w: can't parse '12345..123456' to Decimal", ErrInvalidFormat)},
{"123456.123c456", "", fmt.Errorf("%w: can't parse '123456.123c456' to Decimal", ErrInvalidFormat)},
{"+.", "", fmt.Errorf("%w: can't parse '+.' to Decimal", ErrInvalidFormat)},
{"+", "", fmt.Errorf("%w: can't parse '+' to Decimal", ErrInvalidFormat)},
{"-", "", fmt.Errorf("%w: can't parse '-' to Decimal", ErrInvalidFormat)},
{"abc.1234567890123456789", "", fmt.Errorf("%w: can't parse 'abc.1234567890123456789' to Decimal", ErrInvalidFormat)},
{"123.1234567890123456abc", "", fmt.Errorf("%w: can't parse '123.1234567890123456abc' to Decimal", ErrInvalidFormat)},
{"12345678901234567890123456789012345679801234567890.", "", fmt.Errorf("%w: can't parse '12345678901234567890123456789012345679801234567890.' to Decimal", ErrInvalidFormat)},
{".", "", fmt.Errorf("%w: can't parse '.'", ErrInvalidFormat)},
{"123.", "", fmt.Errorf("%w: can't parse '123.'", ErrInvalidFormat)},
{"-123.", "", fmt.Errorf("%w: can't parse '-123.'", ErrInvalidFormat)},
{"-.123456", "", fmt.Errorf("%w: can't parse '-.123456'", ErrInvalidFormat)},
{"12c45.123456", "", fmt.Errorf("%w: can't parse '12c45.123456'", ErrInvalidFormat)},
{"1245.-123456", "", fmt.Errorf("%w: can't parse '1245.-123456'", ErrInvalidFormat)},
{"1245.123.456", "", fmt.Errorf("%w: can't parse '1245.123.456'", ErrInvalidFormat)},
{"12345..123456", "", fmt.Errorf("%w: can't parse '12345..123456'", ErrInvalidFormat)},
{"123456.123c456", "", fmt.Errorf("%w: can't parse '123456.123c456'", ErrInvalidFormat)},
{"+.", "", fmt.Errorf("%w: can't parse '+.'", ErrInvalidFormat)},
{"+", "", fmt.Errorf("%w: can't parse '+'", ErrInvalidFormat)},
{"-", "", fmt.Errorf("%w: can't parse '-'", ErrInvalidFormat)},
{"abc.1234567890123456789", "", fmt.Errorf("%w: can't parse 'abc.1234567890123456789'", ErrInvalidFormat)},
{"123.1234567890123456abc", "", fmt.Errorf("%w: can't parse '123.1234567890123456abc'", ErrInvalidFormat)},
{"12345678901234567890123456789012345679801234567890.", "", fmt.Errorf("%w: can't parse '12345678901234567890123456789012345679801234567890.'", ErrInvalidFormat)},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -204,15 +204,15 @@ func TestMustParse(t *testing.T) {
{"340282366920938463463374607431768211459", nil},
{"1.234567890123456789012348901", ErrPrecOutOfRange},
{"", ErrEmptyString},
{".", fmt.Errorf("%w: can't parse '.' to Decimal", ErrInvalidFormat)},
{"123.", fmt.Errorf("%w: can't parse '123.' to Decimal", ErrInvalidFormat)},
{"-123.", fmt.Errorf("%w: can't parse '-123.' to Decimal", ErrInvalidFormat)},
{"-.123456", fmt.Errorf("%w: can't parse '-.123456' to Decimal", ErrInvalidFormat)},
{"12c45.123456", fmt.Errorf("%w: can't parse '12c45.123456' to Decimal", ErrInvalidFormat)},
{"12345..123456", fmt.Errorf("%w: can't parse '12345..123456' to Decimal", ErrInvalidFormat)},
{"+.", fmt.Errorf("%w: can't parse '+.' to Decimal", ErrInvalidFormat)},
{"+", fmt.Errorf("%w: can't parse '+' to Decimal", ErrInvalidFormat)},
{"-", fmt.Errorf("%w: can't parse '-' to Decimal", ErrInvalidFormat)},
{".", fmt.Errorf("%w: can't parse '.'", ErrInvalidFormat)},
{"123.", fmt.Errorf("%w: can't parse '123.'", ErrInvalidFormat)},
{"-123.", fmt.Errorf("%w: can't parse '-123.'", ErrInvalidFormat)},
{"-.123456", fmt.Errorf("%w: can't parse '-.123456'", ErrInvalidFormat)},
{"12c45.123456", fmt.Errorf("%w: can't parse '12c45.123456'", ErrInvalidFormat)},
{"12345..123456", fmt.Errorf("%w: can't parse '12345..123456'", ErrInvalidFormat)},
{"+.", fmt.Errorf("%w: can't parse '+.'", ErrInvalidFormat)},
{"+", fmt.Errorf("%w: can't parse '+'", ErrInvalidFormat)},
{"-", fmt.Errorf("%w: can't parse '-'", ErrInvalidFormat)},
}

for _, tc := range testcases {
Expand Down
4 changes: 2 additions & 2 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func ExampleParse() {
// -1234567890123456789.1234567890123456789 <nil>
// -0.00007890123456789 <nil>
// 0 precision out of range. Only support maximum 19 digits after the decimal point
// 0 parse empty string
// 0 invalid format: can't parse '1.123.123' to Decimal
// 0 can't parse empty string
// 0 invalid format: can't parse '1.123.123'
}

func ExampleNewFromHiLo() {
Expand Down

0 comments on commit 751d5a2

Please sign in to comment.