Home > Backend Development > Golang > How to Model Embedded Nodes of Different Types in MongoDB with mgo using Interfaces?

How to Model Embedded Nodes of Different Types in MongoDB with mgo using Interfaces?

DDD
Release: 2024-10-26 10:34:29
Original
855 people have browsed it

How to Model Embedded Nodes of Different Types in MongoDB with mgo using Interfaces?

Interface Types as Models in mgo (Go)

Problem:

In a scenario involving workflows and embedded nodes of various types, using an interface for modeling nodes in MongoDB with mgo results in an error. The error occurs because mgo cannot unmarshal the embedded Node documents without type information.

Solution:

To overcome this limitation, consider defining a struct to hold both the Node type and the associated type information:

<code class="go">type NodeWithType struct {
   Node Node `bson:"-"`
   Type string
}</code>
Copy after login

Within the Workflow struct, use an array of NodeWithType structs to store the nodes:

<code class="go">type Workflow struct {
   CreatedAt time.Time
   StartedAt time.Time
   CreatedBy string
   Nodes []NodeWithType
}</code>
Copy after login

To decode the data correctly, implement the SetBSON function on NodeWithType:

<code class="go">func (nt *NodeWithType) SetBSON(r bson.Raw) error {
   // Decode the type string
   typeStr := r.String()

   // Create a new Node value based on the type string
   switch typeStr {
   case "EmailNode":
      nt.Node = &EmailNode{}
   case "TwitterNode":
      nt.Node = &TwitterNode{}
   default:
      return errors.New("Unknown node type")
   }

   // Unmarshal the remaining data to the Node value
   bsonBytes, err := bson.Marshal(r.Body)
   if err != nil {
      return err
   }
   return bson.Unmarshal(bsonBytes, nt.Node)
}</code>
Copy after login

This approach allows mgo to unmarshal the embedded nodes correctly, based on the type information stored in the NodeWithType struct.

The above is the detailed content of How to Model Embedded Nodes of Different Types in MongoDB with mgo using Interfaces?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template