forked from stellar/go
/
db.go
121 lines (98 loc) · 2.8 KB
/
db.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
package xdr
import (
"errors"
"fmt"
"github.com/lib/pq"
)
// This file contains implementations of the sql.Scanner interface for stellar xdr types
// Scan reads from src into an AccountFlags
func (t *AccountFlags) Scan(src interface{}) error {
val, ok := src.(int64)
if !ok {
return errors.New("Invalid value for xdr.AccountFlags")
}
*t = AccountFlags(val)
return nil
}
// Scan reads from src into an AssetType
func (t *AssetType) Scan(src interface{}) error {
val, ok := src.(int64)
if !ok {
return errors.New("Invalid value for xdr.AssetType")
}
*t = AssetType(val)
return nil
}
// Scan reads from src into an Int64
func (t *Int64) Scan(src interface{}) error {
val, ok := src.(int64)
if !ok {
return errors.New("Invalid value for xdr.Int64")
}
*t = Int64(val)
return nil
}
// Scan reads from a src an xdr.Price
func (t *Price) Scan(src interface{}) error {
// assuming the price is represented as a two-element array [n,d]
arr := pq.Int64Array{}
err := arr.Scan(src)
if err != nil {
return err
}
if len(arr) != 2 {
return errors.New("price array should have exactly 2 elements")
}
*t = Price{Int32(arr[0]), Int32(arr[1])}
return nil
}
// Scan reads from src into an LedgerEntryChanges struct
func (t *LedgerEntryChanges) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an LedgerHeader struct
func (t *LedgerHeader) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an ScpEnvelope struct
func (t *ScpEnvelope) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an ScpEnvelope struct
func (t *ScpQuorumSet) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an Thresholds struct
func (t *Thresholds) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an TransactionEnvelope struct
func (t *TransactionEnvelope) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an TransactionMeta struct
func (t *TransactionMeta) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an TransactionResult struct
func (t *TransactionResult) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// Scan reads from src into an TransactionResultPair struct
func (t *TransactionResultPair) Scan(src interface{}) error {
return safeBase64Scan(src, t)
}
// safeBase64Scan scans from src (which should be either a []byte or string)
// into dest by using `SafeUnmarshalBase64`.
func safeBase64Scan(src, dest interface{}) error {
var val string
switch src := src.(type) {
case []byte:
val = string(src)
case string:
val = src
default:
return fmt.Errorf("Invalid value for %T", dest)
}
return SafeUnmarshalBase64(val, dest)
}