diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2020-01-26 13:15:36 -0500 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2020-01-26 13:15:36 -0500 |
| commit | 6afb4fdc59dcb589f6353e02705eb2d2532cb9df (patch) | |
| tree | 61fb78fbdf44e371e19fba13d0386103b665808a /data | |
| parent | e8bc8649e4217b54462143ddb6b47e8e2b9cd4c3 (diff) | |
Adding a few MIME type aliases
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'data')
| -rw-r--r-- | data/datasource.go | 2 | ||||
| -rw-r--r-- | data/mimetypes.go | 14 | ||||
| -rw-r--r-- | data/mimetypes_test.go | 22 |
3 files changed, 37 insertions, 1 deletions
diff --git a/data/datasource.go b/data/datasource.go index 35dbaf0a..2073ef54 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -389,7 +389,7 @@ func (d *Data) Datasource(alias string, args ...string) (interface{}, error) { } func parseData(mimeType, s string) (out interface{}, err error) { - switch mimeType { + switch mimeAlias(mimeType) { case jsonMimetype: out, err = JSON(s) case jsonArrayMimetype: diff --git a/data/mimetypes.go b/data/mimetypes.go index 4117aa43..bdc12ad4 100644 --- a/data/mimetypes.go +++ b/data/mimetypes.go @@ -9,3 +9,17 @@ const ( yamlMimetype = "application/yaml" envMimetype = "application/x-env" ) + +// mimeTypeAliases defines a mapping for non-canonical mime types that are +// sometimes seen in the wild +var mimeTypeAliases = map[string]string{ + "application/x-yaml": yamlMimetype, + "application/text": textMimetype, +} + +func mimeAlias(m string) string { + if a, ok := mimeTypeAliases[m]; ok { + return a + } + return m +} diff --git a/data/mimetypes_test.go b/data/mimetypes_test.go new file mode 100644 index 00000000..0dd1ab05 --- /dev/null +++ b/data/mimetypes_test.go @@ -0,0 +1,22 @@ +package data + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestMimeAlias(t *testing.T) { + t.Parallel() + data := []struct { + in, out string + }{ + {csvMimetype, csvMimetype}, + {yamlMimetype, yamlMimetype}, + {"application/x-yaml", yamlMimetype}, + } + + for _, d := range data { + assert.Equal(t, d.out, mimeAlias(d.in)) + } +} |
