generated from alecodes/base-template
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package synchronizator
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type default_collection struct {
|
|
platform_name string
|
|
}
|
|
|
|
func (collection *default_collection) ToNode() (string, []byte, error) {
|
|
platform_name := strings.ToUpper(collection.platform_name)
|
|
return platform_name + "_DEFAULT", nil, nil
|
|
}
|
|
|
|
func (collection *default_collection) FromNode(_class string, name string, metadata []byte) error {
|
|
if _class != "DEFAULT" {
|
|
return fmt.Errorf("invalid class %s", _class)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Utility struct to represent a collection of nodes, it's a [Node] itself so all
|
|
// the node's functionality is available.
|
|
type Collection struct {
|
|
Node // Underlaying node info
|
|
childs []*Node // Child nodes
|
|
}
|
|
|
|
// NewCollectionObject creates a new Collection instance without persisting it to the database.
|
|
// This is useful when you want to prepare a Collection for later storage.
|
|
func NewCollection(name string, metadata []byte) *Collection {
|
|
return &Collection{
|
|
Node: Node{
|
|
name: name,
|
|
_class: "COLLECTION",
|
|
metadata: metadata,
|
|
Id: -1, // Use -1 to indicate not persisted
|
|
},
|
|
childs: make([]*Node, 0),
|
|
}
|
|
}
|
|
|
|
// Internal RelationshipClass to handle the collection to node relationship
|
|
type collection_relation struct{}
|
|
|
|
func (collection *collection_relation) ToRelationship() (string, []byte, error) {
|
|
return "COLLECTION_HAS", nil, nil
|
|
}
|
|
|
|
func (collection *collection_relation) FromRelationship(_class string, metadata []byte) error {
|
|
if _class != "COLLECTION_HAS" {
|
|
return fmt.Errorf("invalid class %s", _class)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Adds a new child to this collection. Use the underlaying node's AddRelation
|
|
// method.
|
|
func (collection *Collection) AddChild(node *Node) error {
|
|
_, err := collection.AddRelation(&collection_relation{}, node.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
collection.childs = append(collection.childs, node)
|
|
|
|
return nil
|
|
}
|