/
converters.go
98 lines (86 loc) · 3.78 KB
/
converters.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
94
95
96
97
98
/*
* Copyright 2018-2021 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package objectbox
import (
"fmt"
"strconv"
"time"
)
// StringIdConvertToEntityProperty implements "StringIdConvert" property value converter
func StringIdConvertToEntityProperty(dbValue uint64) (string, error) {
return strconv.FormatUint(dbValue, 10), nil
}
// StringIdConvertToDatabaseValue implements "StringIdConvert" property value converter
func StringIdConvertToDatabaseValue(goValue string) (uint64, error) {
// in case the object was initialized by the user without setting the ID explicitly
if goValue == "" {
return 0, nil
}
return strconv.ParseUint(goValue, 10, 64)
}
// TimeInt64ConvertToEntityProperty converts Unix timestamp in milliseconds (ObjectBox date field) to time.Time
// NOTE - you lose precision - anything smaller then milliseconds is dropped
func TimeInt64ConvertToEntityProperty(dbValue int64) (time.Time, error) {
return time.Unix(dbValue/1000, dbValue%1000*1000000).UTC(), nil
}
// TimeInt64ConvertToDatabaseValue converts time.Time to Unix timestamp in milliseconds (internal format expected by ObjectBox on a date field)
// NOTE - you lose precision - anything smaller then milliseconds is dropped
func TimeInt64ConvertToDatabaseValue(goValue time.Time) (int64, error) {
var ms = int64(goValue.Nanosecond()) / 1000000
return goValue.Unix()*1000 + ms, nil
}
// NanoTimeInt64ConvertToEntityProperty converts Unix timestamp in nanoseconds (ObjectBox date-nano field) to time.Time
func NanoTimeInt64ConvertToEntityProperty(dbValue int64) (time.Time, error) {
const nsInSec = 1000 * 1000 * 1000
var seconds = dbValue / nsInSec
return time.Unix(seconds, dbValue-seconds*nsInSec).UTC(), nil
}
// NanoTimeInt64ConvertToDatabaseValue converts time.Time to Unix timestamp in nanoseconds (internal format expected by ObjectBox on a date-nano field)
func NanoTimeInt64ConvertToDatabaseValue(goValue time.Time) (int64, error) {
return goValue.UnixNano(), nil
}
// TimeTextConvertToEntityProperty uses time.Time.UnmarshalText() to decode RFC 3339 formatted string to time.Time.
func TimeTextConvertToEntityProperty(dbValue string) (goValue time.Time, err error) {
err = goValue.UnmarshalText([]byte(dbValue))
if err != nil {
err = fmt.Errorf("error unmarshalling time %v: %v", dbValue, err)
}
return goValue, err
}
// TimeTextConvertToDatabaseValue uses time.Time.MarshalText() to encode time.Time into RFC 3339 formatted string.
func TimeTextConvertToDatabaseValue(goValue time.Time) (string, error) {
bytes, err := goValue.MarshalText()
if err != nil {
err = fmt.Errorf("error marshalling time %v: %v", goValue, err)
}
return string(bytes), err
}
// TimeBinaryConvertToEntityProperty uses time.Time.UnmarshalBinary() to decode time.Time.
func TimeBinaryConvertToEntityProperty(dbValue []byte) (goValue time.Time, err error) {
err = goValue.UnmarshalBinary(dbValue)
if err != nil {
err = fmt.Errorf("error unmarshalling time %v: %v", dbValue, err)
}
return goValue, err
}
// TimeBinaryConvertToDatabaseValue uses time.Time.MarshalBinary() to encode time.Time.
func TimeBinaryConvertToDatabaseValue(goValue time.Time) ([]byte, error) {
bytes, err := goValue.MarshalBinary()
if err != nil {
err = fmt.Errorf("error marshalling time %v: %v", goValue, err)
}
return bytes, err
}