aboutsummaryrefslogtreecommitdiffhomepage
path: root/output/outputFormat.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2021-03-11 09:18:01 +0100
committerBjørn Erik Pedersen <[email protected]>2021-03-14 15:21:54 +0100
commitba1d0051b44fdd242b20899e195e37ab26501516 (patch)
tree646f815cf5eb027bae13e329696acf14d640d3f9 /output/outputFormat.go
parent1b1dcf586e220c3a8ad5ecfa8e4c3dac97f0ab44 (diff)
downloadhugo-ba1d0051b44fdd242b20899e195e37ab26501516.tar.gz
hugo-ba1d0051b44fdd242b20899e195e37ab26501516.zip
media: Make Type comparable
So we can use it and output.Format as map key etc. This commit also fixes the media.Type implementation so it does not need to mutate itself to handle different suffixes for the same MIME type, e.g. jpg vs. jpeg. This means that there are no Suffix or FullSuffix on media.Type anymore. Fixes #8317 Fixes #8324
Diffstat (limited to 'output/outputFormat.go')
-rw-r--r--output/outputFormat.go37
1 files changed, 25 insertions, 12 deletions
diff --git a/output/outputFormat.go b/output/outputFormat.go
index ee5881e2a..1bd86d2ca 100644
--- a/output/outputFormat.go
+++ b/output/outputFormat.go
@@ -20,6 +20,8 @@ import (
"sort"
"strings"
+ "github.com/pkg/errors"
+
"github.com/mitchellh/mapstructure"
"github.com/gohugoio/hugo/media"
@@ -207,14 +209,16 @@ func (formats Formats) Less(i, j int) bool {
// The lookup is case insensitive.
func (formats Formats) GetBySuffix(suffix string) (f Format, found bool) {
for _, ff := range formats {
- if strings.EqualFold(suffix, ff.MediaType.Suffix()) {
- if found {
- // ambiguous
- found = false
- return
+ for _, suffix2 := range ff.MediaType.Suffixes() {
+ if strings.EqualFold(suffix, suffix2) {
+ if found {
+ // ambiguous
+ found = false
+ return
+ }
+ f = ff
+ found = true
}
- f = ff
- found = true
}
}
return
@@ -310,6 +314,7 @@ func DecodeFormats(mediaTypes media.Types, maps ...map[string]interface{}) (Form
}
f = append(f, newOutFormat)
+
}
}
}
@@ -319,7 +324,7 @@ func DecodeFormats(mediaTypes media.Types, maps ...map[string]interface{}) (Form
return f, nil
}
-func decode(mediaTypes media.Types, input, output interface{}) error {
+func decode(mediaTypes media.Types, input interface{}, output *Format) error {
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
@@ -337,12 +342,19 @@ func decode(mediaTypes media.Types, input, output interface{}) error {
// If mediaType is a string, look it up and replace it
// in the map.
vv := dataVal.MapIndex(key)
- if mediaTypeStr, ok := vv.Interface().(string); ok {
- mediaType, found := mediaTypes.GetByType(mediaTypeStr)
+ vvi := vv.Interface()
+
+ switch vviv := vvi.(type) {
+ case media.Type:
+ // OK
+ case string:
+ mediaType, found := mediaTypes.GetByType(vviv)
if !found {
- return c, fmt.Errorf("media type %q not found", mediaTypeStr)
+ return c, fmt.Errorf("media type %q not found", vviv)
}
dataVal.SetMapIndex(key, reflect.ValueOf(mediaType))
+ default:
+ return nil, errors.Errorf("invalid output format configuration; wrong type for media type, expected string (e.g. text/html), got %T", vvi)
}
}
}
@@ -357,12 +369,13 @@ func decode(mediaTypes media.Types, input, output interface{}) error {
}
return decoder.Decode(input)
+
}
// BaseFilename returns the base filename of f including an extension (ie.
// "index.xml").
func (f Format) BaseFilename() string {
- return f.BaseName + f.MediaType.FullSuffix()
+ return f.BaseName + f.MediaType.FirstSuffix.FullSuffix
}
// MarshalJSON returns the JSON encoding of f.