Skip to content

Commit

Permalink
Enables JSON File Types
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdubs committed Nov 20, 2023
1 parent 1672438 commit a963d41
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
3 changes: 2 additions & 1 deletion db/sqldb/golden/human_readable_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ CREATE TYPE file_type AS ENUM (
'csv',
'yaml',
'zip',
'html');
'html',
'json');
CREATE TYPE language AS ENUM (
'en',
'de',
Expand Down
3 changes: 2 additions & 1 deletion db/sqldb/golden/schema_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ CREATE TYPE public.file_type AS ENUM (
'csv',
'yaml',
'zip',
'html'
'html',
'json'
);


Expand Down
19 changes: 19 additions & 0 deletions db/sqldb/migrations/0005_json_blob_type.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BEGIN;

-- There isn't a way to delete a value from an enum, so this is the workaround
-- https://stackoverflow.com/a/56777227/17909149

ALTER TABLE blob ALTER file_type TYPE TEXT;

DROP TYPE file_type;
CREATE TYPE file_type AS ENUM (
'csv',
'yaml',
'zip',
'html');

ALTER TABLE blob
ALTER file_type TYPE file_type
USING file_type::file_type;

COMMIT;
5 changes: 5 additions & 0 deletions db/sqldb/migrations/0005_json_blob_type.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TYPE file_type ADD VALUE 'json';

COMMIT;
1 change: 1 addition & 0 deletions db/sqldb/sqldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestSchemaHistory(t *testing.T) {
{ID: 2, Version: 2}, // 0002_create_user_table
{ID: 3, Version: 3}, // 0003_domain_types
{ID: 4, Version: 4}, // 0004_audit_log_tweaks
{ID: 5, Version: 5}, // 0005_json_blob_type
}

if diff := cmp.Diff(want, got); diff != "" {
Expand Down
19 changes: 18 additions & 1 deletion pacta/enum_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pacta

import "testing"
import (
"path/filepath"
"testing"
)

func TestParseAuthMechanism(t *testing.T) {
testParseEnum(t, AuthnMechanismValues, ParseAuthnMechanism)
Expand All @@ -12,6 +15,20 @@ func TestParseLanguage(t *testing.T) {

func TestParseFileType(t *testing.T) {
testParseEnum(t, FileTypeValues, ParseFileType)
otherCases := []string{
"hello/world.json",
"hello/world/hithere.JsOn",
" hello/world/hithere.json ",
}
for _, c := range otherCases {
ft, err := ParseFileType(filepath.Ext(c))
if err != nil {
t.Errorf("expected successful parse, got %w", err)
}
if ft != FileType_JSON {
t.Errorf("expected JSON, got %v", ft)
}
}
}

// need
Expand Down
13 changes: 11 additions & 2 deletions pacta/pacta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pacta

import (
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -204,17 +205,23 @@ const (
FileType_YAML = "yaml"
FileType_ZIP = "zip"
FileType_HTML = "html"
FileType_JSON = "json"
)

var FileTypeValues = []FileType{
FileType_CSV,
FileType_YAML,
FileType_ZIP,
FileType_HTML,
FileType_JSON,
}

func ParseFileType(s string) (FileType, error) {
switch s {
ss := strings.TrimSpace(strings.ToLower(s))
if strings.HasPrefix(ss, ".") {
ss = ss[1:]
}
switch ss {
case "csv":
return FileType_CSV, nil
case "yaml":
Expand All @@ -223,8 +230,10 @@ func ParseFileType(s string) (FileType, error) {
return FileType_ZIP, nil
case "html":
return FileType_HTML, nil
case "json":
return FileType_JSON, nil
}
return "", fmt.Errorf("unknown FileType: %q", s)
return "", fmt.Errorf("unknown pacta.FileType: %q", s)
}

type BlobURI string
Expand Down

0 comments on commit a963d41

Please sign in to comment.