我想对指针进行与值不同的编码。目前,如果我们有一个结构体:type Order struct { Item Tool AssociatedItem *Tool}编组时,两者都被内联到 mongo 中的 Order 文档中。在 *Tool 的情况下,我需要能够执行我自己的序列化。例如,在这种情况下,我可以只存储 Too 的 Id 而不是整个内容。不幸的是,mgo 中的覆盖机制是为工具定义 SetBSON() GetBSON 但它不区分指针和非指针。处理这个问题的最佳方法是什么?
1 回答
青春有我
TA贡献1784条经验 获得超8个赞
对“指针”使用不同的类型,例如:
type SelectiveTool Tool
func (st *SelectiveTool) SetBSON(raw bson.Raw) error {
return raw.Unmarshal(s)
}
type Order struct {
Item Tool
AssociatedItem *SelectiveTool
}
- 1 回答
- 0 关注
- 266 浏览
添加回答
举报
0/150
提交
取消
