LastPossum / kamino

Golang library for clone things
MIT License
46 stars 1 forks source link

Deep copy of value is skipped when the map entry key is zero value. #4

Open aazimkhan19 opened 2 weeks ago

aazimkhan19 commented 2 weeks ago

I have a map field in a structure that might contain an entry with an empty key (zero value). The clone function skips recursive deep copying if a map entry's key is zero. Is there any reason for this check !k.IsZero()?

for iter.Next() {
    k := iter.Key()
    // if key needs to be copied, copy it recursively
    if !keyIsBasic && !k.IsZero() {
        newK.Set(k)
        if err := cloneNested(ctx, newK); err != nil {
            return err
        }
        k = newK
    }
    v := iter.Value()
    // if value needs to be copied, copy it recursively
    if !valueIsBasic && !k.IsZero() {
        newV.Set(v)
        if err := cloneNested(ctx, newV); err != nil {
        return err
        }
        v = newV
    }
    // put key and value to a map copy
    res.SetMapIndex(k, v)
}
LastPossum commented 1 week ago

Hi! The check has a simple reason - if your key is zero, you do not need to deep copy it; you can put the zero key into the new map directly.

However, if you have some bugs because of the library, you can send me some additional information or even a test case.