Skip to content

Commit

Permalink
Various Enum Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdubs committed Jan 2, 2024
1 parent c1257de commit 315d6a4
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 47 deletions.
9 changes: 8 additions & 1 deletion cmd/server/pactasrv/conv/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "conv",
Expand All @@ -17,3 +17,10 @@ go_library(
"@org_uber_go_zap//:zap",
],
)

go_test(
name = "conv_test",
srcs = ["conv_test.go"],
embed = [":conv"],
deps = ["//pacta"],
)
39 changes: 39 additions & 0 deletions cmd/server/pactasrv/conv/conv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package conv

import (
"testing"

"github.com/RMI/pacta/pacta"
)

func TestLanguageRoundTrip(t *testing.T) {
testEnumConvertability(t, pacta.LanguageValues, LanguageToOAPI, LanguageFromOAPI)
}

func TestAuditLogActorTypeRoundTrip(t *testing.T) {
testEnumConvertability(t, pacta.AuditLogActorTypeValues, auditLogActorTypeToOAPI, auditLogActorTypeFromOAPI)
}

func TestAuditLogActionRoundTrip(t *testing.T) {
testEnumConvertability(t, pacta.AuditLogActionValues, auditLogActionToOAPI, auditLogActionFromOAPI)
}

func TestAuditLogTargetTypeRoundTrip(t *testing.T) {
testEnumConvertability(t, pacta.AuditLogTargetTypeValues, auditLogTargetTypeToOAPI, auditLogTargetTypeFromOAPI)
}

func testEnumConvertability[A comparable, B any](t *testing.T, as []A, aToB func(in A) (B, error), bToA func(in B) (A, error)) {
for _, a := range as {
b, err := aToB(a)
if err != nil {
t.Fatalf("converting from %T %q: %w", a, a, err)
}
a2, err := bToA(b)
if err != nil {
t.Fatalf("converting from %T %q: %w", b, b, err)
}
if a != a2 {
t.Errorf("conversion from %T %q to %T %q and back failed, returned %q", a, a, b, b, a2)
}
}
}
98 changes: 92 additions & 6 deletions cmd/server/pactasrv/conv/oapi_to_pacta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@ import (

var initiativeIDRegex = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)

func LanguageFromOAPI(l api.Language) (pacta.Language, error) {
switch l {
case api.LanguageEN:
return pacta.Language_EN, nil
case api.LanguageES:
return pacta.Language_ES, nil
case api.LanguageFR:
return pacta.Language_FR, nil
case api.LanguageDE:
return pacta.Language_DE, nil
}
return "", oapierr.BadRequest("unknown language", zap.String("language", string(l)))
}

