forked from nivertech/go-finance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.go
56 lines (43 loc) · 941 Bytes
/
fields.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
package finance
import (
"reflect"
"github.com/shopspring/decimal"
)
func mapFields(vals []string, fc int, v interface{}) {
typ := reflect.TypeOf(v)
val := reflect.ValueOf(v).Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < fc; i++ {
f := val.Field(i)
switch f.Interface().(type) {
case string:
f.Set(reflect.ValueOf(vals[i]))
case int:
f.Set(reflect.ValueOf(toInt(vals[i])))
case Datetime:
f.Set(reflect.ValueOf(ParseDatetime(vals[i])))
case decimal.Decimal:
f.Set(reflect.ValueOf(toDecimal(vals[i])))
case Value:
f.Set(reflect.ValueOf(toEventValue(vals[i])))
}
}
}
func structFields(in interface{}) (str string, fc int) {
typ := reflect.TypeOf(in)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
tag := f.Tag.Get("yfin")
if tag == "-" {
continue
}
str = str + tag
fc++
}
return
}