changed public id format to be 128 bit length.

This commit is contained in:
didier amyot committed 2015-07-05 18:16:40 -04:00
1 parent 78d50d9a6d
commit d2cf9d7498
11 files changed
+187 -22

No files matched your search

+4 -9
View File
@@ -17,11 +17,6 @@ type DoormanValue struct {
Probability *big.Rat `json:"probability"`
}
type doormanValue struct {
Name string
Probability string
}
type DoormanId struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id"`
Name string `json:"name" bson:"name"`
@@ -35,10 +30,10 @@ func (wId *DoormanId) AsJson() string {
}
type DoormanDefinition struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id"`
Name string `json:"name" bson:"name"`
Values []*DoormanValue `json:"values" bson:"values"`
OwnerEmails []string `json:"emails" bson:"emails"`
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Values []*DoormanValue `bson:"values"`
OwnerEmails []string `bson:"emails"`
}
func NewDoormanDefinition(name string, ownerEmails ...string) *DoormanDefinition {
+64
View File
@@ -1,10 +1,17 @@
package doormen
import (
"encoding/json"
"github.com/didiercrunch/doormanserver/shared"
"gopkg.in/mgo.v2/bson"
"math/big"
)
type doormanValue struct {
Name string
Probability string
}
func (val *DoormanValue) GetBSON() (interface{}, error) {
ret := &doormanValue{val.Name, ""}
txt, err := val.Probability.MarshalText()
@@ -24,3 +31,60 @@ func (val *DoormanValue) SetBSON(raw bson.Raw) error {
val.Probability = new(big.Rat)
return val.Probability.UnmarshalText([]byte(v.Probability))
}
type jsonDoormanDefinition struct {
Id string `json:"id,omitempty"`
Name string `json:"name"`
Values []*DoormanValue `json:"values"`
OwnerEmails []string `json:"emails"`
}
func (dm *DoormanDefinition) MarshalJSON() ([]byte, error) {
jdm := new(jsonDoormanDefinition)
jdm.Name = dm.Name
jdm.OwnerEmails = dm.OwnerEmails
jdm.Values = dm.Values
jdm.Id = shared.ObjectIdToPublicId(dm.Id)
return json.Marshal(jdm)
}
func (dm *DoormanDefinition) UnmarshalJSON(data []byte) error {
jsm := new(jsonDoormanDefinition)
var err error
if err = json.Unmarshal(data, jsm); err != nil {
return err
}
if dm.Id, err = shared.PublicIdToObjectId(jsm.Id); err != nil {
return err
}
dm.Name = jsm.Name
dm.Values = jsm.Values
dm.OwnerEmails = jsm.OwnerEmails
return nil
}
type jsonDoormanId struct {
Name string `json:"name"`
Id string `json:"id"`
}
func (did *DoormanId) MarshalJSON() ([]byte, error) {
jdid := new(jsonDoormanId)
jdid.Name = did.Name
jdid.Id = shared.ObjectIdToPublicId(did.Id)
return json.Marshal(jdid)
}
func (did *DoormanId) UnmarshalJSON(data []byte) error {
jdid := new(jsonDoormanId)
if err := json.Unmarshal(data, jdid); err != nil {
return err
}
var err error
if did.Id, err = shared.PublicIdToObjectId(jdid.Id); err != nil {
return err
}
did.Name = jdid.Name
return nil
}
+53
View File
@@ -1,6 +1,7 @@
package doormen
import (
"encoding/base64"
"encoding/json"
"gopkg.in/mgo.v2/bson"
"reflect"
@@ -22,6 +23,42 @@ func TestJsonEncoding(t *testing.T) {
}
}
func TestJsonEncodingWithEmptyIds(t *testing.T) {
dmd := QuickNewDoormanDefinition("test json", "1/4", "3/4")
dmd.Id = ""
data, err := json.Marshal(dmd)
if err != nil {
t.Error(err)
}
dmd2 := new(DoormanDefinition)
if err = json.Unmarshal(data, dmd2); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(dmd2, dmd) {
t.Error()
}
}
func TestJsonIdFormatToBeCompatibleWithSiphashKey(t *testing.T) {
dmd := QuickNewDoormanDefinition("test json", "1/4", "3/4")
data, err := json.Marshal(dmd)
if err != nil {
t.Error(err)
return
}
m := make(map[string]interface{})
if err = json.Unmarshal(data, &m); err != nil {
t.Error(err)
return
}
if b, err := base64.URLEncoding.DecodeString(m["id"].(string)); err != nil {
t.Error(err)
return
} else if len(b) != 16 {
t.Error("siphash requires a 16 bytes (128 bits) key not ", len(b))
}
}
func TestBsonEncoding(t *testing.T) {
dmd := QuickNewDoormanDefinition("test bson", "1/4", "3/4")
data, err := bson.Marshal(dmd)
@@ -36,3 +73,19 @@ func TestBsonEncoding(t *testing.T) {
t.Error()
}
}
func TestDoormanIdJsonEncoding(t *testing.T) {
did := &DoormanId{bson.NewObjectId(), "hahaha"}
data, err := json.Marshal(did)
if err != nil {
t.Error(err)
}
did2 := new(DoormanId)
if err = json.Unmarshal(data, did2); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(did2, did) {
t.Error()
}
}