This commit is contained in:
+45
-1
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Column struct {
|
||||
@@ -51,7 +52,7 @@ func (c *Column) GetDDL() (string, error) {
|
||||
|
||||
func (c *Column) inlineModifiers() []string {
|
||||
var mods []string
|
||||
for k, _ := range c.Modifiers {
|
||||
for k := range c.Modifiers {
|
||||
switch k {
|
||||
case "nn":
|
||||
mods = append(mods, "NOT NULL")
|
||||
@@ -80,3 +81,46 @@ func (c *Column) IsPK() bool {
|
||||
_, ok := c.Modifiers["pk"]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Encodes the value to be DB compatible
|
||||
func (c Column) Encode(value reflect.Value) any {
|
||||
typ := value.Type().String()
|
||||
|
||||
var encoded any
|
||||
switch typ {
|
||||
case "time.Time":
|
||||
timeVal := value.Interface().(time.Time)
|
||||
encoded = timeVal.UnixMilli()
|
||||
case "bool":
|
||||
boolVal := value.Interface().(bool)
|
||||
if boolVal {
|
||||
encoded = 1
|
||||
} else {
|
||||
encoded = false
|
||||
}
|
||||
default:
|
||||
encoded = value.Interface()
|
||||
}
|
||||
|
||||
return encoded
|
||||
}
|
||||
|
||||
// Decodes the raw value from the DBs
|
||||
func (c Column) Decode(expected string, rawValue reflect.Value) any {
|
||||
var decoded any
|
||||
switch expected {
|
||||
case "time.Time":
|
||||
decoded = time.UnixMilli(rawValue.Interface().(int64))
|
||||
case "bool":
|
||||
rawValue := rawValue.Interface().(int)
|
||||
if rawValue > 0 {
|
||||
decoded = true
|
||||
} else {
|
||||
decoded = false
|
||||
}
|
||||
default:
|
||||
decoded = rawValue.Interface()
|
||||
}
|
||||
|
||||
return decoded
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user