为Bson.M mongodb创建自定义mashler / unmashler时出错

阿诺德·埃温(Arnold Ewin):

我得到的错误WriteValueBytes can only write while positioned on a Element or Value but is positioned on a TopLevel尝试创建bson.M定制mashler / unmashler时

我有一个名为TransactionId的自定义类型,它表示一个UUID,我想在存储到monbodb之前将此值转换为字符串,并在从mongodb中提取该值时也将其从字符串转换回。

这是我到目前为止的代码

package main

import (
    "github.com/google/uuid"
    "github.com/pkg/errors"
    "go.mongodb.org/mongo-driver/bson/bsontype"
    "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    "go.mongodb.org/mongo-driver/bson"
    "log"
)

// TransactionID is a UUID used to trace a batch of work which is being processed.
type TransactionID uuid.UUID

// String returns the transaction id as a string
func (id TransactionID) String() (result string, err error) {
    val, err := uuid.FromBytes(id[:])
    if err != nil {
        return result, errors.Wrapf(err, "cannot convert transaction ID %s to UUID", string(id[:]))
    }
    return val.String(), nil
}

func (id TransactionID) MarshalBSONValue() (bsontype.Type, []byte, error) {
    idString, err :=  id.String()
    if err != nil {
        return bsontype.String, nil, err
    }
    return bsontype.String, bsoncore.AppendString(nil, idString), nil
}


func (id *TransactionID) UnmarshalBSONValue(bsonType bsontype.Type, bytes []byte) error {
    uid, err := uuid.FromBytes(bytes)
    if err != nil {
        return err
    }

    *id = TransactionID(uid)
    return nil
}


func NewTransactionID() TransactionID {
    return TransactionID(uuid.New())
}


func main() {
    id := NewTransactionID()

    _, err :=  bson.Marshal(id)
    if err != nil {
        log.Fatal(err)
    }
}

我正在WriteValueBytes can only write while positioned on a Element or Value but is positioned on a TopLevel采取非军事步骤。

链接:https//play.golang.org/p/_n7VpX-KIyP

Wan Bachtiar:

我得到WriteValueBytes只能在定位在元素或值上时写入,而在解组步骤中定位在TopLevel上。

函数bson.Marshal()需要一个可以转换为文档(即interface{})的参数。这就是为什么错误消息与值的位置有关。也就是说,您不能在文档的顶层使用字符串,它必须是文档的元素。如果需要封送单个值,则应改用bson.MarshalValue()

id := NewTransactionID()
vtype, vdata, err := bson.MarshalValue(id)

bson.Marshal()下面是一个示例(添加到您的示例中):

type Foo struct {
    ID TransactionID
}

func main() {
    id := NewTransactionID()
    foo, err :=  bson.Marshal(&Foo{ID:id})
    if err != nil {
        panic(err)
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Magento 1.9 中为自定义模块创建自定义表时出错

创建自定义变量时出错

Mongodb Bson类型为Json

为GKE中的容器使用“ monitoring_v3”创建自定义指标时出错

BSON错误为MongoDB创建新的BsonDocument

为Material-UI创建自定义主题时颜色属性在哪里定义

为NSTableview创建自定义类时,代表为零吗?

Wix自定义操作-会话为空,延迟操作时出错

使用自定义键创建KTable时出错

创建 SharePoint 自定义页面布局 2016 时出错

在 tensorflow 中创建自定义层时出错

创建自定义seaborn颜色图时出错

创建自定义插件Vuejs + Typescript时出错

从自定义模块创建发票时出错(Odoo 13)

从自定义模型创建用户时出错

尝试创建/更新自定义表单时出错

在Spring JPA中为mongodb运行自定义删除查询时,如何利用Pageable?

在Google DataProc创建时无法为Jupyter和Zeppelin自定义端口

为Xamarin Android创建自定义控制器时出现问题

当绑定安全性为TransportCredentialOnly时,在WCF REST中创建自定义IIdentity

创建自定义RecyclerView项目分隔符时为空类

在 Android M 的 AOSP 代码中的开发者选项中添加自定义 EditTextPreference 时出错

为RideIntent创建自定义CLPlacemark

为数组创建自定义类型

为TextInputLayout创建自定义类

为自定义模块创建bean

为自定义UIControl创建ControlProperty

为UIColor创建自定义颜色

在 Kubernetes 上为 mongodb 创建 StatefulSet 时出错