Simplify routable type conversion

This commit is contained in:
Christian Schaible 2024-07-31 13:44:52 +02:00
parent 92fb9923a7
commit 03fb0dd5ed
2 changed files with 37 additions and 10 deletions

View file

@ -84,7 +84,7 @@ func validateAndSerializePartially(
}
// Check that provided identifier type is supported
if err := IsSupportedSingularType(routableIdentifier.Type); err != nil {
if err := routableIdentifier.Type.IsSupportedType(); err != nil {
if errors.Is(err, ErrUnknownSingularType) {
return nil, ErrUnsupportedRoutableType
}
@ -149,7 +149,7 @@ func send(
}
// Check that provided identifier type is supported
if err := IsSupportedSingularType(routableIdentifier.Type); err != nil {
if err := routableIdentifier.Type.IsSupportedType(); err != nil {
if errors.Is(err, ErrUnknownSingularType) {
return ErrUnsupportedRoutableType
}
@ -196,7 +196,7 @@ func isSystemIdentifier(identifier *RoutableIdentifier) bool {
func areIdentifiersIdentical(routableIdentifier *RoutableIdentifier, logName string) error {
dataType, identifier := getTypeAndIdentifierFromString(logName)
pluralType := AsPluralType(dataType)
singularType, err := SingularTypeFromPlural(pluralType)
singularType, err := pluralType.AsSingularType()
if err != nil {
return err
}

View file

@ -6,19 +6,47 @@ const (
SingularTypeFolder SingularType = "folder"
SingularTypeProject SingularType = "project"
PluralTypeTest PluralType = "test"
PluralTypeSystem PluralType = "system"
PluralTypeOrganization PluralType = "organizations"
PluralTypeFolder PluralType = "folders"
PluralTypeProject PluralType = "projects"
)
func (t SingularType) AsPluralType() (PluralType, error) {
switch t {
case SingularTypeSystem:
return PluralTypeSystem, nil
case SingularTypeOrganization:
return PluralTypeOrganization, nil
case SingularTypeFolder:
return PluralTypeFolder, nil
case SingularTypeProject:
return PluralTypeProject, nil
}
return "unknown", ErrUnknownSingularType
}
func AsPluralType(value string) PluralType {
return PluralType(value)
}
func SingularTypeFromPlural(pluralType PluralType) (SingularType, error) {
switch pluralType {
func (t PluralType) IsSupportedType() error {
switch t {
case PluralTypeOrganization:
fallthrough
case PluralTypeFolder:
fallthrough
case PluralTypeProject:
fallthrough
case PluralTypeSystem:
return nil
default:
return ErrUnknownSingularType
}
}
func (t PluralType) AsSingularType() (SingularType, error) {
switch t {
case PluralTypeOrganization:
return SingularTypeOrganization, nil
case PluralTypeFolder:
@ -28,7 +56,7 @@ func SingularTypeFromPlural(pluralType PluralType) (SingularType, error) {
case PluralTypeSystem:
return SingularTypeSystem, nil
default:
return "", ErrUnknownPluralType
return "unknown", ErrUnknownPluralType
}
}
@ -36,8 +64,8 @@ func AsSingularType(value string) SingularType {
return SingularType(value)
}
func IsSupportedSingularType(singularType SingularType) error {
switch singularType {
func (t SingularType) IsSupportedType() error {
switch t {
case SingularTypeOrganization:
fallthrough
case SingularTypeFolder:
@ -48,6 +76,5 @@ func IsSupportedSingularType(singularType SingularType) error {
return nil
default:
return ErrUnknownSingularType
}
}