-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgeneric.go
53 lines (44 loc) · 992 Bytes
/
generic.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
package generic
import (
"bytes"
"database/sql/driver"
"reflect"
)
// Type is the interface used as the basis for generic types
type Type interface {
Valid() bool
Value() (driver.Value, error)
Scan(interface{}) error
Set(interface{}) error
Reset()
}
// ErrInvalidGenericValue is used as error in generic types
type ErrInvalidGenericValue struct {
Value interface{}
}
// ValidFlag is the flag to check that value is valid
type ValidFlag bool
var nullBytes = []byte("null")
// Reset resets ValidFlag
func (v *ValidFlag) Reset() {
*v = false
}
// Valid validates the specified value is nil or not
func (v ValidFlag) Valid() bool {
return bool(v)
}
// Error returns error message
func (e ErrInvalidGenericValue) Error() string {
buf := bytes.Buffer{}
buf.WriteString("invalid value: ")
t := reflect.TypeOf(e.Value)
switch t {
case nil:
buf.WriteString("(nil)")
default:
buf.WriteByte('(')
buf.WriteString(t.String())
buf.WriteByte(')')
}
return buf.String()
}