From 6afb4fdc59dcb589f6353e02705eb2d2532cb9df Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 26 Jan 2020 13:15:36 -0500 Subject: Adding a few MIME type aliases Signed-off-by: Dave Henderson --- data/datasource.go | 2 +- data/mimetypes.go | 14 ++++++++++++++ data/mimetypes_test.go | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 data/mimetypes_test.go (limited to 'data') 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)) + } +} -- cgit v1.2.3