Skip to content

Commit

Permalink
chore!: remove deprecated MaxPartErrors global var (#353)
Browse files Browse the repository at this point in the history
Signed-off-by: James Hillyerd <james@hillyerd.com>
  • Loading branch information
jhillyerd authored Sep 7, 2024
1 parent e563ba5 commit 10d32da
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 65 deletions.
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

0 comments on commit 10d32da

Please sign in to comment.