Support primary key ID and unique key Name where api uses name

This commit is contained in:
breel
2020-08-02 09:59:47 -06:00
parent 37fe9415e7
commit 8e6e86955e
14 changed files with 68 additions and 49 deletions

View File

@@ -106,18 +106,18 @@ func (mp *Map) Insert(_ context.Context, namespace string, doc interface{}) erro
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
if _, ok := m[entity.Name]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name)
} else if _, ok := m[entity.Name].(string); !ok {
if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
if _, ok := mp.db[namespace][m[entity.Name].(string)]; ok {
if _, ok := mp.db[namespace][m[entity.ID].(string)]; ok {
return errors.New("cannot insert: collision on primary key")
}
if _, ok := mp.db[namespace]; !ok {
mp.db[namespace] = make(map[string][]byte)
}
mp.db[namespace][m[entity.Name].(string)] = b
mp.db[namespace][m[entity.ID].(string)] = b
return nil
}