forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar_types.go
183 lines (157 loc) · 3.93 KB
/
scalar_types.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* 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 types
import (
"time"
"github.com/dgraph-io/dgraph/protos/intern"
geom "github.com/twpayne/go-geom"
)
const (
nanoSecondsInSec = 1000000000
)
// Note: These ids are stored in the posting lists to indicate the type
// of the data. The order *cannot* be changed without breaking existing
// data. When adding a new type *always* add to the end of this list.
// Never delete anything from this list even if it becomes unused.
const (
BinaryID = TypeID(intern.Posting_BINARY)
IntID = TypeID(intern.Posting_INT)
FloatID = TypeID(intern.Posting_FLOAT)
BoolID = TypeID(intern.Posting_BOOL)
DateTimeID = TypeID(intern.Posting_DATETIME)
StringID = TypeID(intern.Posting_STRING)
GeoID = TypeID(intern.Posting_GEO)
UidID = TypeID(intern.Posting_UID)
PasswordID = TypeID(intern.Posting_PASSWORD)
DefaultID = TypeID(intern.Posting_DEFAULT)
)
var typeNameMap = map[string]TypeID{
"int": IntID,
"float": FloatID,
"string": StringID,
"bool": BoolID,
"datetime": DateTimeID,
"geo": GeoID,
"uid": UidID,
"password": PasswordID,
"default": DefaultID,
}
type TypeID intern.Posting_ValType
func (t TypeID) Enum() intern.Posting_ValType {
return intern.Posting_ValType(t)
}
func (t TypeID) Name() string {
switch t {
case IntID:
return "int"
case FloatID:
return "float"
case BoolID:
return "bool"
case StringID:
return "string"
case DateTimeID:
return "datetime"
case GeoID:
return "geo"
case UidID:
return "uid"
case PasswordID:
return "password"
case DefaultID:
return "default"
case BinaryID:
return "binary"
}
return ""
}
// Val is a value with type information.
type Val struct {
Tid TypeID
Value interface{}
}
// TypeForName returns the type corresponding to the given name.
// If name is not recognized, it returns nil.
func TypeForName(name string) (TypeID, bool) {
t, ok := typeNameMap[name]
return t, ok
}
func (t TypeID) IsScalar() bool {
return t != UidID
}
// ValueForType returns the zero value for a type id
func ValueForType(id TypeID) Val {
switch id {
case BinaryID:
var b []byte
return Val{BinaryID, &b}
case IntID:
var i int64
return Val{IntID, &i}
case FloatID:
var f float64
return Val{FloatID, &f}
case BoolID:
var b bool
return Val{BoolID, &b}
case DateTimeID:
var t time.Time
return Val{DateTimeID, &t}
case StringID:
var s string
return Val{StringID, s}
case DefaultID:
var s string
return Val{DefaultID, s}
case GeoID:
var g geom.T
return Val{GeoID, &g}
case UidID:
var i uint64
return Val{UidID, &i}
case PasswordID:
var p string
return Val{PasswordID, p}
default:
return Val{}
}
}
func createDate(y int, m time.Month, d int) time.Time {
var dt time.Time
dt = time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
return dt
}
func ParseTime(val string) (time.Time, error) {
var t time.Time
if err := t.UnmarshalText([]byte(val)); err == nil {
return t, err
}
// try without timezone
if t, err := time.Parse(dateTimeFormat, val); err == nil {
return t, err
}
if t, err := time.Parse(dateFormatYMD, val); err == nil {
return t, err
}
if t, err := time.Parse(dateFormatYM, val); err == nil {
return t, err
}
return time.Parse(dateFormatY, val)
}
const dateFormatYMD = "2006-01-02"
const dateFormatYM = "2006-01"
const dateFormatY = "2006"
const dateTimeFormat = "2006-01-02T15:04:05"