upper/db — pull request #50 by elsonwu (2014-11-25 08:40:42+00:00)
Title: Make struct field to work with db.Marshaler and db.Unmarshaler

Now db.Marshaler and db.Unmarshaler does not work with the struct field, for example:

```
type UserProfile struct {
Gender string `json:"gender"`
}

func (self *UserProfile) MarshalDB() (interface{}, error) {
js, err := json.Marshal(self)
if err != nil {
return nil, err
}

return string(js), nil
}

func (self *UserProfile) UnmarshalDB(data interface{}) error {
if d, ok := data.(string); ok {
return json.Unmarshal([]byte(d), self)
}

return errors.New("unknow data")
}

type User struct {
Id string `db:"id"`
Name string `db:"name"`
Profile UserProfile `db:"profile"` // or can try with *UserProfile
}

func main() {

var settings = db.Settings{
Database: "",
}

sess, err := db.Open(sqlite.Adapter, settings)
if err != nil {
fmt.Println(err)
return
}

coll, err := sess.Collection("users")
if err != nil {
fmt.Println(err)
return
}

user := User{
Id: "1",
Name: "Elson Wu",
}

user.Profile.Gender = "male"

r, err := coll.Append(user)
fmt.Println(r, err)

res := coll.Find()

var u User
for nil == res.Next(&u) {
fmt.Printf("%#v, %#v\n", u, u.Profile)
}

res.Close()
}
```