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

View file

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