go-mongox icon indicating copy to clipboard operation
go-mongox copied to clipboard

Updater 序列化错误,导致数据无法更新正确

Open shuqingzai opened this issue 1 month ago • 1 comments

在提交之前请先查找 已有 issues,避免重复上报。| Before submitting, please search for existing issues to avoid duplicate reports.

并且确保自己已经 | Also, ensure that you have:

  • [x] 阅读过文档 | Read the documentation
  • [x] 阅读过注释 | Reviewed the comments
  • [x] 阅读过例子 | Looked at the examples

您使用的 mongox 版本 | The version of mongox you are using

github.com/chenmingyong0423/go-mongox/v2

您遇到的问题 | The issue you encountered.

  1. 在更新操作时,会进行一次预先序列化为 bson.M ,这是出于什么考虑? https://github.com/chenmingyong0423/go-mongox/blob/19e6f49a2546ed9cbe01d12a605a0e15bf937784/updater/updater.go#L135-L138
    1. 它引发了如下问题
      1. 使用默认的编解码器,无法对自定义类型进行编解码,导致数据无法正确序列化 https://github.com/chenmingyong0423/go-mongox/blob/19e6f49a2546ed9cbe01d12a605a0e15bf937784/bsonx/bsonx.go#L78-L91
      2. 多一次的序列化对于追求性能的场景,会有所影响
    2. 即使不进行预先序列化,它可以正常运行
  2. 如下,两个字段应该是有效的时间结构体,需要使用自定义的编解码器才能正确序列化,但是由于预先序列化,导致它变成不可用的数据
Image Image Image

正确的如下:

Image

复现步骤 | Steps to reproduce> 请提供简单的复现代码 | Please provide simple code to reproduce the issue.

错误日志或者截图 | Error logs or Screenshots

你排查的结果,或者你觉得可行的修复方案 | Your Findings or Possible Solutions

可选。我们希望你能够尽量先排查问题,帮助我们减轻维护负担。这对于你个人能力提升同样是有帮助的。| Optional. We hope you can try to troubleshoot the issue first, helping us to reduce our maintenance burden. This is also beneficial for your personal skill development

您期望的结果 | Expected Outcome

你设置的的 Go 环境 | Your Go Environment Setting

上传 go env 的结果 | Upload the result of go env

shuqingzai avatar Nov 05 '25 01:11 shuqingzai

在更新操作时,会进行一次预先序列化为 bson.M ,这是出于什么考虑?

转成 bson.M 是为了方便注入 id, createdAt 等时间字段的值,如果符合规则,就会自动填充 ID,创建和更新时间字段。

看起来问题出现在了 dToM 函数,只需要优化一下这个函数,能触发结构体底层的自定义序列化方法,就能解决问题。方便提供一下代码中涉及到的 时间结构体 carbon.Carbon 吗,如果可以,顺便带上自定义的序列化方法。

chenmingyong0423 avatar Nov 05 '25 08:11 chenmingyong0423