go-automapper使用time.Time字段

huangapple go评论87阅读模式
英文:

go-automapper using time.Time field

问题

我正在使用go-automapper将数据库字段的值复制到POST请求的主体中。这两个实例是相同类型的:

type MessageDTO struct {
    CreationDate time.Time `bson:"creationDate" json:"creationDate,omitempty"`
}

在某个时候,我尝试从一个实例复制到另一个实例:

func entityToDTO(entity models.MessageDTO) models.MessageDTO {
    dto := &models.MessageDTO{}
    automapper.Map(entity, dto)
    return *dto
}

但是在time.Time值上失败了:

Error mapping field: CreationDate. DestType: models.MessageDTO .
SourceType: models.MessageDTO. Error: Error mapping field: wall.
DestType: time.Time. SourceType: time.Time. Error: reflect:
reflect.Value.Set using value obtained using unexported field

有没有办法让它工作?

英文:

I'm using go-automapper to copy values from db fields to a body post request. Both instances are the same type:

type MessageDTO struct {
	CreationDate time.Time          `bson:"creationDate" json:"creationDate,omitempty"`
}

at some point I tried to copy from one instance to another:

func entityToDTO(entity models.MessageDTO) models.MessageDTO{
	dto := &models.MessageDTO{}
	automapper.Map(entity, dto)
	return *dto
}

but it fails in the time.Time value:

> Error mapping field: CreationDate. DestType: models.MessageDTO .
> SourceType: models.MessageDTO. Error: Error mapping field: wall.
> DestType: time.Time. SourceType: time.Time. Error: reflect:
> reflect.Value.Set using value obtained using unexported field

Is there a way to make it work?

答案1

得分: 1

失败原因在错误消息中提到:

> 错误:映射字段时出错:wall ...
> 错误:reflect: reflect.Value.Set 使用使用未导出字段获取的值

参考 time.Time 的源代码:

type Time struct {
	// wall 和 ext 编码了墙上的时间秒数、墙上的时间纳秒数,
	// ...
	wall uint64
	ext  int64

	// loc 指定应该使用的位置
	// ...
	loc *Location
}

go-automapper 的文档中指出:

> 未导出/非公共的值将不会被映射。
>
> 这是一个设计决策,当目标中无法映射字段时会引发 panic,以确保源或目标中的重命名字段不会导致微妙的静默错误。

所以我认为没有直接的方法可以获取它。也许你可以考虑将时间数据复制到 string 或其他可以被 go-automapper 映射的类型中,然后使用像 (t *Time) UnmarshalBinary (t *Time) UnmarshalText 这样的接口将其转换为 time.Time

英文:

The fail reason is mentioned in the error message:

> Error: Error mapping field: wall ...
> Error: reflect: reflect.Value.Set using value obtained using unexported field

Refering to the source code of time.Time:

type Time struct {
	// wall and ext encode the wall time seconds, wall time nanoseconds,
	// ...
	wall uint64
	ext  int64

	// loc specifies the Location that should be used to
	// ...
	loc *Location
}

And the document of go-automapper states:

> Values that are not exported/not public will not be mapped.
>
> It is a design decision to panic when a field cannot be mapped in the
> destination to ensure that a renamed field in either the source or
> destination does not result in subtle silent bug.

So I think there is no direct way to get it. Maybe you could consider to copy the time data in string or other types that can be Mapped by go-automapper, then use interfaces like (t *Time) UnmarshalBinary (t *Time) UnmarshalText to convert to a time.Time.

huangapple
  • 本文由 发表于 2021年10月20日 12:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/69640316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定