2020-12-26 07:47:10 +00:00
|
|
|
package common
|
|
|
|
|
2021-11-11 15:27:07 +00:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-26 07:47:10 +00:00
|
|
|
type NewDecoderFunc func([]byte) Decoder
|
|
|
|
|
2021-11-11 15:43:20 +00:00
|
|
|
type decoderItem struct {
|
|
|
|
noop bool
|
|
|
|
decoder NewDecoderFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
var decoderRegistry = make(map[string][]decoderItem)
|
2020-12-26 07:47:10 +00:00
|
|
|
|
2021-11-11 15:43:20 +00:00
|
|
|
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
|
|
|
decoderRegistry[ext] = append(decoderRegistry[ext],
|
|
|
|
decoderItem{noop: noop, decoder: dispatchFunc})
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|
2021-11-11 15:43:20 +00:00
|
|
|
func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) {
|
2021-11-11 15:27:07 +00:00
|
|
|
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), "."))
|
2021-11-11 15:43:20 +00:00
|
|
|
for _, dec := range decoderRegistry[ext] {
|
|
|
|
if skipNoop && dec.noop {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rs = append(rs, dec.decoder)
|
|
|
|
}
|
|
|
|
return
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|