6
6
"strings"
7
7
"time"
8
8
9
+ "golang.org/x/exp/constraints"
9
10
"gorm.io/gorm"
10
11
"gorm.io/gorm/clause"
11
12
)
17
18
ALL = Star
18
19
)
19
20
21
+ // ScanValuer interface for Field
22
+ type ScanValuer interface {
23
+ Scan (src interface {}) error // sql.Scanner
24
+ Value () (driver.Value , error ) // driver.Valuer
25
+ }
26
+
20
27
// Option field option
21
28
type Option func (clause.Column ) clause.Column
22
29
30
37
// ======================== generic field =======================
31
38
32
39
// NewField create new field
33
- func NewField (table , column string , opts ... Option ) Field {
34
- return Field { GenericsField : GenericsField [driver.Valuer ]{expr {col : toColumn (table , column , opts ... )} }}
40
+ func NewField (table , column string , opts ... Option ) genericsField [driver. Valuer ] {
41
+ return genericsField [driver.Valuer ]{expr {col : toColumn (table , column , opts ... )}}
35
42
}
36
43
37
44
// NewSerializer create new field2
@@ -46,92 +53,97 @@ func NewAsterisk(table string, opts ...Option) Asterisk {
46
53
47
54
// ======================== integer =======================
48
55
49
- // NewInt create new Int
50
- func NewInt (table , column string , opts ... Option ) Int {
51
- return Int {NewGenericsInt [int ](expr {col : toColumn (table , column , opts ... )})}
56
+ // NewNumber build number type field
57
+ func NewNumber [T constraints.Integer | constraints.Float ](table , column string , opts ... Option ) Number [T ] {
58
+ return newNumber [T ](expr {col : toColumn (table , column , opts ... )})
59
+ }
60
+
61
+ // NewInt create new field for int
62
+ func NewInt (table , column string , opts ... Option ) Number [int ] {
63
+ return NewNumber [int ](table , column , opts ... )
52
64
}
53
65
54
- // NewInt8 create new Int8
55
- func NewInt8 (table , column string , opts ... Option ) Int8 {
56
- return Int8 { NewGenericsInt [int8 ](expr { col : toColumn ( table , column , opts ... )})}
66
+ // NewInt8 create new field for int8
67
+ func NewInt8 (table , column string , opts ... Option ) Number [ int8 ] {
68
+ return NewNumber [int8 ](table , column , opts ... )
57
69
}
58
70
59
- // NewInt16 ...
60
- func NewInt16 (table , column string , opts ... Option ) Int16 {
61
- return Int16 { NewGenericsInt [int16 ](expr { col : toColumn ( table , column , opts ... )})}
71
+ // NewInt16 create new field for int16
72
+ func NewInt16 (table , column string , opts ... Option ) Number [ int16 ] {
73
+ return NewNumber [int16 ](table , column , opts ... )
62
74
}
63
75
64
- // NewInt32 ...
65
- func NewInt32 (table , column string , opts ... Option ) Int32 {
66
- return Int32 { NewGenericsInt [int32 ](expr { col : toColumn ( table , column , opts ... )})}
76
+ // NewInt32 create new field for int32
77
+ func NewInt32 (table , column string , opts ... Option ) Number [ int32 ] {
78
+ return NewNumber [int32 ](table , column , opts ... )
67
79
}
68
80
69
- // NewInt64 ...
70
- func NewInt64 (table , column string , opts ... Option ) Int64 {
71
- return Int64 { NewGenericsInt [int64 ](expr { col : toColumn ( table , column , opts ... )})}
81
+ // NewInt64 create new field for int64
82
+ func NewInt64 (table , column string , opts ... Option ) Number [ int64 ] {
83
+ return NewNumber [int64 ](table , column , opts ... )
72
84
}
73
85
74
- // NewUint ...
75
- func NewUint (table , column string , opts ... Option ) Uint {
76
- return Uint { NewGenericsInt [uint ](expr { col : toColumn ( table , column , opts ... )})}
86
+ // NewUint create new field for uint
87
+ func NewUint (table , column string , opts ... Option ) Number [ uint ] {
88
+ return NewNumber [uint ](table , column , opts ... )
77
89
}
78
90
79
- // NewUint8 ...
80
- func NewUint8 (table , column string , opts ... Option ) Uint8 {
81
- return Uint8 { NewGenericsInt [uint8 ](expr { col : toColumn ( table , column , opts ... )})}
91
+ // NewUint8 create new field for uint8
92
+ func NewUint8 (table , column string , opts ... Option ) Number [ uint8 ] {
93
+ return NewNumber [uint8 ](table , column , opts ... )
82
94
}
83
95
84
- // NewUint16 ...
85
- func NewUint16 (table , column string , opts ... Option ) Uint16 {
86
- return Uint16 { NewGenericsInt [uint16 ](expr { col : toColumn ( table , column , opts ... )})}
96
+ // NewUint16 create new field for uint16
97
+ func NewUint16 (table , column string , opts ... Option ) Number [ uint16 ] {
98
+ return NewNumber [uint16 ](table , column , opts ... )
87
99
}
88
100
89
- // NewUint32 ...
90
- func NewUint32 (table , column string , opts ... Option ) Uint32 {
91
- return Uint32 { NewGenericsInt [uint32 ](expr { col : toColumn ( table , column , opts ... )})}
101
+ // NewUint32 create new field for uint32
102
+ func NewUint32 (table , column string , opts ... Option ) Number [ uint32 ] {
103
+ return NewNumber [uint32 ](table , column , opts ... )
92
104
}
93
105
94
- // NewUint64 ...
95
- func NewUint64 (table , column string , opts ... Option ) Uint64 {
96
- return Uint64 { NewGenericsInt [uint64 ](expr { col : toColumn ( table , column , opts ... )})}
106
+ // NewUint64 create new field for uint64
107
+ func NewUint64 (table , column string , opts ... Option ) Number [ uint64 ] {
108
+ return NewNumber [uint64 ](table , column , opts ... )
97
109
}
98
110
99
111
// ======================== float =======================
100
112
101
- // NewFloat32 ...
102
- func NewFloat32 (table , column string , opts ... Option ) Float32 {
103
- return Float32 { NewGenericsInt [float32 ](expr { col : toColumn ( table , column , opts ... )})}
113
+ // NewFloat32 create new field for float32
114
+ func NewFloat32 (table , column string , opts ... Option ) Number [ float32 ] {
115
+ return NewNumber [float32 ](table , column , opts ... )
104
116
}
105
117
106
- // NewFloat64 ...
107
- func NewFloat64 (table , column string , opts ... Option ) Float64 {
108
- return Float64 { NewGenericsInt [float64 ](expr { col : toColumn ( table , column , opts ... )})}
118
+ // NewFloat64 create new field for float64
119
+ func NewFloat64 (table , column string , opts ... Option ) Number [ float64 ] {
120
+ return NewNumber [float64 ](table , column , opts ... )
109
121
}
110
122
111
123
// ======================== string =======================
112
124
113
125
// NewString ...
114
- func NewString (table , column string , opts ... Option ) String {
115
- return String { NewGenericsString [string ](expr {col : toColumn (table , column , opts ... )})}
126
+ func NewString (table , column string , opts ... Option ) Chars [ string ] {
127
+ return newChars [string ](expr {col : toColumn (table , column , opts ... )})
116
128
}
117
129
118
130
// NewBytes ...
119
- func NewBytes (table , column string , opts ... Option ) Bytes {
120
- return Bytes { NewGenericsString [[]byte ](expr {col : toColumn (table , column , opts ... )})}
131
+ func NewBytes (table , column string , opts ... Option ) Chars [[] byte ] {
132
+ return newChars [[]byte ](expr {col : toColumn (table , column , opts ... )})
121
133
}
122
134
123
135
// ======================== bool =======================
124
136
125
137
// NewBool ...
126
138
func NewBool (table , column string , opts ... Option ) Bool {
127
- return Bool {NewGenerics [bool ]( expr {col : toColumn (table , column , opts ... )}) }
139
+ return Bool {genericsField [bool ]{ expr {col : toColumn (table , column , opts ... )}} }
128
140
}
129
141
130
142
// ======================== time =======================
131
143
132
144
// NewTime ...
133
145
func NewTime (table , column string , opts ... Option ) Time {
134
- return Time {NewGenerics [time.Time ]( expr {col : toColumn (table , column , opts ... )}) }
146
+ return Time {genericsField [time.Time ]{ expr {col : toColumn (table , column , opts ... )}} }
135
147
}
136
148
137
149
func toColumn (table , column string , opts ... Option ) clause.Column {
0 commit comments