元帅变成了一个bson.Raw [英] Marshal into a bson.Raw

查看:143
本文介绍了元帅变成了一个bson.Raw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 gopkg.in/mgo.v2/bson ,我想知道如何编组一个接口{}值到类型 bson.Raw 的值中。



> bson.Raw states:


使用这种类型可以解组或者 marshal em> values。

但是我找不到一个Marshal函数,它会返回 bson.Raw



我缺少什么?

要做:

  package main 

import(
fmt

gopkg.in/mgo.v2/bson


func main(){
//如何避免MarshalRaw帮助功能?
raw,err:= MarshalRaw(Hello world)
if err!= nil {
panic(err)
}

fmt.Printf (%+ v \ n,raw)
}

func MarshalRaw(v interface {})(* bson.Raw,error){
bin,err: = bson.Marshal(struct {Raw interface {}} {v})
if err!= nil {
return nil,err
}

var raw struct {Raw bson.Raw}
err = bson.Unmarshal(bin,& raw)
if err!= nil {
return nil,err
}

return& raw.Raw,nil
}

输出:


& {种类:2数据:[12 0 0 0 72 101 108 108 111 32 119 111 114 108 100 0]}



解决方案

bson.Raw 在编组和解组时都被用作一个值。



code> interface {} 转换为 bso n.Raw ,第一件要做的事情就是整理它,这样你就可以得到表示正在编组的所有文档数据:

  var value interface {} = bson.M {some:value} 
data,err:= bson.Marshal(value)
if err!=无{
log.Fatal(err)
}

然后它可以将一个或多个字段解组为 bson.Raw 值:

  var doc struct {Some bson.Raw} 
err = bson.Unmarshal(data,& doc)
if err!= nil {
log.Fatal(err)
}

甚至整个文件:

<$ p $如果错误!=零(
log.Fatal(err)$),
$ var bson.Raw
err = bson.Unmarshal(data,& doc)
b



如果你想要整个文档而不仅仅是一个字段,你也可以使用这个快捷键:

  doc:= bson.Raw {3,data} 

3常数表示<一个href =http://bsonspec.org =nofollow> bson规范,它当然必须符合提供的数据。由于BSON仅支持顶级文档,因此我们知道这一定是正确的。


Using gopkg.in/mgo.v2/bson, I wonder how to marshal an interface{} value into a value of type bson.Raw.

The documentation for bson.Raw states:

Using this type it is possible to unmarshal or marshal values partially.

But I can't find a Marshal function that would return bson.Raw.

What am I missing?

Example of what I try to do:

package main

import (
    "fmt"

    "gopkg.in/mgo.v2/bson"
)

func main() {
    // How to avoid a MarshalRaw help function?
    raw, err := MarshalRaw("Hello world")
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v\n", raw)
}

func MarshalRaw(v interface{}) (*bson.Raw, error) {
    bin, err := bson.Marshal(struct{ Raw interface{} }{v})
    if err != nil {
        return nil, err
    }

    var raw struct{ Raw bson.Raw }
    err = bson.Unmarshal(bin, &raw)
    if err != nil {
        return nil, err
    }

    return &raw.Raw, nil
}

Output:

&{Kind:2 Data:[12 0 0 0 72 101 108 108 111 32 119 111 114 108 100 0]}

解决方案

bson.Raw is used as a value both when marshaling and unmarshaling.

To transform an interface{} into a bson.Raw, the first thing to do is to marshal it so that you get the plain document data that represents whatever is being marshaled:

    var value interface{} = bson.M{"some": "value"}
    data, err := bson.Marshal(value)
    if err != nil {
            log.Fatal(err)
    }

and then it may have one or more fields unmarshaled into bson.Raw values:

    var doc struct{ Some bson.Raw }
    err = bson.Unmarshal(data, &doc)
    if err != nil {
            log.Fatal(err)
    }

or even the entire document:

    var doc bson.Raw
    err = bson.Unmarshal(data, &doc)
    if err != nil {
            log.Fatal(err)
    }

If you want the entire document rather than just a field, you can also use this shortcut:

    doc := bson.Raw{3, data}

The 3 constant represents a document in the bson specification, and it must of course match the provided data. Since BSON only supports documents at the top level, we know this must be correct.

这篇关于元帅变成了一个bson.Raw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