forked from breaded-xyz/alphakit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvklinereader.go
65 lines (53 loc) · 1.38 KB
/
csvklinereader.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
// Copyright 2022 The Coln Group Ltd
// SPDX-License-Identifier: MIT
package market
import (
"encoding/csv"
"io"
)
var _ KlineReader = (*CSVKlineReader)(nil)
// CSVKlineReader is a KlineReader that reads from a CSV file.
type CSVKlineReader struct {
csv *csv.Reader
decoder CSVKlineDecoder
}
// MakeCSVKlineReader is a factory method type that creates a new CSVKlineReader.
type MakeCSVKlineReader func(csv *csv.Reader) *CSVKlineReader
// NewCSVKlineReader creates a new CSVKlineReader with the default Binance decoder.
func NewCSVKlineReader(csv *csv.Reader) *CSVKlineReader {
return &CSVKlineReader{
csv: csv,
decoder: BinanceCSVKlineDecoder,
}
}
// NewCSVKlineReaderWithDecoder creates a new CSVKlineReader with the given decoder.
func NewCSVKlineReaderWithDecoder(csv *csv.Reader, decoder CSVKlineDecoder) *CSVKlineReader {
return &CSVKlineReader{
csv: csv,
decoder: decoder,
}
}
// Read reads the next Kline from the underlying CSV data.
func (r *CSVKlineReader) Read() (Kline, error) {
var k Kline
rec, err := r.csv.Read()
if err != nil {
return k, err
}
return r.decoder(rec)
}
// ReadAll reads all the Klines from the underlying CSV data.
func (r *CSVKlineReader) ReadAll() ([]Kline, error) {
var ks []Kline
for {
k, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
ks = append(ks, k)
}
return ks, nil
}