Commit 690d77e9 authored by slene's avatar slene

orm test add extra custom JsonField

parent bcc8f606
package orm package orm
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
...@@ -11,7 +12,7 @@ import ( ...@@ -11,7 +12,7 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
// A true/false field. // A slice string field.
type SliceStringField []string type SliceStringField []string
func (e SliceStringField) Value() []string { func (e SliceStringField) Value() []string {
...@@ -57,10 +58,39 @@ func (e *SliceStringField) RawValue() interface{} { ...@@ -57,10 +58,39 @@ func (e *SliceStringField) RawValue() interface{} {
return e.String() return e.String()
} }
func (e *SliceStringField) Clean() error { var _ Fielder = new(SliceStringField)
// A json field.
type JsonField struct {
Name string
Data string
}
func (e *JsonField) String() string {
data, _ := json.Marshal(e)
return string(data)
}
func (e *JsonField) FieldType() int {
return TypeTextField
}
func (e *JsonField) SetRaw(value interface{}) error {
switch d := value.(type) {
case string:
return json.Unmarshal([]byte(d), e)
default:
return fmt.Errorf("<JsonField.SetRaw> unknown value `%v`", value)
}
return nil return nil
} }
func (e *JsonField) RawValue() interface{} {
return e.String()
}
var _ Fielder = new(JsonField)
type Data struct { type Data struct {
Id int Id int
Boolean bool Boolean bool
...@@ -130,6 +160,7 @@ type User struct { ...@@ -130,6 +160,7 @@ type User struct {
ShouldSkip string `orm:"-"` ShouldSkip string `orm:"-"`
Nums int Nums int
Langs SliceStringField `orm:"size(100)"` Langs SliceStringField `orm:"size(100)"`
Extra JsonField `orm:"type(text)"`
} }
func (u *User) TableIndex() [][]string { func (u *User) TableIndex() [][]string {
......
...@@ -478,7 +478,9 @@ func TestCustomField(t *testing.T) { ...@@ -478,7 +478,9 @@ func TestCustomField(t *testing.T) {
throwFailNow(t, err) throwFailNow(t, err)
user.Langs = append(user.Langs, "zh-CN", "en-US") user.Langs = append(user.Langs, "zh-CN", "en-US")
_, err = dORM.Update(&user, "Langs") user.Extra.Name = "beego"
user.Extra.Data = "orm"
_, err = dORM.Update(&user, "Langs", "Extra")
throwFailNow(t, err) throwFailNow(t, err)
user = User{Id: 2} user = User{Id: 2}
...@@ -487,6 +489,9 @@ func TestCustomField(t *testing.T) { ...@@ -487,6 +489,9 @@ func TestCustomField(t *testing.T) {
throwFailNow(t, AssertIs(len(user.Langs), 2)) throwFailNow(t, AssertIs(len(user.Langs), 2))
throwFailNow(t, AssertIs(user.Langs[0], "zh-CN")) throwFailNow(t, AssertIs(user.Langs[0], "zh-CN"))
throwFailNow(t, AssertIs(user.Langs[1], "en-US")) throwFailNow(t, AssertIs(user.Langs[1], "en-US"))
throwFailNow(t, AssertIs(user.Extra.Name, "beego"))
throwFailNow(t, AssertIs(user.Extra.Data, "orm"))
} }
func TestExpr(t *testing.T) { func TestExpr(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment