forked from mtusi2026/mtusi2026lab2
80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
package authidentity
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"git.dev.mamont-optics.ru/mamont-optics/backend/application/internal/pkg/types"
|
|
)
|
|
|
|
type Status string
|
|
|
|
const (
|
|
StatusUndefined Status = "UNDEFINED"
|
|
StatusPending Status = "PENDING"
|
|
StatusActive Status = "ACTIVE"
|
|
StatusBlocked Status = "BLOCKED"
|
|
)
|
|
|
|
var allowedStatuses = map[Status]struct{}{
|
|
StatusUndefined: {},
|
|
StatusPending: {},
|
|
StatusActive: {},
|
|
StatusBlocked: {},
|
|
}
|
|
|
|
func ParseStatus(status string) Status {
|
|
s := Status(strings.ToUpper(status))
|
|
if !s.IsValid() {
|
|
return StatusUndefined
|
|
}
|
|
return s
|
|
}
|
|
|
|
func ParseAndValidateStatus(status string) (Status, error) {
|
|
parsed := ParseStatus(status)
|
|
if _, ok := allowedStatuses[parsed]; ok {
|
|
return parsed, nil
|
|
}
|
|
return StatusUndefined, types.ErrUnsupportedAuthIdentityStatus
|
|
}
|
|
|
|
func (t Status) String() string {
|
|
return string(t)
|
|
}
|
|
|
|
func (t Status) Validate() error {
|
|
if _, ok := allowedStatuses[t]; ok {
|
|
return nil
|
|
}
|
|
return types.ErrUnsupportedAuthIdentityStatus
|
|
}
|
|
|
|
func (t Status) IsValid() bool {
|
|
_, ok := allowedStatuses[t]
|
|
return ok
|
|
}
|
|
|
|
func (t Status) MarshalJSON() ([]byte, error) {
|
|
if err := t.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(t.String())
|
|
}
|
|
|
|
func (t *Status) UnmarshalJSON(data []byte) error {
|
|
if t == nil {
|
|
return types.ErrUnmarshalAuthIdentityStatusIntoNilPointer
|
|
}
|
|
var value string
|
|
if err := json.Unmarshal(data, &value); err != nil {
|
|
return err
|
|
}
|
|
parsed := ParseStatus(value)
|
|
if err := parsed.Validate(); err != nil {
|
|
return err
|
|
}
|
|
*t = parsed
|
|
return nil
|
|
}
|