-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
111 lines (88 loc) · 2.12 KB
/
type.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
package gobist
import (
"encoding/json"
"fmt"
"github.com/shopspring/decimal"
"time"
)
type Quote struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
Price string `json:"price"`
History History `json:"history,omitempty"`
Error string `json:"error,omitempty"`
}
func (q *Quote) ToJson() string {
d, _ := json.MarshalIndent(q, "", " ")
return string(d)
}
func (q *Quote) SetError(err error) {
q.Error = err.Error()
}
type History struct {
Begin HistoryData `json:"begin,omitempty"`
End HistoryData `json:"end,omitempty"`
Change HistoryChange `json:"change,omitempty"`
}
func (h *History) SetBegin(dt time.Time, price float64) {
h.Begin = HistoryData{dt.Format(time.DateOnly), decimal.NewFromFloat(price).Truncate(2).String()}
}
func (h *History) SetEnd(dt time.Time, price float64) {
h.End = HistoryData{dt.Format(time.DateOnly), decimal.NewFromFloat(price).Truncate(2).String()}
}
func (h *History) IsValid() bool {
return h.Begin.Date != "" && h.End.Date != ""
}
type HistoryData struct {
Date string `json:"date,omitempty"`
Price string `json:"price,omitempty"`
}
type HistoryChange struct {
ByRatio string `json:"byRatio"`
ByAmount string `json:"byAmount"`
}
type Symbol struct {
Id int `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
Icon string `json:"icon"`
}
type SymbolList struct {
Count int `json:"count"`
Items []Symbol `json:"items"`
}
func (s *SymbolList) fromDTO(d *symbolListResponse) *SymbolList {
if d == nil {
return s
}
s.Count = d.TotalCount
s.Items = make([]Symbol, d.TotalCount)
for i, v := range d.Data {
s.Items[i] = parseSymbolData(i, v.D)
}
return s
}
type QuoteList struct {
Count int `json:"count"`
Items []Quote `json:"items"`
}
func (ql QuoteList) ToJson() string {
d, _ := json.MarshalIndent(ql, "", " ")
return string(d)
}
func parseSymbolData(i int, d []string) Symbol {
s := Symbol{
Id: i + 1,
}
if len(d) != 3 {
return s
}
imgUrl := ""
if d[2] != "" {
imgUrl = fmt.Sprintf("https://s3-symbol-logo.tradingview.com/%s.svg", d[2])
}
s.Code = d[0]
s.Name = d[1]
s.Icon = imgUrl
return s
}