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
|
package gorm_test
import (
"database/sql/driver"
"encoding/hex"
"fmt"
"testing"
"github.com/jinzhu/gorm"
)
type CalculateField struct {
gorm.Model
Name string
Children []CalculateFieldChild
Category CalculateFieldCategory
EmbeddedField
}
type EmbeddedField struct {
EmbeddedName string `sql:"NOT NULL;DEFAULT:'hello'"`
}
type CalculateFieldChild struct {
gorm.Model
CalculateFieldID uint
Name string
}
type CalculateFieldCategory struct {
gorm.Model
CalculateFieldID uint
Name string
}
func TestCalculateField(t *testing.T) {
var field CalculateField
var scope = DB.NewScope(&field)
if field, ok := scope.FieldByName("Children"); !ok || field.Relationship == nil {
t.Errorf("Should calculate fields correctly for the first time")
}
if field, ok := scope.FieldByName("Category"); !ok || field.Relationship == nil {
t.Errorf("Should calculate fields correctly for the first time")
}
if field, ok := scope.FieldByName("embedded_name"); !ok {
t.Errorf("should find embedded field")
} else if _, ok := field.TagSettingsGet("NOT NULL"); !ok {
t.Errorf("should find embedded field's tag settings")
}
}
type UUID [16]byte
type NullUUID struct {
UUID
Valid bool
}
func FromString(input string) (u UUID) {
src := []byte(input)
return FromBytes(src)
}
func FromBytes(src []byte) (u UUID) {
dst := u[:]
hex.Decode(dst[0:4], src[0:8])
hex.Decode(dst[4:6], src[9:13])
hex.Decode(dst[6:8], src[14:18])
hex.Decode(dst[8:10], src[19:23])
hex.Decode(dst[10:], src[24:])
return
}
func (u UUID) String() string {
buf := make([]byte, 36)
src := u[:]
hex.Encode(buf[0:8], src[0:4])
buf[8] = '-'
hex.Encode(buf[9:13], src[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], src[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], src[8:10])
buf[23] = '-'
hex.Encode(buf[24:], src[10:])
return string(buf)
}
func (u UUID) Value() (driver.Value, error) {
return u.String(), nil
}
func (u *UUID) Scan(src interface{}) error {
switch src := src.(type) {
case UUID: // support gorm convert from UUID to NullUUID
*u = src
return nil
case []byte:
*u = FromBytes(src)
return nil
case string:
*u = FromString(src)
return nil
}
return fmt.Errorf("uuid: cannot convert %T to UUID", src)
}
func (u *NullUUID) Scan(src interface{}) error {
u.Valid = true
return u.UUID.Scan(src)
}
func TestFieldSet(t *testing.T) {
type TestFieldSetNullUUID struct {
NullUUID NullUUID
}
scope := DB.NewScope(&TestFieldSetNullUUID{})
field := scope.Fields()[0]
err := field.Set(FromString("3034d44a-da03-11e8-b366-4a00070b9f00"))
if err != nil {
t.Fatal(err)
}
if id, ok := field.Field.Addr().Interface().(*NullUUID); !ok {
t.Fatal()
} else if !id.Valid || id.UUID.String() != "3034d44a-da03-11e8-b366-4a00070b9f00" {
t.Fatal(id)
}
}
|