ostafen / clover

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

Unmarshalling with embedded structs is failing #151

Closed badarsebard closed 1 month ago

badarsebard commented 1 month ago

This is a similar issue to what was happening in #142. Tags aren't being managed properly when using embedded structs.

Example from internal/encoding_test.go: When the Base struct tag is changed from the empty string (such as _id), it fails to be managed correctly with the current version and the ID is lost when calling Convert. This causes the TestNormalize test to fail at line79.

type BaseModel struct {
    ID string `clover:"_id"`
}

type TestStruct struct {
    BaseModel
    IntField    int                    `clover:"int,omitempty"`
    UintField   uint                   `clover:"uint,omitempty"`
    StringField string                 `clover:",omitempty"`
    FloatField  float32                `clover:",omitempty"`
    BoolField   bool                   `clover:",omitempty"`
    TimeField   time.Time              `clover:",omitempty"`
    IntPtr      *int                   `clover:",omitempty"`
    SliceField  []int                  `clover:",omitempty"`
    MapField    map[string]interface{} `clover:",omitempty"`
    Data        []byte                 `clover:",omitempty"`
}

Adding json tags also fails.

The issue is due to how the createRenameMap function iterates through the fields of the struct. I am submitting a PR that adds a recursive call to the function when the field type is anonymous so the fields of embedded structs will be included.

badarsebard commented 1 month ago

Fixed with #152