代码拉取完成,页面将自动刷新
同步操作将从 mirrors_jinzhu/gorm 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。