Change connections storage from arr to map
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -22,14 +21,14 @@ const (
|
||||
)
|
||||
|
||||
type One struct {
|
||||
Name string `bson:"_id,omitempty" json:"name,omitempty"`
|
||||
Type string `bson:"type,omitempty" json:"type,omitempty"`
|
||||
Title string `bson:"title,omitempty" json:"title,omitempty"`
|
||||
Image string `bson:"image,omitempty" json:"image,omitempty"`
|
||||
Text string `bson:"text,omitempty" json:"text,omitempty"`
|
||||
Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"`
|
||||
Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"`
|
||||
Connections []One `bson:"connections,omitempty" json:"connections,omitempty"`
|
||||
Name string `bson:"_id,omitempty" json:"name,omitempty"`
|
||||
Type string `bson:"type,omitempty" json:"type,omitempty"`
|
||||
Title string `bson:"title,omitempty" json:"title,omitempty"`
|
||||
Image string `bson:"image,omitempty" json:"image,omitempty"`
|
||||
Text string `bson:"text,omitempty" json:"text,omitempty"`
|
||||
Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"`
|
||||
Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"`
|
||||
Connections map[string]One `bson:"connections,omitempty" json:"connections,omitempty"`
|
||||
}
|
||||
|
||||
func (o One) Query() One {
|
||||
@@ -38,14 +37,17 @@ func (o One) Query() One {
|
||||
|
||||
func (o One) Peers() []string {
|
||||
names := make([]string, len(o.Connections))
|
||||
for i := range o.Connections {
|
||||
names[i] = o.Connections[i].Name
|
||||
i := 0
|
||||
for k := range o.Connections {
|
||||
names[i] = o.Connections[k].Name
|
||||
i += 1
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func (o One) MarshalBSON() ([]byte, error) {
|
||||
if fmt.Sprint(o) != fmt.Sprint(o.Query()) {
|
||||
isMin := fmt.Sprint(o) == fmt.Sprint(o.Query())
|
||||
if !isMin {
|
||||
o.Modified = time.Now().UnixNano()
|
||||
}
|
||||
b, err := json.Marshal(o)
|
||||
@@ -60,32 +62,27 @@ func (o One) MarshalBSON() ([]byte, error) {
|
||||
m[Name] = name
|
||||
delete(m, JSONName)
|
||||
}
|
||||
var connections primitive.A
|
||||
switch m[Connections].(type) {
|
||||
case nil:
|
||||
case []interface{}:
|
||||
connections = primitive.A(m[Connections].([]interface{}))
|
||||
case primitive.A:
|
||||
connections = m[Connections].(primitive.A)
|
||||
default:
|
||||
return nil, fmt.Errorf("bad type for %q: %T", Connections, m[Connections])
|
||||
}
|
||||
curL := len(connections)
|
||||
wantL := len(o.Connections)
|
||||
for i := curL; i < wantL; i++ {
|
||||
connections = append(connections, nil)
|
||||
}
|
||||
for i, connection := range o.Connections {
|
||||
b, err := bson.Marshal(connection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if !isMin {
|
||||
connections := map[string]interface{}{}
|
||||
switch m[Connections].(type) {
|
||||
case nil:
|
||||
case map[string]interface{}:
|
||||
connections = m[Connections].(map[string]interface{})
|
||||
default:
|
||||
return nil, fmt.Errorf("bad connections type %T", m[Connections])
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := bson.Unmarshal(b, &m); err != nil {
|
||||
return nil, err
|
||||
for k := range connections {
|
||||
b, err := bson.Marshal(o.Connections[k])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := bson.Unmarshal(b, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connections[k] = m
|
||||
}
|
||||
connections[i] = m
|
||||
m[Connections] = connections
|
||||
}
|
||||
m[Connections] = connections
|
||||
return bson.Marshal(m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user