forked from cockroachdb/apd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
165 lines (140 loc) · 4.38 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package apd
// NewErrDecimal creates a ErrDecimal with given context.
func NewErrDecimal(c *Context) *ErrDecimal {
return &ErrDecimal{
Ctx: c,
}
}
// ErrDecimal performs operations on decimals and collects errors during
// operations. If an error is already set, the operation is skipped. Designed to
// be used for many operations in a row, with a single error check at the end.
type ErrDecimal struct {
err error
Ctx *Context
// Flags are the accumulated flags from operations.
Flags Condition
}
// Err returns the first error encountered or the context's trap error
// if present.
func (e *ErrDecimal) Err() error {
if e.err != nil {
return e.err
}
if e.Ctx != nil {
_, err := e.Flags.GoError(e.Ctx.Traps)
return err
}
return nil
}
func (e *ErrDecimal) op2(d, x *Decimal, f func(a, b *Decimal) (Condition, error)) *Decimal {
if e.Err() != nil {
return d
}
res, err := f(d, x)
e.Flags |= res
e.err = err
return d
}
func (e *ErrDecimal) op3(d, x, y *Decimal, f func(a, b, c *Decimal) (Condition, error)) *Decimal {
if e.Err() != nil {
return d
}
res, err := f(d, x, y)
e.Flags |= res
e.err = err
return d
}
// Abs performs e.Ctx.Abs(d, x) and returns d.
func (e *ErrDecimal) Abs(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Abs)
}
// Add performs e.Ctx.Add(d, x, y) and returns d.
func (e *ErrDecimal) Add(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Add)
}
// Ceil performs e.Ctx.Ceil(d, x) and returns d.
func (e *ErrDecimal) Ceil(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Ceil)
}
// Exp performs e.Ctx.Exp(d, x) and returns d.
func (e *ErrDecimal) Exp(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Exp)
}
// Floor performs e.Ctx.Floor(d, x) and returns d.
func (e *ErrDecimal) Floor(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Floor)
}
// Int64 returns 0 if err is set. Otherwise returns d.Int64().
func (e *ErrDecimal) Int64(d *Decimal) int64 {
if e.Err() != nil {
return 0
}
var r int64
r, e.err = d.Int64()
return r
}
// Ln performs e.Ctx.Ln(d, x) and returns d.
func (e *ErrDecimal) Ln(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Ln)
}
// Log10 performs d.Log10(x) and returns d.
func (e *ErrDecimal) Log10(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Log10)
}
// Mul performs e.Ctx.Mul(d, x, y) and returns d.
func (e *ErrDecimal) Mul(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Mul)
}
// Neg performs e.Ctx.Neg(d, x) and returns d.
func (e *ErrDecimal) Neg(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Neg)
}
// Pow performs e.Ctx.Pow(d, x, y) and returns d.
func (e *ErrDecimal) Pow(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Pow)
}
// Quantize performs e.Ctx.Quantize(d, v, x) and returns d.
func (e *ErrDecimal) Quantize(d, v, x *Decimal) *Decimal {
return e.op3(d, v, x, e.Ctx.Quantize)
}
// Quo performs e.Ctx.Quo(d, x, y) and returns d.
func (e *ErrDecimal) Quo(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Quo)
}
// QuoInteger performs e.Ctx.QuoInteger(d, x, y) and returns d.
func (e *ErrDecimal) QuoInteger(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.QuoInteger)
}
// Rem performs e.Ctx.Rem(d, x, y) and returns d.
func (e *ErrDecimal) Rem(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Rem)
}
// Round performs e.Ctx.Round(d, x) and returns d.
func (e *ErrDecimal) Round(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Round)
}
// Sqrt performs e.Ctx.Sqrt(d, x) and returns d.
func (e *ErrDecimal) Sqrt(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.Sqrt)
}
// Sub performs e.Ctx.Sub(d, x, y) and returns d.
func (e *ErrDecimal) Sub(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Sub)
}
// ToIntegral performs e.Ctx.ToIntegral(d, x) and returns d.
func (e *ErrDecimal) ToIntegral(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.ToIntegral)
}