-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_ledger_bounds.go
87 lines (74 loc) · 1.99 KB
/
transaction_ledger_bounds.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
package history
import (
"database/sql/driver"
"fmt"
"strings"
"github.com/guregu/null"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/xdr"
)
// LedgerBounds represents the ledger bounds of a Hcnet transaction
type LedgerBounds struct {
Null bool
MaxLedger null.Int
MinLedger null.Int
}
// Scan implements the database/sql Scanner interface.
func (t *LedgerBounds) Scan(src interface{}) error {
if src == nil {
*t = LedgerBounds{Null: true}
return nil
}
var rangeText string
switch src := src.(type) {
case string:
rangeText = src
case []byte:
rangeText = string(src)
default:
return errors.Errorf("cannot scan %T", src)
}
rangeText = strings.TrimSpace(rangeText)
if len(rangeText) < 3 {
return errors.Errorf("range is invalid %s", rangeText)
}
inner := rangeText[1 : len(rangeText)-1]
parts := strings.Split(inner, ",")
if len(parts) != 2 {
return errors.Errorf("%s does not have 2 comma separated values", rangeText)
}
lower, upper := parts[0], parts[1]
if len(lower) > 0 {
if err := t.MinLedger.Scan(lower); err != nil {
return errors.Wrap(err, "cannot parse lower bound")
}
}
if len(upper) > 0 {
if err := t.MaxLedger.Scan(upper); err != nil {
return errors.Wrap(err, "cannot parse upper bound")
}
}
return nil
}
// Value implements the database/sql/driver Valuer interface.
func (t LedgerBounds) Value() (driver.Value, error) {
if t.Null {
return nil, nil
}
if !t.MaxLedger.Valid || t.MaxLedger.Int64 == 0 {
return fmt.Sprintf("[%d,)", t.MinLedger.Int64), nil
}
return fmt.Sprintf("[%d, %d)", t.MinLedger.Int64, t.MaxLedger.Int64), nil
}
func formatLedgerBounds(ledgerBounds *xdr.LedgerBounds) LedgerBounds {
if ledgerBounds == nil {
return LedgerBounds{Null: true}
}
return LedgerBounds{
MinLedger: null.IntFrom(int64(ledgerBounds.MinLedger)),
// elide max_ledger if it's 0 since that means no upper bound
MaxLedger: null.NewInt(
int64(ledgerBounds.MaxLedger),
ledgerBounds.MaxLedger > 0),
}
}