-
Notifications
You must be signed in to change notification settings - Fork 2
/
record.go
82 lines (67 loc) · 1.72 KB
/
record.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
package gravity_sdk_types_record
import (
"encoding/binary"
"errors"
"fmt"
"math"
"strconv"
)
type RecordDef struct {
HasPrimary bool
PrimaryColumn string
Values map[string]interface{}
ColumnDefs []*ColumnDef
}
type ColumnDef struct {
ColumnName string
BindingName string
Value interface{}
}
func GetValue(value *Value) interface{} {
switch value.Type {
case DataType_FLOAT64:
return math.Float64frombits(binary.LittleEndian.Uint64(value.Value))
case DataType_INT64:
return int64(binary.LittleEndian.Uint64(value.Value))
case DataType_UINT64:
return uint64(binary.LittleEndian.Uint64(value.Value))
case DataType_BOOLEAN:
return int8(value.Value[0]) & 1
case DataType_STRING:
return string(value.Value)
}
// binary
return value.Value
}
func GetDefinition(record *Record) (*RecordDef, error) {
recordDef := &RecordDef{
HasPrimary: false,
Values: make(map[string]interface{}),
ColumnDefs: make([]*ColumnDef, 0, len(record.Fields)),
}
// Scanning fields
for n, field := range record.Fields {
value := GetValue(field.Value)
// Primary key
// if field.IsPrimary == true {
if record.PrimaryKey == field.Name {
recordDef.Values["primary_val"] = value
recordDef.HasPrimary = true
recordDef.PrimaryColumn = field.Name
continue
}
// Generate binding name
bindingName := fmt.Sprintf("val_%s", strconv.Itoa(n))
recordDef.Values[bindingName] = value
// Store definition
recordDef.ColumnDefs = append(recordDef.ColumnDefs, &ColumnDef{
ColumnName: field.Name,
Value: field.Name,
BindingName: bindingName,
})
}
if len(record.PrimaryKey) > 0 && !recordDef.HasPrimary {
return nil, errors.New("Not found primary key")
}
return recordDef, nil
}