2

我正在努力用 mongo-go-driver 对具有多态结构的 bson 数组进行编组/解组。我计划将一个结构鉴别器保存到编组的数据中,并编写一个自定义的 UnmarshalBSONValue 函数来根据结构鉴别器对其进行解码。但我不知道如何正确地做到这一点。

package polymorphism

import (
    "fmt"
    "testing"

    "code.byted.org/gopkg/pkg/testing/assert"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/bsontype"
)

type INode interface {
    GetName() string
}

type TypedNode struct {
    NodeClass string
}

type Node struct {
    TypedNode `bson:"inline"`
    Name      string
    Children  INodeList
}

func (n *Node) GetName() string {
    return n.Name
}

type INodeList []INode

func (l *INodeList) UnmarshalBSONValue(t bsontype.Type, data []byte) error {
    fmt.Println("INodeList.UnmarshalBSONValue")

    var arr []bson.Raw                // 1. First, try to decode data as []bson.Raw
    err := bson.Unmarshal(data, &arr) // error: cannot decode document into []bson.Raw
    if err != nil {
        fmt.Println(err)
        return err
    }
    for _, item := range arr { // 2. Then, try to decode each bson.Raw as concrete Node according to `nodeclass`
        class := item.Lookup("nodeclass").StringValue()
        fmt.Printf("class: %v\n", class)
        if class == "SubNode1" {
            bin, err := bson.Marshal(item)
            if err != nil {
                return err
            }
            var sub1 SubNode1
            err = bson.Unmarshal(bin, &sub1)
            if err != nil {
                return err
            }
            *l = append(*l, &sub1)
        } else if class == "SubNode2" {
            //...
        }
    }
    return nil
}

type SubNode1 struct {
    *Node     `bson:"inline"`
    FirstName string
    LastName  string
}

type SubNode2 struct {
    *Node `bson:"inline"`
    Extra string
}

使用上面的代码,我试图将INodeList数据解码为[]bson.Raw,然后bson.Raw根据nodeclass. 但它报告错误:

cannot decode document into []bson.Raw

在线

err := bson.Unmarshal(data, &arr).

那么,如何正确地做到这一点呢?

4

1 回答 1

2

您需要传递 abson.Raw的指针bson.Unmarshal(data, &arr),然后将其值切片到原始值数组,例如:

func (l *INodeList) UnmarshalBSONValue(t bsontype.Type, data []byte) error {
    fmt.Println("INodeList.UnmarshalBSONValue")

    var raw bson.Raw // 1. First, try to decode data as bson.Raw
    err := bson.Unmarshal(data, &raw)
    if err != nil {
        fmt.Println(err)
        return err
    }

    // Slice the raw document to an array of valid raw values
    rawNodes, err := raw.Values()
    if err != nil {
        return err
    }

    // 2. Then, try to decode each bson.Raw as concrete Node according to `nodeclass`
    for _, rawNode := range rawNodes {
        // Convert the raw node to a raw document in order to access its "nodeclass" field
        d, ok := rawNode.DocumentOK()
        if !ok {
            return fmt.Errorf("raw node can't be converted to doc")
        }
        class := d.Lookup("nodeclass").StringValue()

        // Decode the node's raw doc to the corresponding struct
        var node INode
        switch class {
        case "SubNode1":
            node = &SubNode1{}
        case "SubNode2":
            node = &SubNode2{}
        //...
        default:
            // ...
        }

        bson.Unmarshal(d, node)
        *l = append(*l, node)
    }

    return nil
}

请注意,Note.Children必须是 aINodeList的指针,并且内联字段必须是结构或映射(不是指针):


type Node struct {
    TypedNode `bson:",inline"`
    Name      string
    Children  *INodeList
}

type SubNode1 struct {
    Node      `bson:",inline"`
    FirstName string
    LastName  string
}

type SubNode2 struct {
    Node  `bson:",inline"`
    Extra string
}


于 2020-01-05T17:47:09.113 回答