diff --git a/db/sqldb/golden/human_readable_schema.sql b/db/sqldb/golden/human_readable_schema.sql index 1cf56bb..9eff8d7 100644 --- a/db/sqldb/golden/human_readable_schema.sql +++ b/db/sqldb/golden/human_readable_schema.sql @@ -40,7 +40,8 @@ CREATE TYPE file_type AS ENUM ( 'csv', 'yaml', 'zip', - 'html'); + 'html', + 'json'); CREATE TYPE language AS ENUM ( 'en', 'de', diff --git a/db/sqldb/golden/schema_dump.sql b/db/sqldb/golden/schema_dump.sql index 7514639..2364cdb 100644 --- a/db/sqldb/golden/schema_dump.sql +++ b/db/sqldb/golden/schema_dump.sql @@ -109,7 +109,8 @@ CREATE TYPE public.file_type AS ENUM ( 'csv', 'yaml', 'zip', - 'html' + 'html', + 'json' ); diff --git a/db/sqldb/migrations/0005_json_blob_type.down.sql b/db/sqldb/migrations/0005_json_blob_type.down.sql new file mode 100644 index 0000000..902dbcc --- /dev/null +++ b/db/sqldb/migrations/0005_json_blob_type.down.sql @@ -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; diff --git a/db/sqldb/migrations/0005_json_blob_type.up.sql b/db/sqldb/migrations/0005_json_blob_type.up.sql new file mode 100644 index 0000000..c542ad7 --- /dev/null +++ b/db/sqldb/migrations/0005_json_blob_type.up.sql @@ -0,0 +1,5 @@ +BEGIN; + +ALTER TYPE file_type ADD VALUE 'json'; + +COMMIT; \ No newline at end of file diff --git a/db/sqldb/sqldb_test.go b/db/sqldb/sqldb_test.go index 2476414..cc627b5 100644 --- a/db/sqldb/sqldb_test.go +++ b/db/sqldb/sqldb_test.go @@ -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 != "" { diff --git a/pacta/enum_test.go b/pacta/enum_test.go index 72e455d..6c4411f 100644 --- a/pacta/enum_test.go +++ b/pacta/enum_test.go @@ -1,6 +1,9 @@ package pacta -import "testing" +import ( + "path/filepath" + "testing" +) func TestParseAuthMechanism(t *testing.T) { testParseEnum(t, AuthnMechanismValues, ParseAuthnMechanism) @@ -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 diff --git a/pacta/pacta.go b/pacta/pacta.go index 44144f6..8984de0 100644 --- a/pacta/pacta.go +++ b/pacta/pacta.go @@ -3,6 +3,7 @@ package pacta import ( "fmt" + "strings" "time" ) @@ -204,6 +205,7 @@ const ( FileType_YAML = "yaml" FileType_ZIP = "zip" FileType_HTML = "html" + FileType_JSON = "json" ) var FileTypeValues = []FileType{ @@ -211,10 +213,15 @@ var FileTypeValues = []FileType{ 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": @@ -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