5

假设您有一个由多个不同类型的嵌入式节点组成的工作流。由于节点的类型不同,我想在这里使用 Golang 接口并想出了以下内容:

type Workflow struct {
   CreatedAt time.Time
   StartedAt time.Time
   CreatedBy string
   Nodes []Node
}

type Node interface {
  Exec() (int, error)
}

type EmailNode struct {
   From string
   To string
   Subject string
   Body string
}

type TwitterNode struct {
   Tweet string
   Image []byte
}

func (n *EmailNode) Exec() (int, error){
   //send email
   return 0, nil
}

func (n *TwitterNode) Exec() (int, error) {
   //send tweet
   return 0, nil
}

这些工作流存储在 MongoDB 中,我在其中有示例种子数据。使用 mgo,当我尝试查找工作流(给定其 ID)时:

w = &Workflow{}
collection.FindID(bson.ObjectIdHex(id)).One(w)

我收到错误 - bson.M 类型的值不可分配给 Node 类型。

我也觉得有点奇怪,mgo 如何在没有任何类型信息的情况下将嵌入的 Node 文档解组到 Go 结构中。可能我需要从另一个角度看问题。

任何建议将不胜感激。

4

2 回答 2

8

由于您指出的原因,您不能在文档中使用接口。解码器没有关于要创建的类型的信息。

处理此问题的一种方法是定义一个结构来保存类型信息:

type NodeWithType struct {
   Node Node `bson:"-"`
   Type string
}

type Workflow struct {
   CreatedAt time.Time
   StartedAt time.Time
   CreatedBy string
   Nodes []NodeWithType
}

在此类型上实现 SetBSON 函数。此函数应解码类型字符串,根据该字符串创建一个正确类型的值并解组该值。

func (nt *NodeWithType) SetBSON(r bson.Raw) error {
}
于 2014-09-25T14:08:12.233 回答
5

按照 Simon Fox 关于实施的回答SetBSON,这里有一个更准确的答案。

让我们看一下原始代码:

type Workflow struct {
   CreatedAt time.Time
   StartedAt time.Time
   CreatedBy string
   Nodes     []Node
}

type Node interface {
  Exec() (int, error)
}

type EmailNode struct {
   From    string
   To      string
   Subject string
   Body    string
}

type TwitterNode struct {
   Tweet string
   Image []byte
}

func (n *EmailNode) Exec() (int, error){
   //send email
   return 0, nil
}

func (n *TwitterNode) Exec() (int, error) {
   //send tweet
   return 0, nil
}

您现在要做的是:一旦您从 Mongo 解组 BSON 对象,您希望能够知道每个节点是 anEmailNode还是 a TwitterNode

当您将节点存储为Node接口时,mgo 无法知道要实现什么结构,因此您必须明确告诉它。来了SetBSON

在您的示例中,问题来自 this Workflow.Nodes,这是Node接口的一部分。因为它是一个简单的切片,所以最好创建一个自定义类型,当解组 BSON 时,mgo 将能够调用它:

type NodesList []Node

// The updated Workflow struct:
type Workflow struct {
    CreatedAt time.Time
    StartedAt time.Time
    CreatedBy string
    Nodes     NodesList
}

现在,您可以实现SetBSONNodesList并描述它是如何工作的。请注意,当您使用指针时,您可以定义变量中包含的内容:

// Note that you must use a pointer to the slice
func (list *NodesList) SetBSON(raw raw.BSON) error {
    // Now you need to create the objects according to your
    // own domain logic and what's contained inside "raw":
    if raw.something {
        *list = append(*list, &TwitterNode{})
    } else {
        *list = append(*list, &EmailNode{})
    }

    return nil
}
于 2017-10-28T12:24:14.157 回答