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, …Run Code Online (Sandbox Code Playgroud)