Select Git revision
collection.go
collection.go 1.34 KiB
package id
const (
Collection = "collection"
CollectionsPrefix = "cols"
)
type CollectionID struct {
EnvironmentID
CollectionID string `json:"col_id,omitempty" bson:"col_id, omitempty"`
}
func (t *CollectionID) Type() string { return Collection }
func (t *CollectionID) String() string {
return Join(t.EnvironmentID.String(), CollectionsPrefix, t.CollectionID)
}
func (t *CollectionID) ToMap() map[string]any {
m := t.EnvironmentID.ToMap()
m["col_id"] = t.CollectionID
m["type"] = Collection
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) Validate() error {
if t.CollectionID == "" {
return ErrInvalidID
}
return t.EnvironmentID.Validate()
}
func parseCollectionID(parts []string) (*CollectionID, error) {
if len(parts) != 6 || parts[4] != CollectionsPrefix {
return nil, ErrInvalidID
}
envID, err := parseEnvironmentID(parts[:4])
if err != nil {
return nil, err
}
var id CollectionID
id.CollectionID = parts[5]
id.EnvironmentID = *envID
return &id, nil
}
func NewCollectionID(spaceID, envID, id string) *ID {
return &ID{Descriptor: &CollectionID{EnvironmentID: EnvironmentID{SpaceID: SpaceID{SpaceID: spaceID}, EnvironmentID: envID}, CollectionID: id}}
}