-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization.go
93 lines (67 loc) · 1.78 KB
/
serialization.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
package yadb
import (
"fmt"
"reflect"
clickhouse "github.com/undiabler/clickhouse-driver"
// log "github.com/sirupsen/logrus"
)
/*
CREATE TABLE events (
uuid String,
c_run UInt16,
event_type UInt8,
event_time DateTime,
event_date Date
) ENGINE = MergeTree(event_date, intHash32(c_run), (intHash32(c_run), event_type, cityHash64(uuid), event_date), 8192)
*/
// InsertMap get fields map and add them to BatchWriter queue
func (bw *BatchWriter) InsertMap(fields map[string]interface{}) error {
if bw.IsClosed() {
return fmt.Errorf("Goroutine cant accept data")
}
row := make(clickhouse.Row, 0, len(bw.columns))
for _, column := range bw.columns {
if tmp, ok := fields[column]; ok {
row = append(row, tmp)
} else {
return fmt.Errorf("Missed column %q", column)
}
}
bw.work <- row
return nil
}
func seria(val reflect.Value) map[string]interface{} {
switch val.Kind() {
case reflect.Ptr:
return seria(val.Elem())
case reflect.Struct:
elems := map[string]interface{}{}
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
typeField := val.Type().Field(i)
// if field.PkgPath != "" && !field.Anonymous {
// // unexported
// continue
// }
tag := typeField.Tag.Get("db")
if tag == "-" {
// ignore
continue
}
if tag == "" {
// no tag, but we can record the field name
// tag = camelCaseToSnakeCase(field.Name)
continue
}
elems[tag] = field.Interface()
fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", typeField.Name, field.Interface(), tag)
}
return elems
}
return nil
}
// InsertStruct get fields from struct using reflect and write them as map to DB
func (bw *BatchWriter) InsertStruct(f interface{}) error {
val := reflect.ValueOf(f)
return bw.InsertMap(seria(val))
}