From f7c69e439e6ef6a8d3a025f3f97e85da2d9f324c Mon Sep 17 00:00:00 2001 From: Alex Petraky <petraky@perx.ru> Date: Fri, 19 Apr 2024 06:54:58 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D1=81=D0=BE=D0=B4=D0=B5=D1=80=D0=B6=D0=B8?= =?UTF-8?q?=D0=BC=D0=BE=D0=B5=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=B0=20pkg?= =?UTF-8?q?/id=20=D0=BD=D0=B0=20=D1=83=D1=80=D0=BE=D0=B2=D0=B5=D0=BD=D1=8C?= =?UTF-8?q?=20=D0=B2=D1=8B=D1=88=D0=B5=20=D0=B2=20=D0=BF=D0=B0=D0=BA=D0=B5?= =?UTF-8?q?=D1=82=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {pkg/id => id}/id.go | 0 {pkg/id => id}/id_test.go | 0 id/object_id_test.go | 265 --------------------------------- id/test/object_id_test.go | 257 ++++++++++++++++++++++++++++++++ logs/zap/entry_encoder.go | 7 +- logs/zap/entry_encoder_slow.go | 7 +- pkg/expr/format.go | 2 +- pkg/expr/mongo_test.go | 2 +- pkg/files/file.go | 2 +- pkg/operation/operation.go | 2 +- pkg/queue/queue.go | 2 +- 11 files changed, 268 insertions(+), 278 deletions(-) rename {pkg/id => id}/id.go (100%) rename {pkg/id => id}/id_test.go (100%) delete mode 100644 id/object_id_test.go 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 56a2c104..00000000 --- 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 e29328dd..2f01fa12 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 d7db9b30..e53171c6 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 77bf275d..8c3f217d 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 730388d2..647d4eba 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 9a7df77d..587f2576 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 d2236b83..95bc2e31 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 bbe73370..c6c46ca3 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 34075f23..dbb20769 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 ( -- GitLab