Skip to content

Commit

Permalink
apd: drop github.com/pkg/errors
Browse files Browse the repository at this point in the history
Fixes cockroachdb#123.

Now that the stdlib's "errors" package supports wrapping, use it to
eliminate a dependency on "github.com/pkg/errors".

Picks up the "lib/pq" test dependency. Bump `go.mod` to `go 1.17` to
allow users of the library to prune this dependency
(https://go.dev/ref/mod#graph-pruning).
  • Loading branch information
nvanbenschoten committed Sep 9, 2022
1 parent 5a335e3 commit 46c8453
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package apd

import (
"errors"
"fmt"
"strings"

"github.com/pkg/errors"
)

// Condition holds condition flags.
Expand Down Expand Up @@ -143,7 +143,7 @@ func (r Condition) String() string {
case Clamped:
s = "clamped"
default:
panic(errors.Errorf("unknown condition %d", i))
panic(fmt.Errorf("unknown condition %d", i))
}
names = append(names, s)
}
Expand Down
22 changes: 11 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package apd

import (
"errors"
"fmt"
"math"

"github.com/pkg/errors"
)

// Context maintains options for Decimal operations. It can safely be used
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *Context) setAsNaN(d *Decimal, x, y *Decimal) (Condition, error) {
} else if y != nil && y.Form == NaN {
nan = y
} else {
return 0, errors.Errorf("no NaN value found; was shouldSetAsNaN called?")
return 0, errors.New("no NaN value found; was shouldSetAsNaN called?")
}
d.Set(nan)
var res Condition
Expand Down Expand Up @@ -149,7 +149,7 @@ func (c *Context) add(d, x, y *Decimal, subtract bool) (Condition, error) {
var tmp BigInt
a, b, s, err := upscale(x, y, &tmp)
if err != nil {
return 0, errors.Wrap(err, "add")
return 0, fmt.Errorf("add: %w", err)
}
d.Negative = xn
if xn == yn {
Expand Down Expand Up @@ -379,7 +379,7 @@ func (c *Context) QuoInteger(d, x, y *Decimal) (Condition, error) {
var tmp BigInt
a, b, _, err := upscale(x, y, &tmp)
if err != nil {
return 0, errors.Wrap(err, "QuoInteger")
return 0, fmt.Errorf("QuoInteger: %w", err)
}
d.Coeff.Quo(a, b)
d.Form = Finite
Expand Down Expand Up @@ -421,7 +421,7 @@ func (c *Context) Rem(d, x, y *Decimal) (Condition, error) {
var tmp1 BigInt
a, b, s, err := upscale(x, y, &tmp1)
if err != nil {
return 0, errors.Wrap(err, "Rem")
return 0, fmt.Errorf("Rem: %w", err)
}
var tmp2 BigInt
tmp2.QuoRem(a, b, &d.Coeff)
Expand Down Expand Up @@ -858,7 +858,7 @@ func (c *Context) Log10(d, x *Decimal) (Condition, error) {
var z Decimal
_, err := nc.Ln(&z, x)
if err != nil {
return 0, errors.Wrap(err, "ln")
return 0, fmt.Errorf("ln: %w", err)
}
nc.Precision = c.Precision

Expand Down Expand Up @@ -942,7 +942,7 @@ func (c *Context) Exp(d, x *Decimal) (Condition, error) {
nc := c.WithPrecision(cp)
nc.Rounding = RoundHalfEven
if _, err := nc.Quo(&r, x, &k); err != nil {
return 0, errors.Wrap(err, "Quo")
return 0, fmt.Errorf("Quo: %w", err)
}
var ra Decimal
ra.Abs(&r)
Expand All @@ -951,7 +951,7 @@ func (c *Context) Exp(d, x *Decimal) (Condition, error) {
// Stage 3
rf, err := ra.Float64()
if err != nil {
return 0, errors.Wrap(err, "r.Float64")
return 0, fmt.Errorf("r.Float64: %w", err)
}
pf := float64(p)
nf := math.Ceil((1.435*pf - 1.182) / math.Log10(pf/rf))
Expand Down Expand Up @@ -983,11 +983,11 @@ func (c *Context) Exp(d, x *Decimal) (Condition, error) {
var tmpE BigInt
ki, err := exp10(int64(t), &tmpE)
if err != nil {
return 0, errors.Wrap(err, "ki")
return 0, fmt.Errorf("ki: %w", err)
}
ires, err := nc.integerPower(d, &sum, ki)
if err != nil {
return 0, errors.Wrap(err, "integer power")
return 0, fmt.Errorf("integer power: %w", err)
}
res |= ires
nc.Precision = c.Precision
Expand Down
21 changes: 11 additions & 10 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package apd

import (
"errors"
"fmt"
"strconv"
"strings"
"unsafe"

"database/sql/driver"
"github.com/pkg/errors"
)

// Decimal is an arbitrary-precision decimal. Its value is:
Expand Down Expand Up @@ -113,7 +114,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
// Until there are no parse errors, leave as NaN.
d.Form = NaN
if strings.HasPrefix(s, "-") || strings.HasPrefix(s, "+") {
return 0, errors.Errorf("could not parse: %s", orig)
return 0, fmt.Errorf("could not parse: %s", orig)
}
switch s {
case "infinity", "inf":
Expand All @@ -135,7 +136,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
// We ignore these digits, but must verify them.
_, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "parse payload: %s", s)
return 0, fmt.Errorf("parse payload: %s: %w", s, err)
}
}
return 0, nil
Expand All @@ -145,7 +146,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
if i := strings.IndexByte(s, 'e'); i >= 0 {
exp, err := strconv.ParseInt(s[i+1:], 10, 32)
if err != nil {
return 0, errors.Wrapf(err, "parse exponent: %s", s[i+1:])
return 0, fmt.Errorf("parse exponent: %s: %w", s[i+1:], err)
}
exps = append(exps, exp)
s = s[:i]
Expand All @@ -156,7 +157,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
s = s[:i] + s[i+1:]
}
if _, ok := d.Coeff.SetString(s, 10); !ok {
return 0, errors.Errorf("parse mantissa: %s", s)
return 0, fmt.Errorf("parse mantissa: %s", s)
}
// No parse errors, can now flag as finite.
d.Form = Finite
Expand Down Expand Up @@ -248,19 +249,19 @@ func (d *Decimal) SetFloat64(f float64) (*Decimal, error) {
// int64, an error is returned.
func (d *Decimal) Int64() (int64, error) {
if d.Form != Finite {
return 0, errors.Errorf("%s is not finite", d.String())
return 0, fmt.Errorf("%s is not finite", d.String())
}
var integ, frac Decimal
d.Modf(&integ, &frac)
if !frac.IsZero() {
return 0, errors.Errorf("%s: has fractional part", d.String())
return 0, fmt.Errorf("%s: has fractional part", d.String())
}
var ed ErrDecimal
if integ.Cmp(decimalMaxInt64) > 0 {
return 0, errors.Errorf("%s: greater than max int64", d.String())
return 0, fmt.Errorf("%s: greater than max int64", d.String())
}
if integ.Cmp(decimalMinInt64) < 0 {
return 0, errors.Errorf("%s: less than min int64", d.String())
return 0, fmt.Errorf("%s: less than min int64", d.String())
}
if err := ed.Err(); err != nil {
return 0, err
Expand Down Expand Up @@ -781,7 +782,7 @@ func (d *Decimal) Scan(src interface{}) error {
_, err := d.SetFloat64(src)
return err
default:
return errors.Errorf("could not convert %T to Decimal", src)
return fmt.Errorf("could not convert %T to Decimal", src)
}
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/cockroachdb/apd/v3

go 1.13
go 1.17

require github.com/pkg/errors v0.8.0
require github.com/lib/pq v1.10.7
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
5 changes: 2 additions & 3 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package apd

import (
"fmt"
"math"

"github.com/pkg/errors"
)

type loop struct {
Expand Down Expand Up @@ -79,7 +78,7 @@ func (l *loop) done(z *Decimal) (bool, error) {
}
l.i++
if l.i == l.maxIterations {
return false, errors.Errorf(
return false, fmt.Errorf(
"%s %s: did not converge after %d iterations; prev,last result %s,%s delta %s precision: %d",
l.name, l.arg.String(), l.maxIterations, z.String(), l.prevZ.String(), l.delta.String(), l.precision,
)
Expand Down

0 comments on commit 46c8453

Please sign in to comment.