Skip to content

Commit c5e78f9

Browse files
committed
fix: wildcard matching
1 parent 7bb14e3 commit c5e78f9

2 files changed

Lines changed: 49 additions & 46 deletions

File tree

ucan/delegation/policy/match.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func MatchStatement(statement ucan.Statement, value any) (bool, error) {
132132
if !ok {
133133
return false, NewMatchError(statement, fmt.Errorf(`matching "%s": "%v" is not applicable to operator "%s"`, s.Selector(), one, OpLike))
134134
}
135-
if s.glob.Match(v) {
135+
if !s.glob.Match(v) {
136136
return false, NewMatchError(statement, fmt.Errorf(`matching "%s": "%v" is not like "%v"`, s.Selector(), one, s.model.Pattern))
137137
}
138138
return true, nil

ucan/delegation/policy/match_test.go

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,54 @@ func TestMatch(t *testing.T) {
356356
value: 138,
357357
match: false,
358358
},
359+
{
360+
name: "wildcard match",
361+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
362+
value: "Alice*, Bob, Carol.",
363+
match: true,
364+
},
365+
{
366+
name: "wildcard match",
367+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
368+
value: "Alice*, Bob, Dan, Erin, Carol.",
369+
match: true,
370+
},
371+
{
372+
name: "wildcard match",
373+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
374+
value: "Alice*, Bob , Carol.",
375+
match: true,
376+
},
377+
{
378+
name: "wildcard match",
379+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
380+
value: "Alice*, Bob*, Carol.",
381+
match: true,
382+
},
383+
{
384+
name: "wildcard no match",
385+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
386+
value: "Alice*, Bob, Carol",
387+
match: false,
388+
},
389+
{
390+
name: "wildcard no match",
391+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
392+
value: "Alice*, Bob*, Carol!",
393+
match: false,
394+
},
395+
{
396+
name: "wildcard no match",
397+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
398+
value: "Alice, Bob, Carol.",
399+
match: false,
400+
},
401+
{
402+
name: "wildcard no match",
403+
policy: policy.Like(".", `Alice\*, Bob*, Carol.`),
404+
value: " Alice*, Bob, Carol. ",
405+
match: false,
406+
},
359407
}
360408

361409
for _, tc := range testCases {
@@ -373,51 +421,6 @@ func TestMatch(t *testing.T) {
373421
})
374422
}
375423

376-
// t.Run("wildcard", func(t *testing.T) {
377-
// glb, err := glob.Compile(`Alice\*, Bob*, Carol.`)
378-
// require.NoError(t, err)
379-
380-
// for _, s := range []string{
381-
// "Alice*, Bob, Carol.",
382-
// "Alice*, Bob, Dan, Erin, Carol.",
383-
// "Alice*, Bob , Carol.",
384-
// "Alice*, Bob*, Carol.",
385-
// } {
386-
// func(s string) {
387-
// t.Run(fmt.Sprintf("pass %s", s), func(t *testing.T) {
388-
// np := basicnode.Prototype.String
389-
// nb := np.NewBuilder()
390-
// nb.AssignString(s)
391-
// nd := nb.Build()
392-
393-
// pol := policy.Policy{Like(mustParse(t, "."), glb)}
394-
// ok, err := policy.Match(pol, nd)
395-
// require.True(t, ok)
396-
// })
397-
// }(s)
398-
// }
399-
400-
// for _, s := range []string{
401-
// "Alice*, Bob, Carol",
402-
// "Alice*, Bob*, Carol!",
403-
// "Alice, Bob, Carol.",
404-
// " Alice*, Bob, Carol. ",
405-
// } {
406-
// func(s string) {
407-
// t.Run(fmt.Sprintf("fail %s", s), func(t *testing.T) {
408-
// np := basicnode.Prototype.String
409-
// nb := np.NewBuilder()
410-
// nb.AssignString(s)
411-
// nd := nb.Build()
412-
413-
// pol := policy.Policy{Like(mustParse(t, "."), glb)}
414-
// ok, err := policy.Match(pol, nd)
415-
// require.False(t, ok)
416-
// })
417-
// }(s)
418-
// }
419-
// })
420-
421424
// t.Run("quantification", func(t *testing.T) {
422425
// buildValueNode := func(v int64) ipld.Node {
423426
// np := basicnode.Prototype.Map

0 commit comments

Comments
 (0)