-
Notifications
You must be signed in to change notification settings - Fork 15
/
value_converter.go
67 lines (60 loc) · 1.43 KB
/
value_converter.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
package csvq
import (
"database/sql/driver"
"fmt"
"time"
)
type ValueConverter struct {
}
func (c ValueConverter) ConvertValue(v interface{}) (driver.Value, error) {
if IsCsvqValue(v) {
return v, nil
}
if v == nil {
return Null{}, nil
}
switch v.(type) {
case string:
return String{value: v.(string)}, nil
case int:
return Integer{value: int64(v.(int))}, nil
case int8:
return Integer{value: int64(v.(int8))}, nil
case int16:
return Integer{value: int64(v.(int16))}, nil
case int32:
return Integer{value: int64(v.(int32))}, nil
case int64:
return Integer{value: v.(int64)}, nil
case uint:
return Integer{value: int64(v.(uint))}, nil
case uint8:
return Integer{value: int64(v.(uint8))}, nil
case uint16:
return Integer{value: int64(v.(uint16))}, nil
case uint32:
return Integer{value: int64(v.(uint32))}, nil
case uint64:
u64 := v.(uint64)
if u64 >= 1<<63 {
return nil, fmt.Errorf("uint64 values with high bit set are not supported")
}
return Integer{value: int64(u64)}, nil
case float32:
return Float{value: float64(v.(float32))}, nil
case float64:
return Float{value: v.(float64)}, nil
case bool:
return Boolean{value: v.(bool)}, nil
case time.Time:
return Datetime{value: v.(time.Time)}, nil
}
return nil, fmt.Errorf("unsupported type: %T", v)
}
func IsCsvqValue(v interface{}) bool {
switch v.(type) {
case String, Integer, Float, Boolean, Datetime, Null:
return true
}
return false
}