generated from alecodes/base-template
feat: implement platform handler creation
also refactor public and internal api to support transaction between multiple methods #2
This commit is contained in:
parent
b2d8dadcee
commit
01086d12c9
7 changed files with 423 additions and 253 deletions
47
pkg/node.go
47
pkg/node.go
|
|
@ -4,46 +4,22 @@ package synchronizator
|
|||
// to provide the ability to parse the database node into a user defined
|
||||
// struct that fulfills it's requirements.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// type Library struct {
|
||||
// Name string `json:"name"`
|
||||
// Category string `json:"category"`
|
||||
// Metadata map[string]interface{} `json:"metadata"`
|
||||
// }
|
||||
//
|
||||
// func (library *Library) ToNode() (string, string, []byte, error) {
|
||||
// metadata, err := json.Marshal(library.Metadata)
|
||||
// if err != nil {
|
||||
// return "", "", nil, err
|
||||
// }
|
||||
// return "LIBRARY", library.Name, metadata, nil
|
||||
// }
|
||||
//
|
||||
// func (library *Library) FromNode(_class string, name string, metadata []byte) error {
|
||||
// if _class != "LIBRARY" {
|
||||
// return fmt.Errorf("invalid class %s", _class)
|
||||
// }
|
||||
// if err := json.Unmarshal(metadata, &library.Metadata); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// library.Name = name
|
||||
// return nil
|
||||
// }
|
||||
type NodeClass interface {
|
||||
// This interface is compatible with the [NodeClass] interface but it doesn't
|
||||
// handle the class parameter as it's a static value provided by the
|
||||
// StandardNode
|
||||
type StandardNode interface {
|
||||
// How to transform the struct into a node. It needs to return the class,
|
||||
// name and a []byte representation of the metadata.
|
||||
//
|
||||
// - class: Is used for classification and query pourposes. It's recomended to provide a constante string to increase consistency.
|
||||
// - name: A user friendly name
|
||||
// - metadata: Arbitrary data. This will be stored as a jsonb in the database
|
||||
//
|
||||
ToNode() (string, string, []byte, error)
|
||||
ToNode() (string, []byte, error)
|
||||
|
||||
// How to transform a node into the struct. This method should modify the
|
||||
// struct directly as it receives a pointer.
|
||||
//
|
||||
// - class: Is used for classification and query pourposes.
|
||||
// - class: The class of the node, should not be modified to avoid inconsistencies.
|
||||
// - name: A user friendly name
|
||||
// - metadata: Arbitrary data. This is stored as a jsonb in the database
|
||||
FromNode(string, string, []byte) error
|
||||
|
|
@ -60,11 +36,11 @@ type Node struct {
|
|||
metadata []byte // Arbitrary data. This is stored as a jsonb in the database
|
||||
}
|
||||
|
||||
// Creates a new relation of type RelationshipClass to the node with the
|
||||
// Creates a new relation of type StandardRelationship to the node with the
|
||||
// provided id. An error is returned if the relation already exists.
|
||||
//
|
||||
// This method is a wrapper around the AddRelation method of the connection.
|
||||
func (node *Node) AddRelation(relation RelationshipClass, to int64) (*Relationship, error) {
|
||||
func (node *Node) AddRelation(relation StandardRelationship, to int64) (*Relationship, error) {
|
||||
return node._conn.AddRelation(node.Id, relation, to)
|
||||
}
|
||||
|
||||
|
|
@ -145,6 +121,11 @@ func (node *Node) Delete() error {
|
|||
// if err := node.Unmarshall(data); err != nil {
|
||||
// println(err)
|
||||
// }
|
||||
func (node *Node) Unmarshall(dst NodeClass) error {
|
||||
func (node *Node) Unmarshall(dst StandardNode) error {
|
||||
return dst.FromNode(node._class, node.name, node.metadata)
|
||||
}
|
||||
|
||||
// Returns the class of the node
|
||||
func (relationship *Relationship) GetClass() string {
|
||||
return relationship._class
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue