-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
facet_types.go
129 lines (119 loc) · 3.06 KB
/
facet_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
/*
* Copyright 2017 Dgraph Labs, Inc.
*
* 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 facets
import (
"bytes"
"strconv"
"time"
"unicode"
)
const (
Int32ID = TypeID(Facet_INT32)
FloatID = TypeID(Facet_FLOAT)
BoolID = TypeID(Facet_BOOL)
DateTimeID = TypeID(Facet_DATETIME)
StringID = TypeID(Facet_STRING)
)
type TypeID Facet_ValType
// ValTypeForTypeID gives Facet_ValType for given TypeID
func ValTypeForTypeID(typId TypeID) Facet_ValType {
switch typId {
case Int32ID:
return Facet_INT32
case FloatID:
return Facet_FLOAT
case BoolID:
return Facet_BOOL
case DateTimeID:
return Facet_DATETIME
case StringID:
return Facet_STRING
}
panic("Unhandled case in ValTypeForTypeID.")
}
// TypeIDForValType gives TypeID for Facet_ValType
func TypeIDForValType(valType Facet_ValType) TypeID {
switch valType {
case Facet_INT32:
return Int32ID
case Facet_FLOAT:
return FloatID
case Facet_BOOL:
return BoolID
case Facet_DATETIME:
return DateTimeID
case Facet_STRING:
return StringID
}
panic("Unhandled case in TypeIDForValType.")
}
// ValType gives Facet's TypeID for given facet value str.
func ValType(val string) (Facet_ValType, error) {
if _, err := strconv.ParseInt(val, 10, 32); err == nil {
return Facet_INT32, nil
} else if nume := err.(*strconv.NumError); nume.Err == strconv.ErrRange {
// check if whole string is only of nums or not.
// comes here for : 11111111111111111111132333uasfk333 ; see test.
nonNumChar := false
for _, v := range val {
if !unicode.IsDigit(v) {
nonNumChar = true
break
}
}
if !nonNumChar {
return Facet_INT32, err
}
}
if _, err := strconv.ParseFloat(val, 64); err == nil {
return Facet_FLOAT, nil
} else if nume := err.(*strconv.NumError); nume.Err == strconv.ErrRange {
return Facet_FLOAT, err
}
if val == "true" || val == "false" {
return Facet_BOOL, nil
}
if _, err := parseTime(val); err == nil {
return Facet_DATETIME, nil
}
return Facet_STRING, nil
}
func parseTime(val string) (time.Time, error) {
var t time.Time
if err := t.UnmarshalText([]byte(val)); err == nil {
return t, err
}
if t, err := time.Parse(dateTimeFormat, val); err == nil {
return t, err
}
return time.Parse(dateFormatYMD, val)
}
const dateFormatYMD = "2006-01-02"
const dateTimeFormat = "2006-01-02T15:04:05"
func SameFacets(a []*Facet, b []*Facet) bool {
if len(a) != len(b) {
return false
}
la := len(a)
for i := 0; i < la; i++ {
if (a[i].Key != b[i].Key) ||
!bytes.Equal(a[i].Value, b[i].Value) ||
(a[i].ValType != b[i].ValType) {
return false
}
}
return true
}