Skip to content

Commit

Permalink
Implement conforming :matches and remove some duplicates code
Browse files Browse the repository at this point in the history
  • Loading branch information
foxcpp committed Jan 29, 2024
1 parent b90cd97 commit 2564207
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 387 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ module github.com/foxcpp/go-sieve

go 1.20

require (
github.com/davecgh/go-spew v1.1.1
)
require github.com/davecgh/go-spew v1.1.1

require (
github.com/emersion/go-message v0.18.0 // indirect
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 // indirect
rsc.io/binaryregexp v0.2.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
rsc.io/binaryregexp v0.2.0 h1:HfqmD5MEmC0zvwBuF187nq9mdnXjXsSivRiXN7SmRkE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
136 changes: 32 additions & 104 deletions interp/load_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,12 @@ import (

func loadAddressTest(s *Script, test parser.Test) (Test, error) {
loaded := AddressTest{
Comparator: DefaultComparator,
matcherTest: newMatcherTest(),
AddressPart: All,
Match: MatchIs,
}
err := LoadSpec(s, &Spec{
var key []string
err := LoadSpec(s, loaded.addSpecTags(&Spec{
Tags: map[string]SpecTag{
"comparator": {
NeedsValue: true,
MinStrCount: 1,
MaxStrCount: 1,
MatchStr: func(val []string) {
loaded.Comparator = Comparator(val[0])
},
},
"is": {
MatchBool: func() {
loaded.Match = MatchIs
},
},
"contains": {
MatchBool: func() {
loaded.Match = MatchContains
},
},
"matches": {
MatchBool: func() {
loaded.Match = MatchMatches
},
},
"all": {
MatchBool: func() {
loaded.AddressPart = All
Expand All @@ -62,19 +39,21 @@ func loadAddressTest(s *Script, test parser.Test) (Test, error) {
},
{
MatchStr: func(val []string) {
loaded.Key = val
key = val
},
MinStrCount: 1,
},
},
}, test.Position, test.Args, test.Tests, nil)
switch loaded.Comparator {
case ComparatorOctet, ComparatorUnicodeCaseMap,
ComparatorASCIICaseMap, ComparatorASCIINumeric:
default:
return nil, fmt.Errorf("unsupported comparator: %v", loaded.Comparator)
}), test.Position, test.Args, test.Tests, nil)
if err != nil {
return nil, err
}
return loaded, err

if err := loaded.setKey(s, key); err != nil {
return nil, err
}

return loaded, nil
}

func loadAllOfTest(s *Script, test parser.Test) (Test, error) {
Expand Down Expand Up @@ -103,36 +82,14 @@ func loadEnvelopeTest(s *Script, test parser.Test) (Test, error) {
if !s.RequiresExtension("envelope") {
return nil, fmt.Errorf("missing require 'envelope'")
}

loaded := EnvelopeTest{
Comparator: DefaultComparator,
matcherTest: newMatcherTest(),
AddressPart: All,
Match: MatchIs,
}
err := LoadSpec(s, &Spec{
var key []string
err := LoadSpec(s, loaded.addSpecTags(&Spec{
Tags: map[string]SpecTag{
"comparator": {
NeedsValue: true,
MinStrCount: 1,
MaxStrCount: 1,
MatchStr: func(val []string) {
loaded.Comparator = Comparator(val[0])
},
},
"is": {
MatchBool: func() {
loaded.Match = MatchIs
},
},
"contains": {
MatchBool: func() {
loaded.Match = MatchContains
},
},
"matches": {
MatchBool: func() {
loaded.Match = MatchMatches
},
},
"all": {
MatchBool: func() {
loaded.AddressPart = All
Expand All @@ -158,21 +115,20 @@ func loadEnvelopeTest(s *Script, test parser.Test) (Test, error) {
},
{
MatchStr: func(val []string) {
loaded.Key = val
key = val
},
MinStrCount: 1,
},
},
}, test.Position, test.Args, test.Tests, nil)
}), test.Position, test.Args, test.Tests, nil)
if err != nil {
return nil, err
}
switch loaded.Comparator {
case ComparatorOctet, ComparatorUnicodeCaseMap,
ComparatorASCIICaseMap, ComparatorASCIINumeric:
default:
return nil, fmt.Errorf("unsupported comparator: %v", loaded.Comparator)

if err := loaded.setKey(s, key); err != nil {
return nil, err
}

return loaded, nil
}

Expand Down Expand Up @@ -204,36 +160,9 @@ func loadTrueTest(s *Script, test parser.Test) (Test, error) {
}

func loadHeaderTest(s *Script, test parser.Test) (Test, error) {
loaded := HeaderTest{
Comparator: DefaultComparator,
Match: MatchIs,
}
err := LoadSpec(s, &Spec{
Tags: map[string]SpecTag{
"comparator": {
NeedsValue: true,
MinStrCount: 1,
MaxStrCount: 1,
MatchStr: func(val []string) {
loaded.Comparator = Comparator(val[0])
},
},
"is": {
MatchBool: func() {
loaded.Match = MatchIs
},
},
"contains": {
MatchBool: func() {
loaded.Match = MatchContains
},
},
"matches": {
MatchBool: func() {
loaded.Match = MatchMatches
},
},
},
loaded := HeaderTest{matcherTest: newMatcherTest()}
var key []string
err := LoadSpec(s, loaded.addSpecTags(&Spec{
Pos: []SpecPosArg{
{
MatchStr: func(val []string) {
Expand All @@ -243,21 +172,20 @@ func loadHeaderTest(s *Script, test parser.Test) (Test, error) {
},
{
MatchStr: func(val []string) {
loaded.Key = val
key = val
},
MinStrCount: 1,
},
},
}, test.Position, test.Args, test.Tests, nil)
}), test.Position, test.Args, test.Tests, nil)
if err != nil {
return nil, err
}
switch loaded.Comparator {
case ComparatorOctet, ComparatorUnicodeCaseMap,
ComparatorASCIICaseMap, ComparatorASCIINumeric:
default:
return nil, fmt.Errorf("unsupported comparator: %v", loaded.Comparator)

if err := loaded.setKey(s, key); err != nil {
return nil, err
}

return loaded, nil
}

Expand Down
47 changes: 10 additions & 37 deletions interp/load_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,10 @@ func loadStringTest(s *Script, test parser.Test) (Test, error) {
if !s.RequiresExtension("variables") {
return nil, fmt.Errorf("missing require 'variables'")
}
loaded := TestString{
Comparator: DefaultComparator,
Match: MatchIs,
}
err := LoadSpec(s, &Spec{
Tags: map[string]SpecTag{
"comparator": {
NeedsValue: true,
MinStrCount: 1,
MaxStrCount: 1,
MatchStr: func(val []string) {
loaded.Comparator = Comparator(val[0])
},
},
"is": {
MatchBool: func() {
loaded.Match = MatchIs
},
},
"contains": {
MatchBool: func() {
loaded.Match = MatchContains
},
},
"matches": {
MatchBool: func() {
loaded.Match = MatchMatches
},
},
},

loaded := TestString{matcherTest: newMatcherTest()}
var key []string
err := LoadSpec(s, loaded.addSpecTags(&Spec{
Pos: []SpecPosArg{
{
MatchStr: func(val []string) {
Expand All @@ -216,20 +190,19 @@ func loadStringTest(s *Script, test parser.Test) (Test, error) {
},
{
MatchStr: func(val []string) {
loaded.Key = val
key = val
},
MinStrCount: 1,
},
},
}, test.Position, test.Args, test.Tests, nil)
}), test.Position, test.Args, test.Tests, nil)
if err != nil {
return nil, err
}
switch loaded.Comparator {
case ComparatorOctet, ComparatorUnicodeCaseMap,
ComparatorASCIICaseMap, ComparatorASCIINumeric:
default:
return nil, fmt.Errorf("unsupported comparator: %v", loaded.Comparator)

if err := loaded.setKey(s, key); err != nil {
return nil, err
}

return loaded, nil
}
Loading

0 comments on commit 2564207

Please sign in to comment.