jmattheis / goverter

Generate type-safe Go converters by simply defining an interface
https://goverter.jmattheis.de/
MIT License
469 stars 44 forks source link

Bug when convert one field to many field #151

Closed newcworld closed 1 day ago

newcworld commented 1 week ago

Describe the bug

When convert from one field to two or more field, it will only use the first map function

To Reproduce Include the input file that goverter has problems with, and describe the steps you took to trigger the bug

// goverter:converter
// goverter:matchIgnoreCase
// goverter:useZeroValueOnPointerInconsistency
// goverter:extend PointId
// goverter:extend Name
// goverter:extend TimeToTimestamp
// goverter:enum no
type Converter interface {
    ConvertSearchPoints([]*qdrant.ScoredPoint) []*ggpb.LightSearchItem
    // goverter:ignoreUnexported
    // goverter:map Payload Keyword
    // goverter:map Payload Image
    // goverter:ignore Index Tags
    ConvertSearchPoint(*qdrant.ScoredPoint) *ggpb.LightSearchItem
}

func PointId(b *qdrant.PointId) int64 {
    return int64(b.GetNum())
}

func Name(b map[string]*qdrant.Value) string {
    return b["name"].GetStringValue()
}

func Image(b map[string]*qdrant.Value) string {
    return b["image"].GetStringValue()
}

func (c *ConverterImpl) ConvertSearchPoint(source *qdrant.ScoredPoint) *ggpb.LightSearchItem {
    var pGgpbLightSearchItem *ggpb.LightSearchItem
    if source != nil {
        var ggpbLightSearchItem ggpb.LightSearchItem
        ggpbLightSearchItem.Id = convert.PointId((*source).Id)
        ggpbLightSearchItem.Keyword = convert.Name((*source).Payload)
        ggpbLightSearchItem.Image = convert.Name((*source).Payload)
        pGgpbLightSearchItem = &ggpbLightSearchItem
    }
    return pGgpbLightSearchItem
}

Expected behavior map one field to many field in diff methods


func (c *ConverterImpl) ConvertSearchPoint(source *qdrant.ScoredPoint) *ggpb.LightSearchItem {
    var pGgpbLightSearchItem *ggpb.LightSearchItem
    if source != nil {
        var ggpbLightSearchItem ggpb.LightSearchItem
        ggpbLightSearchItem.Id = convert.PointId((*source).Id)
        ggpbLightSearchItem.Keyword = convert.Name((*source).Payload)
        ggpbLightSearchItem.Image = convert.Image((*source).Payload)
        pGgpbLightSearchItem = &ggpbLightSearchItem
    }
    return pGgpbLightSearchItem
}
jmattheis commented 1 week ago

Add the expected output you want to get, I don't understand what you are trying to achieve.

newcworld commented 1 week ago

func (c *ConverterImpl) ConvertSearchPoint(source *qdrant.ScoredPoint) *ggpb.LightSearchItem {
    var pGgpbLightSearchItem *ggpb.LightSearchItem
    if source != nil {
        var ggpbLightSearchItem ggpb.LightSearchItem
        ggpbLightSearchItem.Id = convert.PointId((*source).Id)
        ggpbLightSearchItem.Keyword = convert.Name((*source).Payload)
+       ggpbLightSearchItem.Image = convert.Image((*source).Payload)
        pGgpbLightSearchItem = &ggpbLightSearchItem
    }
    return pGgpbLightSearchItem
}
jmattheis commented 1 week ago

Have you tried map [SOURCE-PATH] TARGET | METHOD?:

// goverter:converter
// goverter:matchIgnoreCase
// goverter:useZeroValueOnPointerInconsistency
// goverter:extend PointId
// goverter:extend TimeToTimestamp
// goverter:enum no
type Converter interface {
    ConvertSearchPoints([]*qdrant.ScoredPoint) []*ggpb.LightSearchItem
    // goverter:ignoreUnexported
    // goverter:map Payload Keyword | Name
    // goverter:map Payload Image | Image
    // goverter:ignore Index Tags
    ConvertSearchPoint(*qdrant.ScoredPoint) *ggpb.LightSearchItem
}