func InitiativeCreateFromOAPI(i *api.InitiativeCreate) (*pacta.Initiative, error) {
if i == nil {
return nil, oapierr.BadRequest("InitiativeCreate cannot be nil")
}
if !initiativeIDRegex.MatchString(i.Id) {
return nil, oapierr.BadRequest("id must contain only alphanumeric characters, underscores, and dashes")
}
lang, err := pacta.ParseLanguage(string(i.Language))
lang, err := LanguageFromOAPI(i.Language)
if err != nil {
return nil, oapierr.BadRequest("failed to parse language", zap.Error(err))
return nil, err
}
var pv *pacta.PACTAVersion
if i.PactaVersion != nil {
Expand Down Expand Up @@ -109,15 +123,65 @@ func PortfolioGroupCreateFromOAPI(pg *api.PortfolioGroupCreate, ownerID pacta.Ow
}

func auditLogActionFromOAPI(i api.AuditLogAction) (pacta.AuditLogAction, error) {
return pacta.ParseAuditLogAction(string(i))
switch i {
case api.AuditLogActionCREATE:
return pacta.AuditLogAction_Create, nil
case api.AuditLogActionUPDATE:
return pacta.AuditLogAction_Update, nil
case api.AuditLogActionDELETE:
return pacta.AuditLogAction_Delete, nil
case api.AuditLogActionADDTO:
return pacta.AuditLogAction_AddTo, nil
case api.AuditLogActionREMOVEFROM:
return pacta.AuditLogAction_RemoveFrom, nil
case api.AuditLogActionENABLEADMINDEBUG:
return pacta.AuditLogAction_EnableAdminDebug, nil
case api.AuditLogActionDISABLEADMINDEBUG:
return pacta.AuditLogAction_DisableAdminDebug, nil
case api.AuditLogActionDOWNLOAD:
return pacta.AuditLogAction_Download, nil
case api.AuditLogActionENABLESHARING:
return pacta.AuditLogAction_EnableSharing, nil
case api.AuditLogActionDISABLESHARING:
return pacta.AuditLogAction_DisableSharing, nil
}
return "", oapierr.BadRequest("unknown audit log action", zap.String("audit_log_action", string(i)))
}

func auditLogActorTypeFromOAPI(i api.AuditLogActorType) (pacta.AuditLogActorType, error) {
return pacta.ParseAuditLogActorType(string(i))
switch i {
case api.AuditLogActorTypePUBLIC:
return pacta.AuditLogActorType_Public, nil
case api.AuditLogActorTypeOWNER:
return pacta.AuditLogActorType_Owner, nil
case api.AuditLogActorTypeADMIN:
return pacta.AuditLogActorType_Admin, nil
case api.AuditLogActorTypeSUPERADMIN:
return pacta.AuditLogActorType_SuperAdmin, nil
case api.AuditLogActorTypeSYSTEM:
return pacta.AuditLogActorType_System, nil
}
return "", oapierr.BadRequest("unknown audit log actor type", zap.String("audit_log_actor_type", string(i)))
}

func auditLogTargetTypeFromOAPI(i api.AuditLogTargetType) (pacta.AuditLogTargetType, error) {
return pacta.ParseAuditLogTargetType(string(i))
switch i {
case api.AuditLogTargetTypeUSER:
return pacta.AuditLogTargetType_User, nil
case api.AuditLogTargetTypePORTFOLIO:
return pacta.AuditLogTargetType_Portfolio, nil
case api.AuditLogTargetTypeINCOMPLETEUPLOAD:
return pacta.AuditLogTargetType_IncompleteUpload, nil
case api.AuditLogTargetTypePORTFOLIOGROUP:
return pacta.AuditLogTargetType_PortfolioGroup, nil
case api.AuditLogTargetTypeINITIATIVE:
return pacta.AuditLogTargetType_Initiative, nil
case api.AuditLogTargetTypePACTAVERSION:
return pacta.AuditLogTargetType_PACTAVersion, nil
case api.AuditLogTargetTypeANALYSIS:
return pacta.AuditLogTargetType_Analysis, nil
}
return "", oapierr.BadRequest("unknown audit log target type", zap.String("audit_log_target_type", string(i)))
}

func auditLogQueryWhereFromOAPI(i api.AuditLogQueryWhere) (*db.AuditLogQueryWhere, error) {
Expand Down Expand Up @@ -168,7 +232,29 @@ func auditLogQueryWhereFromOAPI(i api.AuditLogQueryWhere) (*db.AuditLogQueryWher
}

func auditLogQuerySortByFromOAPI(i api.AuditLogQuerySortBy) (db.AuditLogQuerySortBy, error) {
return db.ParseAuditLogQuerySortBy(string(i))
switch i {
case api.AuditLogQuerySortByCREATEDAT:
return db.AuditLogQuerySortBy_CreatedAt, nil
case api.AuditLogQuerySortByACTORTYPE:
return db.AuditLogQuerySortBy_ActorType, nil
case api.AuditLogQuerySortByACTORID:
return db.AuditLogQuerySortBy_ActorID, nil
case api.AuditLogQuerySortByACTOROWNERID:
return db.AuditLogQuerySortBy_ActorOwnerID, nil
case api.AuditLogQuerySortByPRIMARYTARGETID:
return db.AuditLogQuerySortBy_PrimaryTargetID, nil
case api.AuditLogQuerySortByPRIMARYTARGETTYPE:
return db.AuditLogQuerySortBy_PrimaryTargetType, nil
case api.AuditLogQuerySortByPRIMARYTARGETOWNERID:
return db.AuditLogQuerySortBy_PrimaryTargetOwnerID, nil
case api.AuditLogQuerySortBySECONDARYTARGETID:
return db.AuditLogQuerySortBy_SecondaryTargetID, nil
case api.AuditLogQuerySortBySECONDARYTARGETTYPE:
return db.AuditLogQuerySortBy_SecondaryTargetType, nil
case api.AuditLogQuerySortBySECONDARYTARGETOWNERID:
return db.AuditLogQuerySortBy_SecondaryTargetOwnerID, nil
}
return "", oapierr.BadRequest("unknown audit log query sort by", zap.String("audit_log_query_sort_by", string(i)))
}

func auditLogQuerySortFromOAPI(i api.AuditLogQuerySort) (*db.AuditLogQuerySort, error) {
Expand Down
54 changes: 45 additions & 9 deletions cmd/server/pactasrv/conv/pacta_to_oapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
func LanguageToOAPI(l pacta.Language) (api.Language, error) {
switch l {
case pacta.Language_DE:
return api.De, nil
return api.LanguageDE, nil
case pacta.Language_ES:
return api.Es, nil
return api.LanguageES, nil
case pacta.Language_EN:
return api.En, nil
return api.LanguageEN, nil
case pacta.Language_FR:
return api.Fr, nil
return api.LanguageFR, nil
default:
return "", fmt.Errorf("unknown language: %q", l)
}
Expand Down Expand Up @@ -173,10 +173,13 @@ func IncompleteUploadsToOAPI(ius []*pacta.IncompleteUpload) ([]*api.IncompleteUp
}

func FailureCodeToOAPI(f pacta.FailureCode) (*api.FailureCode, error) {
if f == "" {
switch f {
case "":
return nil, nil
case pacta.FailureCode_Unknown:
return ptr(api.FailureCodeUNKNOWN), nil
}
return ptr(api.FailureCode(f)), nil
return nil, fmt.Errorf("unknown failure code: %q", f)
}

func IncompleteUploadToOAPI(iu *pacta.IncompleteUpload) (*api.IncompleteUpload, error) {
Expand Down Expand Up @@ -273,14 +276,34 @@ func PortfolioGroupsToOAPI(pgs []*pacta.PortfolioGroup) ([]*api.PortfolioGroup,
return convAll(pgs, PortfolioGroupToOAPI)
}

func FileTypeToOAPI(ft pacta.FileType) (api.FileType, error) {
switch ft {
case pacta.FileType_CSV:
return api.FileTypeCSV, nil
case pacta.FileType_YAML:
return api.FileTypeYAML, nil
case pacta.FileType_ZIP:
return api.FileTypeZIP, nil
case pacta.FileType_JSON:
return api.FileTypeJSON, nil
case pacta.FileType_HTML:
return api.FileTypeHTML, nil
}
return "", fmt.Errorf("unknown file type: %q", ft)
}

func BlobToOAPI(b *pacta.Blob) (*api.Blob, error) {
if b == nil {
return nil, oapierr.Internal("blobToOAPI: can't convert nil pointer")
}
ft, err := FileTypeToOAPI(b.FileType)
if err != nil {
return nil, oapierr.Internal("blobToOAPI: fileTypeToOAPI failed", zap.Error(err))
}
return &api.Blob{
Id: string(b.ID),
FileName: b.FileName,
FileType: api.FileType(b.FileType),
FileType: ft,
CreatedAt: b.CreatedAt,
}, nil
}
Expand Down Expand Up @@ -337,6 +360,16 @@ func AnalysisArtifactToOAPI(aa *pacta.AnalysisArtifact) (*api.AnalysisArtifact,
}, nil
}

func AnalysisTypeToOAPI(at pacta.AnalysisType) (api.AnalysisType, error) {
switch at {
case pacta.AnalysisType_Audit:
return api.AnalysisTypeAUDIT, nil
case pacta.AnalysisType_Report:
return api.AnalysisTypeREPORT, nil
}
return "", fmt.Errorf("unknown analysis type: %q", at)
}

func AnalysisToOAPI(a *pacta.Analysis) (*api.Analysis, error) {
if a == nil {
return nil, oapierr.Internal("analysisToOAPI: can't convert nil pointer")
Expand All @@ -357,10 +390,13 @@ func AnalysisToOAPI(a *pacta.Analysis) (*api.Analysis, error) {
if a.FailureMessage != "" {
fm = ptr(a.FailureMessage)
}

at, err := AnalysisTypeToOAPI(a.AnalysisType)
if err != nil {
return nil, oapierr.Internal("analysisToOAPI: analysisTypeToOAPI failed", zap.Error(err))
}
return &api.Analysis{
Id: string(a.ID),
AnalysisType: api.AnalysisType(a.AnalysisType),
AnalysisType: at,
PactaVersion: string(a.PACTAVersion.ID),
PortfolioSnapshot: *snapshot,
Name: a.Name,
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/pactasrv/initiative.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *Server) UpdateInitiative(ctx context.Context, request api.UpdateInitiat
mutations = append(mutations, db.SetInitiativeIsAcceptingNewPortfolios(*b.IsAcceptingNewPortfolios))
}
if b.Language != nil {
lang, err := pacta.ParseLanguage(string(*b.Language))
lang, err := conv.LanguageFromOAPI(*b.Language)
if err != nil {
return nil, oapierr.BadRequest("failed to parse language", zap.Error(err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/pactasrv/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *Server) UpdateUser(ctx context.Context, request api.UpdateUserRequestOb
mutations = append(mutations, db.SetUserName(*request.Body.Name))
}
if request.Body.PreferredLanguage != nil {
lang, err := pacta.ParseLanguage(string(*request.Body.PreferredLanguage))
lang, err := conv.LanguageFromOAPI(*request.Body.PreferredLanguage)
if err != nil {
return nil, oapierr.BadRequest("invalid language", zap.Error(err))
}
Expand Down
46 changes: 17 additions & 29 deletions openapi/pacta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,7 @@ info:
url: https://mit-license.org/
servers:
- url: TODO
definitions:
Language:
type: string
enum: &LANGUAGES
- en
- fr
- es
- de
FileType:
type: string
enum: &FILE_TYPES
- csv
- yaml
- zip
- json
- html
AnalysisType:
type: string
enum: &ANALYSIS_TYPES
- audit
- report
FailureCode:
type: string
enum: &FAILURE_CODES
- UNKNOWN

basePath: /v1
paths:
/access-blob-content:
Expand Down Expand Up @@ -990,16 +966,28 @@ components:
schemas:
Language:
type: string
enum: *LANGUAGES
enum:
- Language_EN
- Language_FR
- Language_ES
- Language_DE
FileType:
type: string
enum: *FILE_TYPES
enum:
- FileType_CSV
- FileType_YAML
- FileType_ZIP
- FileType_JSON
- FileType_HTML
AnalysisType:
type: string
enum: *ANALYSIS_TYPES
enum:
- AnalysisType_AUDIT
- AnalysisType_REPORT
FailureCode:
type: string
enum: *FAILURE_CODES
enum:
- FailureCode_UNKNOWN
PactaVersionCreate:
type: object
required:
Expand Down

0 comments on commit 315d6a4

Please sign in to comment.