-
Notifications
You must be signed in to change notification settings - Fork 37
/
type.go
126 lines (106 loc) · 3.04 KB
/
type.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dosa
import (
"github.com/gofrs/uuid"
"github.com/pkg/errors"
)
//go:generate stringer -type=Type
// Type defines a data type for an entity field
type Type int
const (
// Invalid type to be used as zero value for Type
Invalid Type = iota
// TUUID is different from dosa.UUID
TUUID
// String represents a string
String
// Int32 represents an int32
Int32
// Int64 represents either an int64 or a regular int
Int64
// Double is a float64
Double
// Blob is a byte slice
Blob
// Timestamp is a time.Time
Timestamp
// Bool is a bool type
Bool
)
// UUID stores a string format of uuid.
// Validation is done before saving to datastore.
// The format of uuid used in datastore is orthogonal to the string format here.
type UUID string
// NewUUID is a helper for returning a new dosa.UUID value
func NewUUID() UUID {
// return UUID(uuid.Must(uuid.NewV4()).String())
return UUID(uuid.Must(uuid.NewV4()).String())
}
// Bytes gets the bytes from a UUID
func (u UUID) Bytes() ([]byte, error) {
id, err := uuid.FromString(string(u))
if err != nil {
return nil, errors.Wrap(err, "invalid uuid string")
}
return id.Bytes(), nil
}
// BytesToUUID creates a UUID from a byte slice
func BytesToUUID(bs []byte) (UUID, error) {
id, err := uuid.FromBytes(bs)
if err != nil {
return "", errors.Wrap(err, "invalid uuid bytes")
}
return UUID(id.String()), nil
}
// FromString converts string to dosa Type
func FromString(s string) Type {
switch s {
case TUUID.String():
return TUUID
case String.String():
return String
case Int32.String():
return Int32
case Int64.String():
return Int64
case Double.String():
return Double
case Blob.String():
return Blob
case Timestamp.String():
return Timestamp
case Bool.String():
return Bool
default:
return Invalid
}
}
func isInvalidPrimaryKeyType(c *ColumnDefinition) bool {
if c.IsPointer {
return true
}
switch c.Type {
case Invalid:
return true
default:
return false
}
}