123 lines
3.1 KiB
Go
123 lines
3.1 KiB
Go
package entity
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
const (
|
|
ID = "_id"
|
|
Name = "name"
|
|
Relationship = "relationship"
|
|
Type = "type"
|
|
Title = "title"
|
|
Text = "text"
|
|
Modified = "modified"
|
|
Connections = "connections"
|
|
Attachments = "attachments"
|
|
Location = "location"
|
|
)
|
|
|
|
type One struct {
|
|
ID string `bson:"_id,omitempty" json:"_id"`
|
|
Name string `bson:"name,omitempty" json:"name"`
|
|
Type string `bson:"type,omitempty" json:"type"`
|
|
Title string `bson:"title,omitempty" json:"title"`
|
|
Text string `bson:"text,omitempty" json:"text"`
|
|
Modified int64 `bson:"modified,omitempty" json:"modified"`
|
|
Connections map[string]Connection `bson:"connections" json:"connections"`
|
|
Attachments map[string]Attachment `bson:"attachments" json:"attachments"`
|
|
}
|
|
|
|
type Connection struct {
|
|
Relationship string `bson:"relationship,omitempty" json:"relationship"`
|
|
}
|
|
|
|
type Attachment struct {
|
|
Location string `bson:"location,omitempty" json:"location"`
|
|
}
|
|
|
|
func (o One) Query() bson.M {
|
|
return bson.M{ID: o.ID}
|
|
}
|
|
|
|
func (o One) Peers() []string {
|
|
ids := make([]string, len(o.Connections))
|
|
i := 0
|
|
for k := range o.Connections {
|
|
ids[i] = k
|
|
i += 1
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func (o One) Generic() bson.M {
|
|
b, _ := bson.Marshal(o)
|
|
var m bson.M
|
|
bson.Unmarshal(b, &m)
|
|
return m
|
|
}
|
|
|
|
func (o One) MarshalJSON() ([]byte, error) {
|
|
b, err := o.MarshalBSON()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
v := bson.M{}
|
|
if err := bson.Unmarshal(b, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func (o One) MarshalBSON() ([]byte, error) {
|
|
isMin := fmt.Sprint(o) == fmt.Sprint(o.Query())
|
|
if !isMin {
|
|
o.Modified = time.Now().UnixNano()
|
|
}
|
|
if o.Connections == nil {
|
|
o.Connections = make(map[string]Connection)
|
|
}
|
|
if o.Attachments == nil {
|
|
o.Attachments = make(map[string]Attachment)
|
|
}
|
|
var v interface{}
|
|
b, err := o.toBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fromBytes(b, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return bson.Marshal(v)
|
|
}
|
|
|
|
func (o One) toBytes() ([]byte, error) {
|
|
return json.Marshal(struct {
|
|
ID string `bson:"_id,omitempty" json:"_id"`
|
|
Name string `bson:"name,omitempty" json:"name"`
|
|
Type string `bson:"type,omitempty" json:"type"`
|
|
Title string `bson:"title,omitempty" json:"title"`
|
|
Text string `bson:"text,omitempty" json:"text"`
|
|
Modified int64 `bson:"modified,omitempty" json:"modified"`
|
|
Connections map[string]Connection `bson:"connections" json:"connections"`
|
|
Attachments map[string]Attachment `bson:"attachments" json:"attachments"`
|
|
}{
|
|
ID: o.ID,
|
|
Name: o.Name,
|
|
Type: o.Type,
|
|
Title: o.Title,
|
|
Text: o.Text,
|
|
Modified: o.Modified,
|
|
Connections: o.Connections,
|
|
Attachments: o.Attachments,
|
|
})
|
|
}
|
|
|
|
func fromBytes(b []byte, v interface{}) error {
|
|
return json.Unmarshal(b, v)
|
|
}
|