ostafen / clover

A lightweight document-oriented NoSQL database written in pure Golang.
MIT License
666 stars 55 forks source link

fix: anonymous field will not be Unmarshalled to struct #83

Closed garfeng closed 2 years ago

garfeng commented 2 years ago
type BasicModel struct {
    ID string `clover:"_id" json:"_id"`
}

type User struct {
    BasicModel
    Name string `clover:"name" json:"name"`
}

func TestAnonymousField(t *testing.T) {
    user := &User{
        Name: "Alice",
    }
    user.ID = "UUUU-UUUU-UUUU"

    doc := clover.NewDocumentOf(user)

    docMap := map[string]interface{}{}
    doc.Unmarshal(&docMap)

    buff, _ := json.MarshalIndent(docMap, "", "  ")

    fmt.Println(string(buff))
    /*
        {
          "BasicModel": {
            "_id": "UUUU-UUUU-UUUU"
          },
          "name": "Alice"
        }
    */

    user2 := &User{}

    doc.Unmarshal(user2)

    fmt.Println(user2.ID) // "", empty
}

With my commit, the buff will be like :

{
  "_id": "UUUU-UUUU-UUUU",
  "name": "Alice"
}
garfeng commented 2 years ago

I will add it soon. @ostafen