Storage to uuids
This commit is contained in:
@@ -19,42 +19,40 @@ const (
|
||||
Modified = "modified"
|
||||
Connections = "connections"
|
||||
Attachments = "attachments"
|
||||
Location = "location"
|
||||
)
|
||||
|
||||
type One struct {
|
||||
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
|
||||
Name string `bson:"name,omitempty" json:"name,omitempty"`
|
||||
Type string `bson:"type,omitempty" json:"type,omitempty"`
|
||||
Title string `bson:"title,omitempty" json:"title,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" json:"connections,omitempty"`
|
||||
Attachments map[string]string `bson:"attachments" json:"attachments,omitempty"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func (o One) Query() One {
|
||||
return One{Name: o.Name}
|
||||
type Connection struct {
|
||||
Relationship string `bson:"relationship,omitempty" json:"relationship"`
|
||||
}
|
||||
|
||||
func (o One) Peer() One {
|
||||
return One{
|
||||
Name: o.Name,
|
||||
Type: o.Type,
|
||||
Title: o.Title,
|
||||
Relationship: o.Relationship,
|
||||
Modified: o.Modified,
|
||||
}
|
||||
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 {
|
||||
names := make([]string, len(o.Connections))
|
||||
ids := make([]string, len(o.Connections))
|
||||
i := 0
|
||||
for k := range o.Connections {
|
||||
names[i] = o.Connections[k].Name
|
||||
ids[i] = k
|
||||
i += 1
|
||||
}
|
||||
return names
|
||||
return ids
|
||||
}
|
||||
|
||||
func (o One) MarshalBSON() ([]byte, error) {
|
||||
@@ -62,6 +60,12 @@ func (o One) MarshalBSON() ([]byte, error) {
|
||||
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)
|
||||
}
|
||||
b, err := json.Marshal(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -76,36 +80,5 @@ func (o One) MarshalBSON() ([]byte, error) {
|
||||
m[k] = strings.TrimSpace(v.(string))
|
||||
}
|
||||
}
|
||||
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])
|
||||
}
|
||||
delete(connections, "")
|
||||
for k := range connections {
|
||||
if k == "" {
|
||||
continue
|
||||
}
|
||||
if o.Connections[k].Name == "" {
|
||||
p := o.Connections[k]
|
||||
p.Name = k
|
||||
o.Connections[k] = p
|
||||
}
|
||||
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
|
||||
}
|
||||
m[Connections] = connections
|
||||
}
|
||||
return bson.Marshal(m)
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import (
|
||||
|
||||
func TestOne(t *testing.T) {
|
||||
one := One{
|
||||
Name: "myname",
|
||||
ID: "myname",
|
||||
Type: "mytype",
|
||||
}
|
||||
q := one.Query()
|
||||
if want := fmt.Sprint(One{Name: one.Name}); want != fmt.Sprint(q) {
|
||||
if want := fmt.Sprint(bson.M{ID: one.ID}); want != fmt.Sprint(q) {
|
||||
t.Error(want, q)
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func TestOne(t *testing.T) {
|
||||
func TestOneMarshalBSON(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
sameAsQuery bool
|
||||
one One
|
||||
one interface{}
|
||||
}{
|
||||
"query no modified change": {
|
||||
sameAsQuery: true,
|
||||
@@ -31,18 +31,16 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||
one: One{Name: "hello", Type: "world", Modified: 1},
|
||||
},
|
||||
"w/ connections": {
|
||||
one: One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]One{"hi": One{Name: "hi", Relationship: "mom"}}},
|
||||
one: One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]Connection{"hi": Connection{Relationship: "mom"}}},
|
||||
},
|
||||
"w/ attachments": {
|
||||
one: One{Name: "hello", Type: "world", Modified: 1, Attachments: map[string]string{"hello": "/world"}},
|
||||
one: One{Name: "hello", Type: "world", Modified: 1, Attachments: map[string]Attachment{"hello": Attachment{"/world"}}},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
c := d
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var bm bson.Marshaler = c.one
|
||||
t.Log(bm)
|
||||
b, err := bson.Marshal(c.one)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -51,85 +49,11 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||
if err := bson.Unmarshal(b, &one); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.sameAsQuery && (fmt.Sprint(one) != fmt.Sprint(one.Query()) || fmt.Sprint(one) != fmt.Sprint(c.one)) {
|
||||
t.Error(c.sameAsQuery, c.one, one)
|
||||
} else if !c.sameAsQuery {
|
||||
if c.one.Modified == one.Modified {
|
||||
t.Error(c.one.Modified, one.Modified)
|
||||
}
|
||||
c.one.Modified = 0
|
||||
one.Modified = 0
|
||||
for k := range one.Connections {
|
||||
temp := one.Connections[k]
|
||||
temp.Modified = 0
|
||||
one.Connections[k] = temp
|
||||
}
|
||||
if fmt.Sprint(c.one) != fmt.Sprint(one) {
|
||||
t.Error(c.one, one)
|
||||
if !c.sameAsQuery {
|
||||
if one.Modified < 2 {
|
||||
t.Error(one.Modified)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneMarshalBSONBadConnections(t *testing.T) {
|
||||
t.Run("connections has an empty string for a key that should die", func(t *testing.T) {
|
||||
input := One{Name: "hello", Connections: map[string]One{"": One{Name: "teehee"}}}
|
||||
|
||||
b, err := bson.Marshal(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
output := One{}
|
||||
if err := bson.Unmarshal(b, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(output.Connections) != 0 {
|
||||
t.Fatal(output.Connections)
|
||||
}
|
||||
|
||||
input.Connections = nil
|
||||
output.Connections = nil
|
||||
input.Modified = 0
|
||||
output.Modified = 0
|
||||
|
||||
if fmt.Sprint(input) != fmt.Sprint(output) {
|
||||
t.Fatal(input, output)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("connections has a key but empty name that should correct", func(t *testing.T) {
|
||||
input := One{Name: "hello", Connections: map[string]One{"teehee": One{Name: ""}}}
|
||||
|
||||
b, err := bson.Marshal(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
output := One{}
|
||||
if err := bson.Unmarshal(b, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(output.Connections) != 1 {
|
||||
t.Fatal(output.Connections)
|
||||
} else {
|
||||
for k := range output.Connections {
|
||||
if k != output.Connections[k].Name {
|
||||
t.Fatal(k, output.Connections)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
input.Connections = nil
|
||||
output.Connections = nil
|
||||
input.Modified = 0
|
||||
output.Modified = 0
|
||||
|
||||
if fmt.Sprint(input) != fmt.Sprint(output) {
|
||||
t.Fatal(input, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user