forked from square/squalor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
170 lines (157 loc) · 4.49 KB
/
validate.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
// Copyright 2014 Square 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 squalor
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"sort"
"strings"
"time"
)
func sqlBaseType(sqlType string) string {
if index := strings.IndexAny(sqlType, "( "); index != -1 {
return sqlType[:index]
}
return sqlType
}
var (
byteSliceType = reflect.TypeOf([]byte(""))
setStringType = reflect.TypeOf(map[string]struct{}{})
scannerType = reflect.TypeOf(new(sql.Scanner)).Elem()
valuerType = reflect.TypeOf(new(driver.Valuer)).Elem()
timeType = reflect.TypeOf(time.Time{})
)
func validateIntType(bits int, sqlType string, goType reflect.Type) bool {
if strings.HasSuffix(sqlType, " unsigned") {
switch goType.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return bits == goType.Bits()
}
} else {
switch goType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return bits == goType.Bits()
}
}
return false
}
func validateBitType(sqlType string, goType reflect.Type) bool {
bits := 0
if n, err := fmt.Sscanf(sqlType, "bit(%d)", &bits); err != nil {
return false
} else if n != 1 {
return false
}
requiredBits := []int{8, 16, 32, 64}
i := sort.SearchInts(requiredBits, bits)
if i >= len(requiredBits) {
return false
}
bits = requiredBits[i]
switch goType.Kind() {
case reflect.Int, reflect.Uint:
fallthrough
case reflect.Int8, reflect.Uint8:
fallthrough
case reflect.Int16, reflect.Uint16:
fallthrough
case reflect.Int32, reflect.Uint32:
fallthrough
case reflect.Int64, reflect.Uint64:
return bits == goType.Bits()
}
return false
}
func validateFloatType(bits int, goType reflect.Type) bool {
switch goType.Kind() {
case reflect.Float32:
return bits == 32
case reflect.Float64:
return bits == 64
}
return false
}
func validateDateTimeType(goType reflect.Type) bool {
switch {
case goType.Kind() == reflect.Int64:
return true
case goType == timeType:
return true
}
return false
}
func validateStringType(goType reflect.Type) bool {
switch {
case goType.Kind() == reflect.String:
return true
case goType == byteSliceType:
return true
}
return false
}
func validateModelType(sqlType string, goType reflect.Type) error {
// If the go type implements the driver.Valuer and sql.Scanner
// interfaces, just ignore the sql type and assume it is doing the
// right thing. It would be possible to instantiate the go type and
// check to see if it can handle the sql type, but that seems overly
// complicated for the benefit.
if goType.Implements(valuerType) &&
reflect.PtrTo(goType).Implements(scannerType) {
return nil
}
sqlType = strings.ToLower(sqlType)
baseType := sqlBaseType(sqlType)
valid := false
switch baseType {
case "int":
valid = validateIntType(32, sqlType, goType)
case "tinyint":
// The "(1)" is really a formatting specifier, but MySQL uses it
// when "bool" or "boolean" is specified as the type.
valid = (sqlType == "tinyint(1)" && goType.Kind() == reflect.Bool)
valid = valid || validateIntType(8, sqlType, goType)
case "smallint":
valid = validateIntType(16, sqlType, goType)
case "mediumint":
valid = validateIntType(32, sqlType, goType)
case "bigint":
valid = validateIntType(64, sqlType, goType)
case "bit":
valid = validateBitType(sqlType, goType)
case "float":
valid = validateFloatType(32, goType)
case "double":
valid = validateFloatType(64, goType)
case "decimal":
valid = validateFloatType(64, goType)
case "date", "time", "datetime", "timestamp":
valid = validateDateTimeType(goType)
case "char", "varchar", "binary", "varbinary":
fallthrough
case "blob", "tinyblob", "mediumblob", "longblob":
fallthrough
case "text", "tinytext", "mediumtext", "longtext":
valid = validateStringType(goType)
case "enum":
valid = (goType.Kind() == reflect.String)
case "set":
valid = (goType == setStringType)
}
if valid {
return nil
}
return fmt.Errorf("incompatible types: \"%s\" vs \"%s\"", sqlType, goType)
}