Skip to content
Snippets Groups Projects
Commit 221d730e authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

feat: Добавлен OID для Locales

parents eea78cb4 28274816
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,10 @@ func TestID_MarshalUnmarshalBSON(t *testing.T) { ...@@ -41,6 +41,10 @@ func TestID_MarshalUnmarshalBSON(t *testing.T) {
name: "RoleID", name: "RoleID",
id: &ObjectId{Descriptor: &RoleId{RoleID: "1", SpaceId: SpaceId{SpaceID: "1"}}}, id: &ObjectId{Descriptor: &RoleId{RoleID: "1", SpaceId: SpaceId{SpaceID: "1"}}},
}, },
{
name: "LocaleID",
id: &ObjectId{Descriptor: &LocaleID{LocaleID: "1", SpaceId: SpaceId{SpaceID: "1"}}},
},
{ {
name: "CollectionId", name: "CollectionId",
id: &ObjectId{Descriptor: &CollectionId{CollectionID: "1", EnvironmentId: EnvironmentId{EnvironmentID: "1", SpaceId: SpaceId{SpaceID: "1"}}}}, id: &ObjectId{Descriptor: &CollectionId{CollectionID: "1", EnvironmentId: EnvironmentId{EnvironmentID: "1", SpaceId: SpaceId{SpaceID: "1"}}}},
......
package id
import "fmt"
const (
Locale = "locale"
LocalesPrefix = "locales"
)
var _ Descriptor = &LocaleID{}
type LocaleID struct {
SpaceId
LocaleID string `json:"locale_id,omitempty" bson:"locale_id,omitempty"`
}
func (id *LocaleID) New() Descriptor {
return &LocaleID{}
}
func (id *LocaleID) Type() string { return Locale }
func (id *LocaleID) String() string {
return Join(id.SpaceId.String(), LocalesPrefix, id.LocaleID)
}
func (id *LocaleID) FromParts(parts []string) error {
if len(parts) != 4 || parts[2] != LocalesPrefix {
return ErrInvalidID
}
if err := id.SpaceId.FromParts(parts[:2]); err != nil {
return err
}
id.LocaleID = parts[3]
return nil
}
func (id *LocaleID) Map() map[string]any {
m := id.SpaceId.Map()
m["locale_id"] = id.LocaleID
m["type"] = Locale
return m
}
func (id *LocaleID) FromMap(m map[string]any) error {
id.LocaleID = m["locale_id"].(string)
if id.LocaleID == "" {
return fmt.Errorf("%w: LocaleID required", ErrInvalidID)
}
return id.SpaceId.FromMap(m)
}
func (id *LocaleID) Validate() error {
if id.LocaleID == "" {
return fmt.Errorf("%w: LocaleID required", ErrInvalidID)
}
return id.SpaceId.Validate()
}
func NewLocaleId(spaceID, id string) *ObjectId {
return &ObjectId{Descriptor: &LocaleID{SpaceId: SpaceId{SpaceID: spaceID}, LocaleID: id}}
}
...@@ -45,6 +45,11 @@ func Test_ParseID(t *testing.T) { ...@@ -45,6 +45,11 @@ func Test_ParseID(t *testing.T) {
id: "/spaces/<space_id>/roles/<role_id>", id: "/spaces/<space_id>/roles/<role_id>",
result: MustObjectId("/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", name: "EnvironmentID",
id: "/spaces/<space_id>/envs/<env_id>", id: "/spaces/<space_id>/envs/<env_id>",
...@@ -165,6 +170,13 @@ func Test_Map(t *testing.T) { ...@@ -165,6 +170,13 @@ func Test_Map(t *testing.T) {
RoleID: "<role_id>", RoleID: "<role_id>",
}}, }},
}, },
{
name: "LocaleID",
id: &ObjectId{Descriptor: &LocaleID{
SpaceId: SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
},
{ {
name: "EnvironmentID", name: "EnvironmentID",
id: &ObjectId{Descriptor: &EnvironmentId{ id: &ObjectId{Descriptor: &EnvironmentId{
......
...@@ -110,6 +110,7 @@ func RegisterSystemIds(r *Registry) { ...@@ -110,6 +110,7 @@ func RegisterSystemIds(r *Registry) {
r.RegisterDescriptor(&SystemId{}) r.RegisterDescriptor(&SystemId{})
r.RegisterDescriptor(&ServiceId{}) r.RegisterDescriptor(&ServiceId{})
r.RegisterDescriptor(&OrganizationId{}) r.RegisterDescriptor(&OrganizationId{})
r.RegisterDescriptor(&LocaleID{})
} }
func GetRegistry() *Registry { func GetRegistry() *Registry {
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/environments" "git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/items" "git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/locales"
"git.perx.ru/perxis/perxis-go/pkg/organizations" "git.perx.ru/perxis/perxis-go/pkg/organizations"
"git.perx.ru/perxis/perxis-go/pkg/roles" "git.perx.ru/perxis/perxis-go/pkg/roles"
"git.perx.ru/perxis/perxis-go/pkg/spaces" "git.perx.ru/perxis/perxis-go/pkg/spaces"
...@@ -74,6 +75,11 @@ func Handler(obj any) *id.ObjectId { ...@@ -74,6 +75,11 @@ func Handler(obj any) *id.ObjectId {
var i id.UserId var i id.UserId
i.UserID = val.GetID(context.TODO()) i.UserID = val.GetID(context.TODO())
return id.MustObjectId(&i) return id.MustObjectId(&i)
case *locales.Locale:
var i id.LocaleID
i.SpaceID = val.SpaceID
i.LocaleID = val.ID
return id.MustObjectId(&i)
} }
return nil return nil
} }
...@@ -92,6 +98,7 @@ func Register(r *id.Registry) { ...@@ -92,6 +98,7 @@ func Register(r *id.Registry) {
r.RegisterObjectHandler(reflect.TypeOf(&auth.ClientPrincipal{}), Handler) r.RegisterObjectHandler(reflect.TypeOf(&auth.ClientPrincipal{}), Handler)
r.RegisterObjectHandler(reflect.TypeOf(&auth.SystemPrincipal{}), Handler) r.RegisterObjectHandler(reflect.TypeOf(&auth.SystemPrincipal{}), Handler)
r.RegisterObjectHandler(reflect.TypeOf(&auth.Anonymous{}), Handler) r.RegisterObjectHandler(reflect.TypeOf(&auth.Anonymous{}), Handler)
r.RegisterObjectHandler(reflect.TypeOf(&locales.Locale{}), Handler)
} }
func init() { func init() {
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/environments" "git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/items" "git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/locales"
"git.perx.ru/perxis/perxis-go/pkg/organizations" "git.perx.ru/perxis/perxis-go/pkg/organizations"
"git.perx.ru/perxis/perxis-go/pkg/roles" "git.perx.ru/perxis/perxis-go/pkg/roles"
"git.perx.ru/perxis/perxis-go/pkg/spaces" "git.perx.ru/perxis/perxis-go/pkg/spaces"
...@@ -799,3 +800,89 @@ func Test_RevisionId(t *testing.T) { ...@@ -799,3 +800,89 @@ func Test_RevisionId(t *testing.T) {
}) })
} }
} }
func Test_LocaleId(t *testing.T) {
tests := []struct {
name string
in any
out string
result *id.ObjectId
err error
}{
{
name: "valid string",
in: "/spaces/<space_id>/locales/<locale_id>",
result: &id.ObjectId{Descriptor: &id.LocaleID{
SpaceId: id.SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
},
{
name: "invalid string",
in: "/locales/<locale_id>",
result: &id.ObjectId{Descriptor: &id.LocaleID{
SpaceId: id.SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
err: id.ErrInvalidID,
},
{
name: "valid object",
in: &locales.Locale{SpaceID: "<space_id>", ID: "<locale_id>"},
out: "/spaces/<space_id>/locales/<locale_id>",
result: &id.ObjectId{Descriptor: &id.LocaleID{
SpaceId: id.SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
},
{
name: "valid map",
in: map[string]any{"type": "locale", "space_id": "<space_id>", "locale_id": "<locale_id>"},
out: "/spaces/<space_id>/locales/<locale_id>",
result: &id.ObjectId{Descriptor: &id.LocaleID{
SpaceId: id.SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
},
{
name: "invalid map 1",
in: map[string]any{"type": "client", "space_id": "<space_id>"},
out: "/spaces/<space_id>/locales/<locale_id>",
result: &id.ObjectId{Descriptor: &id.LocaleID{
SpaceId: id.SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
err: id.ErrInvalidID,
},
{
name: "invalid map 2",
in: map[string]any{"type": "locale", "locale_id": "<locale_id>"},
out: "/spaces/<space_id>/locales/<locale_id>",
result: &id.ObjectId{Descriptor: &id.LocaleID{
SpaceId: id.SpaceId{SpaceID: "<space_id>"},
LocaleID: "<locale_id>",
}},
err: id.ErrInvalidID,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i, err := id.NewObjectId(tt.in)
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
require.Equal(t, tt.result, i)
if tt.out == "" {
require.Equal(t, tt.in, i.String())
} else {
require.Equal(t, tt.out, i.String())
}
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment