diff --git a/audit/api/api_common.go b/audit/api/api_common.go index 15f6d68..97fbed2 100644 --- a/audit/api/api_common.go +++ b/audit/api/api_common.go @@ -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 } diff --git a/audit/api/api_routable_types.go b/audit/api/api_routable_types.go index c8f90f5..6b99ce3 100644 --- a/audit/api/api_routable_types.go +++ b/audit/api/api_routable_types.go @@ -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 - } }