orm test add extra custom JsonField
Showing
2 changed files
with
39 additions
and
3 deletions
| 1 | package orm | 1 | package orm |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | ||
| 4 | "fmt" | 5 | "fmt" |
| 5 | "os" | 6 | "os" |
| 6 | "strings" | 7 | "strings" |
| ... | @@ -11,7 +12,7 @@ import ( | ... | @@ -11,7 +12,7 @@ import ( |
| 11 | _ "github.com/mattn/go-sqlite3" | 12 | _ "github.com/mattn/go-sqlite3" |
| 12 | ) | 13 | ) |
| 13 | 14 | ||
| 14 | // A true/false field. | 15 | // A slice string field. |
| 15 | type SliceStringField []string | 16 | type SliceStringField []string |
| 16 | 17 | ||
| 17 | func (e SliceStringField) Value() []string { | 18 | func (e SliceStringField) Value() []string { |
| ... | @@ -57,10 +58,39 @@ func (e *SliceStringField) RawValue() interface{} { | ... | @@ -57,10 +58,39 @@ func (e *SliceStringField) RawValue() interface{} { |
| 57 | return e.String() | 58 | return e.String() |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | func (e *SliceStringField) Clean() error { | 61 | var _ Fielder = new(SliceStringField) |
| 62 | |||
| 63 | // A json field. | ||
| 64 | type JsonField struct { | ||
| 65 | Name string | ||
| 66 | Data string | ||
| 67 | } | ||
| 68 | |||
| 69 | func (e *JsonField) String() string { | ||
| 70 | data, _ := json.Marshal(e) | ||
| 71 | return string(data) | ||
| 72 | } | ||
| 73 | |||
| 74 | func (e *JsonField) FieldType() int { | ||
| 75 | return TypeTextField | ||
| 76 | } | ||
| 77 | |||
| 78 | func (e *JsonField) SetRaw(value interface{}) error { | ||
| 79 | switch d := value.(type) { | ||
| 80 | case string: | ||
| 81 | return json.Unmarshal([]byte(d), e) | ||
| 82 | default: | ||
| 83 | return fmt.Errorf("<JsonField.SetRaw> unknown value `%v`", value) | ||
| 84 | } | ||
| 61 | return nil | 85 | return nil |
| 62 | } | 86 | } |
| 63 | 87 | ||
| 88 | func (e *JsonField) RawValue() interface{} { | ||
| 89 | return e.String() | ||
| 90 | } | ||
| 91 | |||
| 92 | var _ Fielder = new(JsonField) | ||
| 93 | |||
| 64 | type Data struct { | 94 | type Data struct { |
| 65 | Id int | 95 | Id int |
| 66 | Boolean bool | 96 | Boolean bool |
| ... | @@ -130,6 +160,7 @@ type User struct { | ... | @@ -130,6 +160,7 @@ type User struct { |
| 130 | ShouldSkip string `orm:"-"` | 160 | ShouldSkip string `orm:"-"` |
| 131 | Nums int | 161 | Nums int |
| 132 | Langs SliceStringField `orm:"size(100)"` | 162 | Langs SliceStringField `orm:"size(100)"` |
| 163 | Extra JsonField `orm:"type(text)"` | ||
| 133 | } | 164 | } |
| 134 | 165 | ||
| 135 | func (u *User) TableIndex() [][]string { | 166 | func (u *User) TableIndex() [][]string { | ... | ... |
| ... | @@ -478,7 +478,9 @@ func TestCustomField(t *testing.T) { | ... | @@ -478,7 +478,9 @@ func TestCustomField(t *testing.T) { |
| 478 | throwFailNow(t, err) | 478 | throwFailNow(t, err) |
| 479 | 479 | ||
| 480 | user.Langs = append(user.Langs, "zh-CN", "en-US") | 480 | user.Langs = append(user.Langs, "zh-CN", "en-US") |
| 481 | _, err = dORM.Update(&user, "Langs") | 481 | user.Extra.Name = "beego" |
| 482 | user.Extra.Data = "orm" | ||
| 483 | _, err = dORM.Update(&user, "Langs", "Extra") | ||
| 482 | throwFailNow(t, err) | 484 | throwFailNow(t, err) |
| 483 | 485 | ||
| 484 | user = User{Id: 2} | 486 | user = User{Id: 2} |
| ... | @@ -487,6 +489,9 @@ func TestCustomField(t *testing.T) { | ... | @@ -487,6 +489,9 @@ func TestCustomField(t *testing.T) { |
| 487 | throwFailNow(t, AssertIs(len(user.Langs), 2)) | 489 | throwFailNow(t, AssertIs(len(user.Langs), 2)) |
| 488 | throwFailNow(t, AssertIs(user.Langs[0], "zh-CN")) | 490 | throwFailNow(t, AssertIs(user.Langs[0], "zh-CN")) |
| 489 | throwFailNow(t, AssertIs(user.Langs[1], "en-US")) | 491 | throwFailNow(t, AssertIs(user.Langs[1], "en-US")) |
| 492 | |||
| 493 | throwFailNow(t, AssertIs(user.Extra.Name, "beego")) | ||
| 494 | throwFailNow(t, AssertIs(user.Extra.Data, "orm")) | ||
| 490 | } | 495 | } |
| 491 | 496 | ||
| 492 | func TestExpr(t *testing.T) { | 497 | func TestExpr(t *testing.T) { | ... | ... |
-
Please register or sign in to post a comment