diff --git a/pkg/id/id.go b/id/id.go similarity index 100% rename from pkg/id/id.go rename to id/id.go diff --git a/pkg/id/id_test.go b/id/id_test.go similarity index 100% rename from pkg/id/id_test.go rename to id/id_test.go diff --git a/id/object_id_test.go b/id/object_id_test.go deleted file mode 100644 index 56a2c104e000756640eefc978be025c01757d13b..0000000000000000000000000000000000000000 --- a/id/object_id_test.go +++ /dev/null @@ -1,265 +0,0 @@ -package id - -import ( - "testing" - - "git.perx.ru/perxis/perxis-go/pkg/items" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func Test_ParseID(t *testing.T) { - tests := []struct { - name string - id any - result *ObjectId - wantError bool - }{ - { - name: "SpaceId", - id: "/spaces/<space_id>", - result: MustObjectId("/spaces/<space_id>"), - }, - { - name: "ServiceID", - id: "/services/<service_id>", - result: MustObjectId("/services/<service_id>"), - }, - { - name: "UserID", - id: "/users/<user_id>", - result: MustObjectId("/users/<user_id>"), - }, - { - name: "OrganizationID", - id: "/orgs/<org_id>", - result: MustObjectId("/orgs/<org_id>"), - }, - { - name: "ClientID", - id: "/spaces/<space_id>/clients/<client_id>", - result: MustObjectId("/spaces/<space_id>/clients/<client_id>"), - }, - { - name: "RoleID", - id: "/spaces/<space_id>/roles/<role_id>", - result: MustObjectId("/spaces/<space_id>/roles/<role_id>"), - }, - { - name: "LocaleID", - id: "/spaces/<space_id>/locales/<locale_id>", - result: MustObjectId("/spaces/<space_id>/locales/<locale_id>"), - }, - { - name: "EnvironmentID", - id: "/spaces/<space_id>/envs/<env_id>", - result: MustObjectId("/spaces/<space_id>/envs/<env_id>"), - }, - { - name: "CollectionId", - id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>", - result: MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>"), - }, - { - name: "SchemaID", - id: "/spaces/<space_id>/envs/<env_id>/schema/<collection_id>", - result: MustObjectId("/spaces/<space_id>/envs/<env_id>/schema/<collection_id>"), - }, - { - name: "ItemId", - id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>", - result: MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>"), - }, - { - name: "RevisionID", - id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/revs/<rev_id>", - result: MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/revs/<rev_id>"), - }, - { - name: "FieldId", - id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/fields/<field_name>", - result: MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/fields/<field_name>"), - }, - { - name: "With error #1: no backslash in the beginning of id", - id: "spaces/<space_id>", - result: nil, - wantError: true, - }, - { - name: "With error #2: backslash in the end of id", - id: "/spaces/<space_id>/", - result: nil, - wantError: true, - }, - { - name: "With error #3: typo in 'spaces'", - id: "/space/<space_id>", - result: nil, - wantError: true, - }, - { - name: "With error #4: no space_id in id", - id: "/spaces", - result: nil, - wantError: true, - }, - { - name: "With error #5: multiple backslashes in the end of id", - id: "/spaces/<space_id>///", - result: nil, - wantError: true, - }, - { - name: "With error #6: nil value", - id: nil, - wantError: true, - }, - { - name: "With error #7: nil object value", - id: (*items.Item)(nil), - wantError: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - id, err := NewObjectId(tt.id) - if tt.wantError { - require.Error(t, err) - return - } - require.NoError(t, err) - require.Equal(t, tt.result, id) - require.Equal(t, tt.id, id.String()) - }) - } -} - -func Test_Map(t *testing.T) { - tests := []struct { - name string - id *ObjectId - }{ - { - name: "ServiceID", - id: &ObjectId{Descriptor: &ServiceId{ServiceID: "<service_id>"}}, - }, - { - name: "UserID", - id: &ObjectId{Descriptor: &UserId{UserID: "<user_id>"}}, - }, - { - name: "OrganizationID", - id: &ObjectId{Descriptor: &OrganizationId{OrganizationID: "<org_id>"}}, - }, - { - name: "SpaceId", - id: &ObjectId{Descriptor: &SpaceId{SpaceID: "<space_id>"}}, - }, - { - name: "ClientID", - id: &ObjectId{Descriptor: &ClientId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - ClientID: "<client_id>", - }}, - }, - { - name: "RoleID", - id: &ObjectId{Descriptor: &RoleId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - RoleID: "<role_id>", - }}, - }, - { - name: "LocaleID", - id: &ObjectId{Descriptor: &LocaleID{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - LocaleID: "<locale_id>", - }}, - }, - { - name: "EnvironmentID", - id: &ObjectId{Descriptor: &EnvironmentId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - EnvironmentID: "<env_id>", - }}, - }, - { - name: "CollectionId", - id: &ObjectId{Descriptor: &CollectionId{ - EnvironmentId: EnvironmentId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - EnvironmentID: "<env_id>", - }, - CollectionID: "<collection_id>", - }}, - }, - { - name: "Schema ID", - id: &ObjectId{Descriptor: &SchemaId{ - EnvironmentId: EnvironmentId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - EnvironmentID: "<env_id>", - }, - CollectionID: "<collection_id>", - }}, - }, - { - name: "ItemId", - id: &ObjectId{Descriptor: &ItemId{ - CollectionId: CollectionId{ - EnvironmentId: EnvironmentId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - EnvironmentID: "<env_id>", - }, - CollectionID: "<collection_id>", - }, - ItemID: "<item_id>", - }}, - }, - { - name: "RevisionID", - id: &ObjectId{Descriptor: &RevisionId{ - ItemId: ItemId{ - CollectionId: CollectionId{ - EnvironmentId: EnvironmentId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - EnvironmentID: "<env_id>", - }, - CollectionID: "<collection_id>", - }, - ItemID: "<item_id>", - }, - RevisionID: "<rev_id>", - }}, - }, - { - name: "FieldId", - id: &ObjectId{Descriptor: &FieldId{ - ItemId: ItemId{ - CollectionId: CollectionId{ - EnvironmentId: EnvironmentId{ - SpaceId: SpaceId{SpaceID: "<space_id>"}, - EnvironmentID: "<env_id>", - }, - CollectionID: "<collection_id>", - }, - ItemID: "<item_id>", - }, - Field: "<field_name>", - }}, - }, - { - name: "SystemID", - id: &ObjectId{Descriptor: &SystemId{}}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - v, err := FromMap(tt.id.Map()) - require.NoError(t, err) - assert.Equal(t, tt.id, v, "проверка FromMap для типа ID, должен быть равен исходному значению") - assert.Equal(t, v.Map(), tt.id.Map()) - }) - } -} diff --git a/id/test/object_id_test.go b/id/test/object_id_test.go index e29328ddf25f799cbb56707fa292762f69c39207..2f01fa125e376623ceb710bbe594235b8434d853 100644 --- a/id/test/object_id_test.go +++ b/id/test/object_id_test.go @@ -14,6 +14,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/roles" "git.perx.ru/perxis/perxis-go/pkg/spaces" "git.perx.ru/perxis/perxis-go/pkg/users" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -886,3 +887,259 @@ func Test_LocaleId(t *testing.T) { }) } } + +func Test_ParseID(t *testing.T) { + tests := []struct { + name string + id any + result *id.ObjectId + wantError bool + }{ + { + name: "SpaceId", + id: "/spaces/<space_id>", + result: id.MustObjectId("/spaces/<space_id>"), + }, + { + name: "ServiceID", + id: "/services/<service_id>", + result: id.MustObjectId("/services/<service_id>"), + }, + { + name: "UserID", + id: "/users/<user_id>", + result: id.MustObjectId("/users/<user_id>"), + }, + { + name: "OrganizationID", + id: "/orgs/<org_id>", + result: id.MustObjectId("/orgs/<org_id>"), + }, + { + name: "ClientID", + id: "/spaces/<space_id>/clients/<client_id>", + result: id.MustObjectId("/spaces/<space_id>/clients/<client_id>"), + }, + { + name: "RoleID", + id: "/spaces/<space_id>/roles/<role_id>", + result: id.MustObjectId("/spaces/<space_id>/roles/<role_id>"), + }, + { + name: "LocaleID", + id: "/spaces/<space_id>/locales/<locale_id>", + result: id.MustObjectId("/spaces/<space_id>/locales/<locale_id>"), + }, + { + name: "EnvironmentID", + id: "/spaces/<space_id>/envs/<env_id>", + result: id.MustObjectId("/spaces/<space_id>/envs/<env_id>"), + }, + { + name: "CollectionId", + id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>", + result: id.MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>"), + }, + { + name: "SchemaID", + id: "/spaces/<space_id>/envs/<env_id>/schema/<collection_id>", + result: id.MustObjectId("/spaces/<space_id>/envs/<env_id>/schema/<collection_id>"), + }, + { + name: "ItemId", + id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>", + result: id.MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>"), + }, + { + name: "RevisionID", + id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/revs/<rev_id>", + result: id.MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/revs/<rev_id>"), + }, + { + name: "FieldId", + id: "/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/fields/<field_name>", + result: id.MustObjectId("/spaces/<space_id>/envs/<env_id>/cols/<collection_id>/items/<item_id>/fields/<field_name>"), + }, + { + name: "With error #1: no backslash in the beginning of id", + id: "spaces/<space_id>", + result: nil, + wantError: true, + }, + { + name: "With error #2: backslash in the end of id", + id: "/spaces/<space_id>/", + result: nil, + wantError: true, + }, + { + name: "With error #3: typo in 'spaces'", + id: "/space/<space_id>", + result: nil, + wantError: true, + }, + { + name: "With error #4: no space_id in id", + id: "/spaces", + result: nil, + wantError: true, + }, + { + name: "With error #5: multiple backslashes in the end of id", + id: "/spaces/<space_id>///", + result: nil, + wantError: true, + }, + { + name: "With error #6: nil value", + id: nil, + wantError: true, + }, + { + name: "With error #7: nil object value", + id: (*items.Item)(nil), + wantError: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + id, err := id.NewObjectId(tt.id) + if tt.wantError { + require.Error(t, err) + return + } + require.NoError(t, err) + require.Equal(t, tt.result, id) + require.Equal(t, tt.id, id.String()) + }) + } +} + +func Test_Map(t *testing.T) { + tests := []struct { + name string + id *id.ObjectId + }{ + { + name: "ServiceID", + id: &id.ObjectId{Descriptor: &id.ServiceId{ServiceID: "<service_id>"}}, + }, + { + name: "UserID", + id: &id.ObjectId{Descriptor: &id.UserId{UserID: "<user_id>"}}, + }, + { + name: "OrganizationID", + id: &id.ObjectId{Descriptor: &id.OrganizationId{OrganizationID: "<org_id>"}}, + }, + { + name: "SpaceId", + id: &id.ObjectId{Descriptor: &id.SpaceId{SpaceID: "<space_id>"}}, + }, + { + name: "ClientID", + id: &id.ObjectId{Descriptor: &id.ClientId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + ClientID: "<client_id>", + }}, + }, + { + name: "RoleID", + id: &id.ObjectId{Descriptor: &id.RoleId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + RoleID: "<role_id>", + }}, + }, + { + name: "LocaleID", + id: &id.ObjectId{Descriptor: &id.LocaleID{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + LocaleID: "<locale_id>", + }}, + }, + { + name: "EnvironmentID", + id: &id.ObjectId{Descriptor: &id.EnvironmentId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + EnvironmentID: "<env_id>", + }}, + }, + { + name: "CollectionId", + id: &id.ObjectId{Descriptor: &id.CollectionId{ + EnvironmentId: id.EnvironmentId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + EnvironmentID: "<env_id>", + }, + CollectionID: "<collection_id>", + }}, + }, + { + name: "Schema ID", + id: &id.ObjectId{Descriptor: &id.SchemaId{ + EnvironmentId: id.EnvironmentId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + EnvironmentID: "<env_id>", + }, + CollectionID: "<collection_id>", + }}, + }, + { + name: "ItemId", + id: &id.ObjectId{Descriptor: &id.ItemId{ + CollectionId: id.CollectionId{ + EnvironmentId: id.EnvironmentId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + EnvironmentID: "<env_id>", + }, + CollectionID: "<collection_id>", + }, + ItemID: "<item_id>", + }}, + }, + { + name: "RevisionID", + id: &id.ObjectId{Descriptor: &id.RevisionId{ + ItemId: id.ItemId{ + CollectionId: id.CollectionId{ + EnvironmentId: id.EnvironmentId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + EnvironmentID: "<env_id>", + }, + CollectionID: "<collection_id>", + }, + ItemID: "<item_id>", + }, + RevisionID: "<rev_id>", + }}, + }, + { + name: "FieldId", + id: &id.ObjectId{Descriptor: &id.FieldId{ + ItemId: id.ItemId{ + CollectionId: id.CollectionId{ + EnvironmentId: id.EnvironmentId{ + SpaceId: id.SpaceId{SpaceID: "<space_id>"}, + EnvironmentID: "<env_id>", + }, + CollectionID: "<collection_id>", + }, + ItemID: "<item_id>", + }, + Field: "<field_name>", + }}, + }, + { + name: "SystemID", + id: &id.ObjectId{Descriptor: &id.SystemId{}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v, err := id.FromMap(tt.id.Map()) + require.NoError(t, err) + assert.Equal(t, tt.id, v, "проверка FromMap для типа ID, должен быть равен исходному значению") + assert.Equal(t, v.Map(), tt.id.Map()) + }) + } +} diff --git a/logs/zap/entry_encoder.go b/logs/zap/entry_encoder.go index d7db9b301a7a41c9b869ddcc0c06146afafe9780..e53171c69dc21a0f9b0b0c82e91f087e780c8017 100644 --- a/logs/zap/entry_encoder.go +++ b/logs/zap/entry_encoder.go @@ -4,9 +4,8 @@ import ( "fmt" "slices" - oid "git.perx.ru/perxis/perxis-go/id" + "git.perx.ru/perxis/perxis-go/id" "git.perx.ru/perxis/perxis-go/logs" - "git.perx.ru/perxis/perxis-go/pkg/id" "git.perx.ru/perxis/perxis-go/zap" "go.uber.org/zap/zapcore" ) @@ -60,9 +59,9 @@ func (enc *entryEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field case "event": ent.Event = clone.fields[i].String case "object": - ent.ObjectID, _ = clone.fields[i].Interface.(*oid.ObjectId) + ent.ObjectID, _ = clone.fields[i].Interface.(*id.ObjectId) case "caller": - ent.CallerID, _ = clone.fields[i].Interface.(*oid.ObjectId) + ent.CallerID, _ = clone.fields[i].Interface.(*id.ObjectId) case "attr": ent.Attr = clone.fields[i].Interface case "error": diff --git a/logs/zap/entry_encoder_slow.go b/logs/zap/entry_encoder_slow.go index 77bf275d93d242ac9ae38f04efe937fa986e5733..8c3f217d126c8ace9983c8de3c57ed11f16235f3 100644 --- a/logs/zap/entry_encoder_slow.go +++ b/logs/zap/entry_encoder_slow.go @@ -4,9 +4,8 @@ import ( "fmt" "maps" - oid "git.perx.ru/perxis/perxis-go/id" + "git.perx.ru/perxis/perxis-go/id" "git.perx.ru/perxis/perxis-go/logs" - "git.perx.ru/perxis/perxis-go/pkg/id" "go.uber.org/zap/zapcore" ) @@ -51,8 +50,8 @@ func (enc *entryEncoderSlow) EncodeEntry(entry zapcore.Entry, fields []zapcore.F ent.Category, _ = clone.Fields["category"].(string) ent.Component, _ = clone.Fields["component"].(string) ent.Event, _ = clone.Fields["event"].(string) - ent.ObjectID, _ = clone.Fields["object"].(*oid.ObjectId) - ent.CallerID, _ = clone.Fields["caller"].(*oid.ObjectId) + ent.ObjectID, _ = clone.Fields["object"].(*id.ObjectId) + ent.CallerID, _ = clone.Fields["caller"].(*id.ObjectId) ent.Attr = clone.Fields["attr"] if err, _ := clone.Fields["error"].(error); err != nil { diff --git a/pkg/expr/format.go b/pkg/expr/format.go index 730388d2d1e654549f8dae00332c6958efb26a65..647d4eba1a6df77e3eacbd76fd8e48749b59c67e 100644 --- a/pkg/expr/format.go +++ b/pkg/expr/format.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "git.perx.ru/perxis/perxis-go/pkg/id" + "git.perx.ru/perxis/perxis-go/id" "github.com/gosimple/slug" ) diff --git a/pkg/expr/mongo_test.go b/pkg/expr/mongo_test.go index 9a7df77d00a8c006327b5b2645818a014d86d9b7..587f25769e4a850d29d00fff3c93c09c6e647eb5 100644 --- a/pkg/expr/mongo_test.go +++ b/pkg/expr/mongo_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "git.perx.ru/perxis/perxis-go/pkg/id" + "git.perx.ru/perxis/perxis-go/id" "github.com/expr-lang/expr" "github.com/expr-lang/expr/ast" "github.com/stretchr/testify/assert" diff --git a/pkg/files/file.go b/pkg/files/file.go index d2236b83368e3d5efc763fd6f7e53664e15cc827..95bc2e31fe1c555b031c997211f7ce3b443c4771 100644 --- a/pkg/files/file.go +++ b/pkg/files/file.go @@ -7,7 +7,7 @@ import ( "strings" "text/template" - "git.perx.ru/perxis/perxis-go/pkg/id" + "git.perx.ru/perxis/perxis-go/id" ) const ( diff --git a/pkg/operation/operation.go b/pkg/operation/operation.go index bbe73370c21ce1a0c290fd53cd8a2225ba62cc4a..c6c46ca3a4353bbc663fb553477c0e8adcfd849f 100644 --- a/pkg/operation/operation.go +++ b/pkg/operation/operation.go @@ -5,9 +5,9 @@ import ( "fmt" "time" + "git.perx.ru/perxis/perxis-go/id" "git.perx.ru/perxis/perxis-go/pkg/errors" grpcerr "git.perx.ru/perxis/perxis-go/pkg/errors/grpc" - "git.perx.ru/perxis/perxis-go/pkg/id" "git.perx.ru/perxis/perxis-go/proto/common" "google.golang.org/grpc" "google.golang.org/protobuf/proto" diff --git a/pkg/queue/queue.go b/pkg/queue/queue.go index 34075f23240f0aff3002f1ad576fd204d620f9e7..dbb207696a2a539dea72f8e7936a15c24194cf29 100644 --- a/pkg/queue/queue.go +++ b/pkg/queue/queue.go @@ -5,8 +5,8 @@ import ( "sync" "time" + "git.perx.ru/perxis/perxis-go/id" "git.perx.ru/perxis/perxis-go/pkg/errors" - "git.perx.ru/perxis/perxis-go/pkg/id" ) const (