Skip to content
Snippets Groups Projects
Commit 2d9030bc authored by ko_oler's avatar ko_oler
Browse files

добавлен метод FromMap для типа ID

parent 82a75787
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ func (t *ClientID) String() string {
func (t *ClientID) ToMap() map[string]any {
m := t.SpaceID.ToMap()
m["client_id"] = t.ClientID
m["type"] = Client
return m
}
......
......@@ -19,6 +19,7 @@ func (t *CollectionID) String() string {
func (t *CollectionID) ToMap() map[string]any {
m := t.EnvironmentID.ToMap()
m["col_id"] = t.CollectionID
m["type"] = Collection
return m
}
......
......@@ -20,6 +20,7 @@ func (t *EnvironmentID) String() string {
func (t *EnvironmentID) ToMap() map[string]any {
m := t.SpaceID.ToMap()
m["env_id"] = t.EnvironmentID
m["type"] = Environment
return m
}
......
......@@ -20,6 +20,7 @@ func (t *FieldID) String() string {
func (t *FieldID) ToMap() map[string]any {
m := t.ItemID.ToMap()
m["field_name"] = t.FieldName
m["type"] = Field
return m
}
......
......@@ -92,3 +92,40 @@ func Join(parts ...string) string {
}
return s
}
func (id *ID) FromMap(m map[string]any) error {
if m == nil {
return errors.New("nil map")
}
switch m["type"] {
case Organization:
id.Descriptor = new(OrganizationID)
case Service:
id.Descriptor = new(ServiceID)
case User:
id.Descriptor = new(UserID)
case Space:
id.Descriptor = new(SpaceID)
case Environment:
id.Descriptor = new(EnvironmentID)
case Client:
id.Descriptor = new(ClientID)
case Role:
id.Descriptor = new(RoleID)
case Collection:
id.Descriptor = new(CollectionID)
case Schema:
id.Descriptor = new(SchemaID)
case Item:
id.Descriptor = new(ItemID)
case Revision:
id.Descriptor = new(RevisionID)
case Field:
id.Descriptor = new(FieldID)
default:
return errors.New("type of ID not specified in map")
}
_ = id.Descriptor.FromMap(m)
return nil
}
......@@ -282,9 +282,9 @@ func Test_Map(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := tt.id
v := new(ID)
_ = v.FromMap(tt.id.ToMap())
require.Equal(t, tt.id, v)
require.Equal(t, tt.id, v, "проверка FromMap для типа ID, должен быть равен исходному значению")
require.Equal(t, v.ToMap(), tt.id.ToMap())
})
}
......
......@@ -20,6 +20,7 @@ func (t *ItemID) String() string {
func (t *ItemID) ToMap() map[string]any {
m := t.CollectionID.ToMap()
m["item_id"] = t.ItemID
m["type"] = Item
return m
}
......
......@@ -18,6 +18,7 @@ func (t *OrganizationID) String() string {
func (t *OrganizationID) ToMap() map[string]any {
return map[string]any{
"organization_id": t.OrganizationID,
"type": Organization,
}
}
......
......@@ -20,6 +20,7 @@ func (t *RevisionID) String() string {
func (t *RevisionID) ToMap() map[string]any {
m := t.ItemID.ToMap()
m["rev_id"] = t.RevisionID
m["type"] = Revision
return m
}
......
......@@ -20,6 +20,7 @@ func (t *RoleID) String() string {
func (t *RoleID) ToMap() map[string]any {
m := t.SpaceID.ToMap()
m["role_id"] = t.RoleID
m["type"] = Role
return m
}
......@@ -54,22 +55,3 @@ func parseRoleID(parts []string) (*RoleID, error) {
id.RoleID = parts[3]
return &id, nil
}
//
//func (t *RoleID) UnmarshalJSON(b []byte) error {
// var data string
// if err := jsoniter.Unmarshal(b, &data); err != nil {
// return err
// }
// knownID, err := Parse(data)
// if err != nil {
// return err
// }
// t.SpaceID = knownID.(*RoleID).SpaceID
// t.RoleID = knownID.(*RoleID).RoleID
// return nil
//}
//
//func (t *RoleID) MarshalJSON() ([]byte, error) {
// return []byte(t.String()), nil
//}
......@@ -7,7 +7,7 @@ const (
type SchemaID struct {
EnvironmentID
CollectionID string `json:"collection_id"`
CollectionID string `json:"col_id"`
}
func (t *SchemaID) Type() string { return Schema }
......@@ -18,7 +18,8 @@ func (t *SchemaID) String() string {
func (t *SchemaID) ToMap() map[string]any {
m := t.EnvironmentID.ToMap()
m["collection_id"] = t.CollectionID
m["col_id"] = t.CollectionID
m["type"] = Schema
return m
}
......@@ -26,7 +27,7 @@ func (t *SchemaID) FromMap(m map[string]any) error {
if err := t.EnvironmentID.FromMap(m); err != nil {
return err
}
t.CollectionID = m["collection_id"].(string)
t.CollectionID = m["col_id"].(string)
return nil
}
......
......@@ -18,6 +18,7 @@ func (t *ServiceID) String() string {
func (t *ServiceID) ToMap() map[string]any {
return map[string]any{
"service_id": t.ServiceID,
"type": Service,
}
}
......
......@@ -18,6 +18,7 @@ func (t *SpaceID) String() string {
func (t *SpaceID) ToMap() map[string]any {
return map[string]any{
"space_id": t.SpaceID,
"type": Space,
}
}
......
......@@ -18,6 +18,7 @@ func (t *UserID) String() string {
func (t *UserID) ToMap() map[string]any {
return map[string]any{
"user_id": t.UserID,
"type": User,
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment