This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility.go
69 lines (61 loc) · 2.23 KB
/
utility.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
package cql
import (
"database/sql/driver"
"fmt"
"reflect"
"time"
"github.com/gocql/gocql"
)
// valuesToInterface coverts driver.Value to interface
func valuesToInterface(args []driver.Value) []interface{} {
values := make([]interface{}, len(args))
for i := 0; i < len(args); i++ {
values[i] = args[i]
}
return values
}
// namedValuesToInterface coverts driver.NamedValue to interface
func namedValuesToInterface(namedValues []driver.NamedValue) ([]interface{}, error) {
values := make([]interface{}, len(namedValues))
for i := 0; i < len(namedValues); i++ {
if len(namedValues[i].Name) > 0 {
return []interface{}{}, ErrNamedValuesNotSupported
}
if namedValues[i].Ordinal < 1 || namedValues[i].Ordinal > len(namedValues) {
return []interface{}{}, ErrOrdinalOutOfRange
}
values[namedValues[i].Ordinal-1] = namedValues[i].Value
}
return values, nil
}
// columnInfoToString coverts gocql.ColumnInfo to string
func columnInfoToString(columnInfo []gocql.ColumnInfo) []string {
names := make([]string, len(columnInfo))
for i := 0; i < len(columnInfo); i++ {
names[i] = columnInfo[i].Name
}
return names
}
// interfaceToValue coverts interface to driver.Value
func interfaceToValue(sourceInterface interface{}) (driver.Value, error) {
source := reflect.ValueOf(sourceInterface)
if source.Kind() != reflect.Ptr {
return driver.Value(nil), fmt.Errorf("source is not a pointer")
}
return driver.Value(source.Elem().Interface()), nil
}
// DurationToDuration converts gocql.Duration type to time.Duration.
// Does not check for overflow
func DurationToDuration(cqlDuration gocql.Duration) time.Duration {
return (2629800000000000 * time.Duration(cqlDuration.Months)) + (86400000000000 * time.Duration(cqlDuration.Days)) + time.Duration(cqlDuration.Nanoseconds)
}
// InterfaceToDuration converts an interface of gocql.Duration type to time.Duration.
// Does not check for overflow.
// Returns 0 if interface is not gocql.Duration
func InterfaceToDuration(aInterface interface{}) time.Duration {
cqlDuration, ok := aInterface.(gocql.Duration)
if !ok {
return time.Duration(0)
}
return (2629800000000000 * time.Duration(cqlDuration.Months)) + (86400000000000 * time.Duration(cqlDuration.Days)) + time.Duration(cqlDuration.Nanoseconds)
}