mirror of
https://github.com/astaxie/beego.git
synced 2025-07-03 16:40:18 +00:00
orm add test about CustomField
This commit is contained in:
@ -3,6 +3,7 @@ package orm
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@ -10,6 +11,56 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// A true/false field.
|
||||
type SliceStringField []string
|
||||
|
||||
func (e SliceStringField) Value() []string {
|
||||
return []string(e)
|
||||
}
|
||||
|
||||
func (e *SliceStringField) Set(d []string) {
|
||||
*e = SliceStringField(d)
|
||||
}
|
||||
|
||||
func (e *SliceStringField) Add(v string) {
|
||||
*e = append(*e, v)
|
||||
}
|
||||
|
||||
func (e *SliceStringField) String() string {
|
||||
return strings.Join(e.Value(), ",")
|
||||
}
|
||||
|
||||
func (e *SliceStringField) FieldType() int {
|
||||
return TypeCharField
|
||||
}
|
||||
|
||||
func (e *SliceStringField) SetRaw(value interface{}) error {
|
||||
switch d := value.(type) {
|
||||
case []string:
|
||||
e.Set(d)
|
||||
case string:
|
||||
if len(d) > 0 {
|
||||
parts := strings.Split(d, ",")
|
||||
v := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
v = append(v, strings.TrimSpace(p))
|
||||
}
|
||||
e.Set(v)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *SliceStringField) RawValue() interface{} {
|
||||
return e.String()
|
||||
}
|
||||
|
||||
func (e *SliceStringField) Clean() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Id int
|
||||
Boolean bool
|
||||
@ -78,6 +129,7 @@ type User struct {
|
||||
Posts []*Post `orm:"reverse(many)" json:"-"`
|
||||
ShouldSkip string `orm:"-"`
|
||||
Nums int
|
||||
Langs SliceStringField `orm:"size(100)"`
|
||||
}
|
||||
|
||||
func (u *User) TableIndex() [][]string {
|
||||
|
Reference in New Issue
Block a user