使用 mgo.Marshal() 编组指针

我想对指针进行与值不同的编码。目前,如果我们有一个结构体:


type Order struct {

    Item           Tool

    AssociatedItem *Tool

}

编组时,两者都被内联到 mongo 中的 Order 文档中。在 *Tool 的情况下,我需要能够执行我自己的序列化。例如,在这种情况下,我可以只存储 Too 的 Id 而不是整个内容。不幸的是,mgo 中的覆盖机制是为工具定义 SetBSON() GetBSON 但它不区分指针和非指针。


处理这个问题的最佳方法是什么?


杨魅力
浏览 269回答 1
1回答

青春有我

对“指针”使用不同的类型,例如:type SelectiveTool Toolfunc (st *SelectiveTool) SetBSON(raw bson.Raw) error {    return raw.Unmarshal(s)}type Order struct {    Item           Tool    AssociatedItem *SelectiveTool}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go