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

WIP

parent 143766b5
No related branches found
No related tags found
No related merge requests found
package id
const (
Collection = "collection"
CollectionsPrefix = "cols"
)
type CollectionID struct {
EnvironmentID
CollectionID string `json:"col_id"`
}
func (t *CollectionID) Type() string { return Collection }
func (t *CollectionID) String() string {
return Join(t.EnvironmentID.String(), t.CollectionID)
}
func (t *CollectionID) ToMap() map[string]any {
m := t.EnvironmentID.ToMap()
m["col_id"] = t.EnvironmentID
return m
}
func (t *CollectionID) FromMap(m map[string]any) error {
if err := t.EnvironmentID.FromMap(m); err != nil {
return err
}
t.CollectionID = m["col_id"].(string)
return nil
}
func (t *CollectionID) FromString(id string) error {
parts := Split(id)
return t.fromParts(parts)
}
func (t *CollectionID) fromParts(parts []string) error {
if err := t.EnvironmentID.fromParts(parts); err != nil {
return err
}
if len(parts) < 6 || parts[4] != CollectionsPrefix {
return ErrInvalidID
}
t.CollectionID = parts[5]
return nil
}
func (t *CollectionID) Validate() error {
if t.CollectionID == "" {
return ErrInvalidID
}
return t.EnvironmentID.Validate()
}
package id
const (
Environment = "environment"
EnvironmentsPrefix = "envs"
)
type EnvironmentID struct {
SpaceID
EnvironmentID string `json:"env_id"`
}
func (t *EnvironmentID) Type() string { return Environment }
func (t *EnvironmentID) String() string {
return Join(t.SpaceID.String(), t.EnvironmentID)
}
func (t *EnvironmentID) ToMap() map[string]any {
m := t.SpaceID.ToMap()
m["env_id"] = t.EnvironmentID
return m
}
func (t *EnvironmentID) FromMap(m map[string]any) error {
if err := t.SpaceID.FromMap(m); err != nil {
return err
}
t.EnvironmentID = m["env_id"].(string)
return nil
}
func (t *EnvironmentID) FromString(id string) error {
parts := Split(id)
return t.fromParts(parts)
}
func (t *EnvironmentID) fromParts(parts []string) error {
if err := t.SpaceID.fromParts(parts); err != nil {
return err
}
if len(parts) < 4 || parts[2] != EnvironmentsPrefix {
return ErrInvalidID
}
t.EnvironmentID = parts[3]
return nil
}
func (t *EnvironmentID) Validate() error {
if t.EnvironmentID == "" {
return ErrInvalidID
}
return t.SpaceID.Validate()
}
id/id.go 0 → 100644
package id
import (
"errors"
"strings"
)
const Separator = '/'
var ErrInvalidID = errors.New("invalid id")
type ID interface {
String() string
Type() string
ToMap() map[string]any
FromString(string) error
FromMap(map[string]any) error
Validate() error
}
func Split(id string) []string {
if id[0] != Separator {
return nil
}
return strings.Split(id[1:], string(Separator))
}
func Join(parts ...string) string {
s := strings.Join(parts, string(Separator))
if s[0] != Separator {
s = string(Separator) + s
}
return s
}
package id
// Сохранение/чтение в JSON (в строку)
package id
const (
Space = "space"
SpacesPrefix = "spaces"
)
type SpaceID struct {
SpaceID string `json:"space_id"`
}
func (t *SpaceID) Type() string { return Space }
func (t *SpaceID) String() string {
return Join(SpacesPrefix, t.SpaceID)
}
func (t *SpaceID) ToMap() map[string]any {
return map[string]any{
"space_id": t.SpaceID,
}
}
func (t *SpaceID) FromMap(m map[string]any) error {
t.SpaceID = m["space_id"].(string)
return nil
}
func (t *SpaceID) FromString(id string) error {
parts := Split(id)
return t.fromParts(parts)
}
func (t *SpaceID) fromParts(parts []string) error {
if len(parts) < 2 || parts[0] != SpacesPrefix {
return ErrInvalidID
}
t.SpaceID = parts[1]
return nil
}
func (t *SpaceID) Validate() error {
if t.SpaceID == "" {
return ErrInvalidID
}
return nil
}
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