Skip to content

Commit

Permalink
Adding a few MIME type aliases
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
Dave Henderson committed Jan 26, 2020
1 parent e8bc864 commit 6afb4fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions data/mimetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions data/mimetypes_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 6afb4fd

Please sign in to comment.