Skip to content

Commit

Permalink
fix!: rename mediatype.MediaTypeParseOptions to remove repetition
Browse files Browse the repository at this point in the history
Signed-off-by: James Hillyerd <[email protected]>

enable linters after fixes

Signed-off-by: James Hillyerd <[email protected]>
  • Loading branch information
jhillyerd committed Aug 31, 2024
1 parent af8da8b commit 904a449
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 20 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ linters:
- contextcheck
- decorder
# - dupl
# - dupword
- durationcheck
- errchkjson
- errname
Expand Down Expand Up @@ -56,7 +55,7 @@ linters:
- promlinter
- protogetter
- reassign
# - revive
- revive
- rowserrcheck
- sloglint
- stylecheck
Expand Down
2 changes: 1 addition & 1 deletion boundary.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (b *boundaryReader) Next() (bool, error) {
_, _ = io.Copy(io.Discard, b)
}
for {
var line []byte = nil
var line []byte
var err error
for {
// Read whole line, handle extra long lines in cycle
Expand Down
8 changes: 4 additions & 4 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func TestBuilderAddAttachment(t *testing.T) {
"text/html",
"image/jpeg",
}
gotParts := root.DepthMatchAll(func(p *enmime.Part) bool { return true })
gotParts := root.DepthMatchAll(func(_ *enmime.Part) bool { return true })
gotTypes := make([]string, 0)
for _, p := range gotParts {
gotTypes = append(gotTypes, p.ContentType)
Expand Down Expand Up @@ -734,7 +734,7 @@ func TestBuilderAddInline(t *testing.T) {
"text/html",
"image/jpeg",
}
gotParts := root.DepthMatchAll(func(p *enmime.Part) bool { return true })
gotParts := root.DepthMatchAll(func(_ *enmime.Part) bool { return true })
gotTypes := make([]string, 0)
for _, p := range gotParts {
gotTypes = append(gotTypes, p.ContentType)
Expand Down Expand Up @@ -878,7 +878,7 @@ func TestBuilderAddOtherPart(t *testing.T) {
"text/html",
"image/jpeg",
}
gotParts := root.DepthMatchAll(func(p *enmime.Part) bool { return true })
gotParts := root.DepthMatchAll(func(_ *enmime.Part) bool { return true })
gotTypes := make([]string, 0)
for _, p := range gotParts {
contentType := p.ContentType
Expand Down Expand Up @@ -1016,7 +1016,7 @@ func TestBuilderFullStructure(t *testing.T) {
"multipart/related > image/png",
"multipart/mixed > image/jpeg",
}
gotParts := root.DepthMatchAll(func(p *enmime.Part) bool { return true })
gotParts := root.DepthMatchAll(func(_ *enmime.Part) bool { return true })
gotTypes := make([]string, 0)
for _, p := range gotParts {
pct := ""
Expand Down
4 changes: 2 additions & 2 deletions cmd/mime-extractor/mime-extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestExtractFailedToParse(t *testing.T) {

func TestExtractAttachmentWriteFail(t *testing.T) {
s := &bytes.Buffer{}
fw := func(filename string, data []byte, perm os.FileMode) error {
fw := func(_ string, _ []byte, _ os.FileMode) error {
return errors.New("AttachmentWriteFail")
}
testExtractor := &extractor{
Expand All @@ -105,7 +105,7 @@ func TestExtractAttachmentWriteFail(t *testing.T) {
func TestExtractSuccess(t *testing.T) {
b := &bytes.Buffer{}
attachmentCount := 0
fw := func(filename string, data []byte, perm os.FileMode) error {
fw := func(_ string, _ []byte, _ os.FileMode) error {
attachmentCount++
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions mediatype/mediatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const (
utf8 = "utf-8"
)

// MediaTypeParseOptions controls the parsing of content-type and media-type strings.
type MediaTypeParseOptions struct {
// ParseOptions controls the parsing of content-type and media-type strings.
type ParseOptions struct {
StripMediaTypeInvalidCharacters bool
}

Expand All @@ -43,11 +43,11 @@ type MediaTypeParseOptions struct {
// - Unquoted values in media parameters containing 'tspecials' characters
// - Newline characters
func Parse(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
return ParseWithOptions(ctype, MediaTypeParseOptions{})
return ParseWithOptions(ctype, ParseOptions{})
}

// ParseWithOptions parses media-type with additional options controlling the parsing behavior.
func ParseWithOptions(ctype string, options MediaTypeParseOptions) (mtype string, params map[string]string, invalidParams []string, err error) {
func ParseWithOptions(ctype string, options ParseOptions) (mtype string, params map[string]string, invalidParams []string, err error) {
mtype, params, err = mime.ParseMediaType(
fixNewlines(fixUnescapedQuotes(fixUnquotedSpecials(fixMangledMediaType(removeTrailingHTMLTags(ctype), ';', options)))))
if err != nil {
Expand All @@ -73,7 +73,7 @@ func ParseWithOptions(ctype string, options MediaTypeParseOptions) (mtype string

// fixMangledMediaType is used to insert ; separators into media type strings that lack them, and
// remove repeated parameters.
func fixMangledMediaType(mtype string, sep rune, options MediaTypeParseOptions) string {
func fixMangledMediaType(mtype string, sep rune, options ParseOptions) string {
strsep := string([]rune{sep})
if mtype == "" {
return ""
Expand Down
4 changes: 2 additions & 2 deletions mediatype/mediatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestFixMangledMediaType(t *testing.T) {
input string
sep rune
want string
options MediaTypeParseOptions
options ParseOptions
}{
{
input: "",
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestFixMangledMediaType(t *testing.T) {
input: `text/html>`,
sep: ';',
want: `text/html`,
options: MediaTypeParseOptions{StripMediaTypeInvalidCharacters: true},
options: ParseOptions{StripMediaTypeInvalidCharacters: true},
},
}
for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestSetCustomParseMediaType(t *testing.T) {
alwaysReturnHTML := func(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
alwaysReturnHTML := func(_ string) (mtype string, params map[string]string, invalidParams []string, err error) {
return "text/html", nil, nil, err
}
changeAndUtilizeDefault := func(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion part.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (p *Part) decodeContent(r io.Reader, readPartErrorPolicy ReadPartErrorPolic
// parses media type using custom or default media type parser
func (p *Part) parseMediaType(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
if p.parser == nil || p.parser.customParseMediaType == nil {
return mediatype.ParseWithOptions(ctype, mediatype.MediaTypeParseOptions{StripMediaTypeInvalidCharacters: p.parser.stripMediaTypeInvalidCharacters})
return mediatype.ParseWithOptions(ctype, mediatype.ParseOptions{StripMediaTypeInvalidCharacters: p.parser.stripMediaTypeInvalidCharacters})
}

return p.parser.customParseMediaType(ctype)
Expand Down
4 changes: 2 additions & 2 deletions part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPlainTextPart(t *testing.T) {
test.ContentContainsString(t, p.Content, want)
}

func TestAddChildInfiniteLoops(t *testing.T) {
func TestAddChildInfiniteLoops(_ *testing.T) {
// Part adds itself
parentPart := &enmime.Part{
ContentType: "text/plain",
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestReadPartErrorPolicy(t *testing.T) {
})

// example policy 3: always recover the partial content read, no matter the error
examplePolicy3 := enmime.ReadPartErrorPolicy(func(p *enmime.Part, err error) bool {
examplePolicy3 := enmime.ReadPartErrorPolicy(func(_ *enmime.Part, _ error) bool {
return true
})

Expand Down

0 comments on commit 904a449

Please sign in to comment.