diff --git a/id/client.go b/id/client.go index c3c9a4037909daa2701f6545d4905039fcd385de..623ef05ce01e0f46cc32d96823c7c4afe3c0f012 100644 --- a/id/client.go +++ b/id/client.go @@ -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 } diff --git a/id/collection.go b/id/collection.go index 075abc849b5378d47c3307c873e27d2908b9eb2d..26057019c870685529a94a2cbc5c0b56479829b2 100644 --- a/id/collection.go +++ b/id/collection.go @@ -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 } diff --git a/id/environment.go b/id/environment.go index ec8643a382bc97db656b54dc30419ac39ec49ddc..f1bbb5407f4b67c4f2e8f71a09935857fc7faca8 100644 --- a/id/environment.go +++ b/id/environment.go @@ -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 } diff --git a/id/field.go b/id/field.go index b30eb59136c696af7cf563242c7c053ca26adc61..dbba11d79f0af879f1849a13d0dffae296d0455f 100644 --- a/id/field.go +++ b/id/field.go @@ -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 } diff --git a/id/id.go b/id/id.go index a1931febebe45c08e117c4e2a8c375337c210dd0..d333b618700a0813fe2f729cb1b06abc6f3ba5cd 100644 --- a/id/id.go +++ b/id/id.go @@ -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 +} diff --git a/id/id_test.go b/id/id_test.go index 1a72840652db54bce9451c57358e2aa109651da4..757586d87ad94c461fa74291d2bc928fb806d337 100644 --- a/id/id_test.go +++ b/id/id_test.go @@ -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()) }) } diff --git a/id/item.go b/id/item.go index 1668dd16212d82302177041b99b984ac7c288f4c..d32f0ccc1fa958af2ed40ea1c23536fd5b0ec9ff 100644 --- a/id/item.go +++ b/id/item.go @@ -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 } diff --git a/id/organization.go b/id/organization.go index 7f2ca659183118f2ed61d7beea95008605bd21e4..ed8707434e3234ff51cb5eff8a741aa9f0d10a73 100644 --- a/id/organization.go +++ b/id/organization.go @@ -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, } } diff --git a/id/revision.go b/id/revision.go index 3a111dfeaf6cb5cd76b8db192eeb801677edb655..6752e84d4bdd5873a6c173ced9e1d6c21c8c3981 100644 --- a/id/revision.go +++ b/id/revision.go @@ -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 } diff --git a/id/role.go b/id/role.go index 7080b23e741171ac4a2e8e05c31e60b2a3d88de0..b71f0f99e4f4060de8d2ec51c7a2b212e73cb03b 100644 --- a/id/role.go +++ b/id/role.go @@ -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 -//} diff --git a/id/schema.go b/id/schema.go index d77577a0f018d38b775b3542dc6d311a6c02c93f..b845b70b8a92f921496ade1159fdbe4ff4bde3f8 100644 --- a/id/schema.go +++ b/id/schema.go @@ -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 } diff --git a/id/service.go b/id/service.go index 6c3a44004008a82e1314e79150d193eb9e5c9612..dd52fc09061f7b8fe71039417aef3a11ab1c00fe 100644 --- a/id/service.go +++ b/id/service.go @@ -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, } } diff --git a/id/space.go b/id/space.go index 3d9d6c35cba03a45d1cee00d692865e0fc8a5037..aadcacc63751a731d0873473efd59c827175bc76 100644 --- a/id/space.go +++ b/id/space.go @@ -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, } } diff --git a/id/user.go b/id/user.go index 1abaa0df603a586521247f44a3c8e18d9b112cad..5422bd248c5cbd9079c32e37cb49a3e12b316df3 100644 --- a/id/user.go +++ b/id/user.go @@ -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, } }