jmattheis / goverter

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

Structure conversion error #52

Closed shuqingzai closed 1 year ago

shuqingzai commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

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

Expected behavior A clear and concise description of what you expected to happen.

time.go

import "github.com/golang-module/carbon/v2"

type In struct {
    CreatedAt  carbon.DateTime
    ReleasedAt *carbon.DateTime
}

type Out struct {
    CreatedAt  carbon.DateTime
    ReleasedAt *carbon.DateTime
}

// goverter:converter
type Converter interface {
    ModelToDomain(source *In) (*Out, error)
}
  1. If the unexported field is ignored, panic: runtime error: invalid memory address or nil pointer dereference

    go run github.com/jmattheis/goverter/cmd/goverter --ignoreUnexportedFields --wrapErrors ./time.go
  2. If unexported fields are not ignored, an error field mismatch will be reported

    go run github.com/jmattheis/goverter/cmd/goverter  --wrapErrors ./time.go
jmattheis commented 1 year ago

Goverter doesn't know how to deeply clone carbon.DateTime so, you've to manually define the method. As far as I can see, carbon.DateTime is immutable and every call method call on it will return a new instance. Due to this, you can just pass the object through without doing any deep cloning, because you don't have to clone a value that is immutable.

// goverter:converter
// goverter:extend CloneDateTime
type Converter interface {
    Convert(*In) *Out
}

type In struct {
    CreatedAt  carbon.DateTime
    ReleasedAt *carbon.DateTime
}

type Out struct {
    CreatedAt  carbon.DateTime
    ReleasedAt *carbon.DateTime
}

func CloneDateTime(source carbon.DateTime) carbon.DateTime {
    return source
}
shuqingzai commented 1 year ago

Thanks