mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(filters): improve rejection handling (#1776)
* feat(filters): improve rejection handling * fix(filters): rejection tests * fix(filters): size check error rejection
This commit is contained in:
parent
ae779e5461
commit
f029de233f
8 changed files with 586 additions and 311 deletions
140
internal/domain/rejections.go
Normal file
140
internal/domain/rejections.go
Normal file
|
@ -0,0 +1,140 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Rejection struct {
|
||||
key string
|
||||
got any
|
||||
want any
|
||||
format string
|
||||
}
|
||||
|
||||
type RejectionReasons struct {
|
||||
data []Rejection
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) Len() int {
|
||||
return len(r.data)
|
||||
}
|
||||
|
||||
func NewRejectionReasons() *RejectionReasons {
|
||||
return &RejectionReasons{
|
||||
data: make([]Rejection, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) String() string {
|
||||
if len(r.data) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
builder := strings.Builder{}
|
||||
for i, rejection := range r.data {
|
||||
if i > 0 {
|
||||
builder.WriteString(", ")
|
||||
}
|
||||
|
||||
if rejection.format != "" {
|
||||
fmt.Fprintf(&builder, rejection.format, rejection.key, rejection.got, rejection.want)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Fprintf(&builder, "[%s] not matching: got %v want: %v", rejection.key, rejection.got, rejection.want)
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) StringTruncated() string {
|
||||
if len(r.data) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
builder := strings.Builder{}
|
||||
for i, rejection := range r.data {
|
||||
got := rejection.got
|
||||
switch v := rejection.got.(type) {
|
||||
case string:
|
||||
if len(v) > 1024 {
|
||||
got = v[:1024]
|
||||
}
|
||||
}
|
||||
|
||||
want := rejection.want
|
||||
switch v := rejection.want.(type) {
|
||||
case string:
|
||||
if len(v) > 1024 {
|
||||
want = v[:1024]
|
||||
}
|
||||
}
|
||||
|
||||
if i > 0 {
|
||||
builder.WriteString(", ")
|
||||
}
|
||||
fmt.Fprintf(&builder, "[%s] not matching: got %v want: %v", rejection.key, got, want)
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) WriteString() string {
|
||||
var output []string
|
||||
for _, rejection := range r.data {
|
||||
output = append(output, fmt.Sprintf("[%s] not matching: got %v want: %v", rejection.key, rejection.got, rejection.want))
|
||||
}
|
||||
|
||||
return strings.Join(output, ", ")
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) WriteJSON() ([]byte, error) {
|
||||
var output map[string]string
|
||||
for _, rejection := range r.data {
|
||||
output[rejection.key] = fmt.Sprintf("[%s] not matching: got %v want: %v", rejection.key, rejection.got, rejection.want)
|
||||
}
|
||||
|
||||
return json.Marshal(output)
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) Add(key string, got any, want any) {
|
||||
r.data = append(r.data, Rejection{
|
||||
key: key,
|
||||
got: got,
|
||||
want: want,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) Addf(key string, format string, got any, want any) {
|
||||
r.data = append(r.data, Rejection{
|
||||
key: key,
|
||||
format: format,
|
||||
got: got,
|
||||
want: want,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *RejectionReasons) AddTruncated(key string, got any, want any) {
|
||||
switch wanted := want.(type) {
|
||||
case string:
|
||||
if len(wanted) > 1024 {
|
||||
want = wanted[:1024]
|
||||
}
|
||||
|
||||
case []string:
|
||||
for i, s := range wanted {
|
||||
if len(s) > 1024 {
|
||||
wanted[i] = s[:1024]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
r.Add(key, got, want)
|
||||
}
|
||||
|
||||
// Clear rejections
|
||||
func (r *RejectionReasons) Clear() {
|
||||
r.data = make([]Rejection, 0)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue