Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: remove deprecated MaxPartErrors global var #353

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ const (
ErrorMalformedChildPart = "Malformed child part"
)

// MaxPartErrors limits number of part parsing errors, errors after the limit are ignored.
// 0 means unlimited.
//
// Deprecated: This limit may be set via the `MaxStoredPartErrors` Parser option.
var MaxPartErrors = 0

// Error describes an error encountered while parsing.
type Error struct {
Name string // The name or type of error encountered, from Error consts.
Expand Down Expand Up @@ -84,10 +78,10 @@ func (p *Part) addWarningf(name string, detailFmt string, args ...interface{}) {

// addProblem adds general *Error to the Part error slice.
func (p *Part) addProblem(err *Error) {
maxErrors := MaxPartErrors
if p.parser != nil && p.parser.maxStoredPartErrors != nil {
maxErrors := 0
if p.parser != nil {
// Override global var.
maxErrors = *p.parser.maxStoredPartErrors
maxErrors = p.parser.maxStoredPartErrors
}

if (maxErrors == 0) || (len(p.Errors) < maxErrors) {
Expand Down
53 changes: 0 additions & 53 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,53 +133,7 @@ func TestErrorEnvelopeWarnings(t *testing.T) {
}
}

// Deprecated.
func TestErrorLimit(t *testing.T) {
// Backup global variable
originalMaxPartErros := MaxPartErrors
defer func() {
MaxPartErrors = originalMaxPartErros
}()

addThreeErrors := func() int {
part := NewPart("text/plain")
part.addError("test1", "test1")
part.addError("test2", "test2")
part.addError("test3", "test3")

return len(part.Errors)
}

// Check unlimited
var errCount int
MaxPartErrors = 0
errCount = addThreeErrors()
if errCount != 3 {
t.Errorf("Expected unlimited errors (3), got %d", errCount)
}

// Check limit
MaxPartErrors = 1
errCount = addThreeErrors()
if errCount != 1 {
t.Errorf("Expected limited errors (1), got %d", errCount)
}

// Check limit matching count
MaxPartErrors = 3
errCount = addThreeErrors()
if errCount != 3 {
t.Errorf("Expected limited errors (3), got %d", errCount)
}
}

func TestErrorLimitOption(t *testing.T) {
// Backup global variable
originalMaxPartErros := MaxPartErrors
defer func() {
MaxPartErrors = originalMaxPartErros
}()

addThreeErrors := func(parser *Parser) int {
part := NewPart("text/plain")
if parser != nil {
Expand All @@ -205,13 +159,6 @@ func TestErrorLimitOption(t *testing.T) {
got = addThreeErrors(NewParser())
assert.Equal(t, want, got, "expected unlimited errors")

// Check the default actually comes from deprecated MaxPartErrors global.
want = 1
MaxPartErrors = want
got = addThreeErrors(nil)
assert.Equal(t, want, got, "expected limited errors")
MaxPartErrors = 0

// Check limit.
want = 1
got = addThreeErrors(NewParser(MaxStoredPartErrors(want)))
Expand Down
3 changes: 1 addition & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func MaxStoredPartErrors(n int) Option {
type maxStoredPartErrorsOption int

func (o maxStoredPartErrorsOption) apply(p *Parser) {
n := int(o)
p.maxStoredPartErrors = &n
p.maxStoredPartErrors = int(o)
}

// RawContent if set to true will not try to decode the CTE and return the raw part content.
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CustomParseMediaType func(ctype string) (mtype string, params map[string]st

// Parser parses MIME. Create with NewParser to inherit recommended defaults.
type Parser struct {
maxStoredPartErrors *int // TODO: Pointer until global var removed.
maxStoredPartErrors int
multipartWOBoundaryAsSinglePart bool
readPartErrorPolicy ReadPartErrorPolicy
skipMalformedParts bool
Expand Down
Loading