Skip to content

Commit fd4f7fb

Browse files
committed
accounts: add SQL store implementation
In this commit, we add the SQLStore type which implements the accounts.Store interface. To demonstrate that it works as expected, we also plug this implementation into all the account unit tests to show that they pass against the sqlite and postgres backends. One can use `make unit pkg=accounts tags=test_db_postgres` or `make unit pkg=accounts tags=test_db_sqlite` to test locally. Note that 2 small timestamp related changes are made to the unit tests. This is to compensate for timestamp precision in postgres.
1 parent 400a129 commit fd4f7fb

File tree

6 files changed

+835
-3
lines changed

6 files changed

+835
-3
lines changed

accounts/interface.go

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package accounts
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/binary"
57
"encoding/hex"
68
"errors"
79
"fmt"
@@ -55,6 +57,31 @@ func ParseAccountID(idStr string) (*AccountID, error) {
5557
return &id, nil
5658
}
5759

60+
// ToInt64 converts an AccountID to its int64 representation.
61+
func (a AccountID) ToInt64() (int64, error) {
62+
var value int64
63+
buf := bytes.NewReader(a[:])
64+
if err := binary.Read(buf, byteOrder, &value); err != nil {
65+
return 0, err
66+
}
67+
68+
return value, nil
69+
}
70+
71+
// AccountIDFromInt64 converts an int64 to an AccountID.
72+
func AccountIDFromInt64(value int64) (AccountID, error) {
73+
var (
74+
a = AccountID{}
75+
buf = new(bytes.Buffer)
76+
)
77+
if err := binary.Write(buf, binary.BigEndian, value); err != nil {
78+
return a, err
79+
}
80+
copy(a[:], buf.Bytes())
81+
82+
return a, nil
83+
}
84+
5885
// String returns the string representation of the AccountID.
5986
func (a AccountID) String() string {
6087
return hex.EncodeToString(a[:])

0 commit comments

Comments
 (0)