/
keytypes.go
133 lines (120 loc) · 2.92 KB
/
keytypes.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
122
123
124
125
126
127
128
129
130
131
132
133
package io
import (
"fmt"
"path/filepath"
"strings"
"github.com/alpacahq/marketstore/utils"
)
type TimeBucketKey struct {
Key string `msgpack:"key"` // Key is the appended form, suitable for exported usage
/*
itemKey string
categoryKey string
*/
}
const DefaultTimeBucketSchema = "Symbol/Timeframe/AttributeGroup"
func NewTimeBucketKey(itemKey string, categoryKey_opt ...string) (mk *TimeBucketKey) {
var categoryKey string
if len(categoryKey_opt) != 0 && categoryKey_opt[0] != "" {
categoryKey = categoryKey_opt[0]
} else {
categoryKey = DefaultTimeBucketSchema
}
mk = new(TimeBucketKey)
mk.Key = itemKey + ":" + categoryKey
return mk
}
func NewTimeBucketKeyFromString(itemCategoryString string) (mk *TimeBucketKey) {
splitKey := strings.Split(itemCategoryString, ":")
if len(splitKey) < 2 {
return NewTimeBucketKey(splitKey[0])
}
return NewTimeBucketKey(splitKey[0], splitKey[1])
}
func (mk *TimeBucketKey) String() (stringKey string) {
/*
return mk.itemKey + ":" + mk.categoryKey
*/
return mk.Key
}
func (mk *TimeBucketKey) GetCatKey() (catKey string) {
/*
return mk.categoryKey
*/
splitKey := strings.Split(mk.Key, ":")
return splitKey[1]
}
func (mk *TimeBucketKey) GetItemKey() (itemKey string) {
/*
return mk.itemKey
*/
splitKey := strings.Split(mk.Key, ":")
return splitKey[0]
}
func (mk *TimeBucketKey) GetCategories() (cats []string) {
/*
return strings.Split(mk.categoryKey, "/")
*/
return strings.Split(mk.GetCatKey(), "/")
}
func (mk *TimeBucketKey) GetItems() (items []string) {
/*
return strings.Split(mk.itemKey, "/")
*/
return strings.Split(mk.GetItemKey(), "/")
}
func (mk *TimeBucketKey) GetItemInCategory(catName string) (item string) {
for i, name := range mk.GetCategories() {
if name == catName {
return mk.GetItems()[i]
}
}
return ""
}
func (mk *TimeBucketKey) GetMultiItemInCategory(catName string) (items []string) {
for i, name := range mk.GetCategories() {
if name == catName {
presplit := mk.GetItems()[i]
items = strings.Split(presplit, ",")
return items
}
}
return nil
}
func (mk *TimeBucketKey) SetItemInCategory(catName string, itemName string) {
cats := mk.GetCategories()
items := mk.GetItems()
for i, cat := range cats {
if cat == catName {
items[i] = itemName
}
}
/*
mk.itemKey = ""
for i, item := range items {
mk.itemKey += item
if i != 2 {
mk.itemKey += "/"
}
}
mk.Key = mk.String()
*/
itemKey := ""
for i, item := range items {
itemKey += item
if i != 2 {
itemKey += "/"
}
}
mk.Key = itemKey + ":" + mk.GetCatKey()
}
func (mk *TimeBucketKey) GetTimeFrame() (tf *utils.Timeframe, err error) {
tfs := mk.GetItemInCategory("Timeframe")
if len(tfs) == 0 {
return &utils.Timeframe{}, fmt.Errorf("Error: Unable to get timeframe from key")
}
return utils.TimeframeFromString(tfs), nil
}
func (mk *TimeBucketKey) GetPathToYearFiles(rootDir string) string {
return filepath.Join(rootDir, mk.GetItemKey())
}