Repository: casbin/casbin
Pull request #509 (2020-07-02 15:09:24)

fix: remove unwanted AddMatchingFunc and AddDomainMatchingFunc name argument.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/31828799/badge)](https://coveralls.io/builds/31828799)

Coverage remained the same at 75.448% when pulling **8d3f350b0f2f7ef9c3cd82585e2d36b5116886e0 on divy9881:add_matching_func** into **a5d65bea854ccfa2d9b802813bbcc9323b9e1acd on casbin:master**.

nodece: LGTM, but it is breaking changes.
hsluoyz: Frozen for breaking change.
nodece: @divy9881 Could you make a new PR that merge your code to releae-3.0.0 branch?

Patch:
diff --git a/rbac/default-role-manager/role_manager.go b/rbac/default-role-manager/role_manager.go
index f81b21a..f0a1898 100644
--- a/rbac/default-role-manager/role_manager.go
+++ b/rbac/default-role-manager/role_manager.go
@@ -51,13 +51,13 @@ func NewRoleManager(maxHierarchyLevel int) rbac.RoleManager {
 }
 
 // AddMatchingFunc support use pattern in g
-func (rm *RoleManager) AddMatchingFunc(name string, fn MatchingFunc) {
+func (rm *RoleManager) AddMatchingFunc(fn MatchingFunc) {
 	rm.hasPattern = true
 	rm.matchingFunc = fn
 }
 
 // AddDomainMatchingFunc support use domain pattern in g
-func (rm *RoleManager) AddDomainMatchingFunc(name string, fn MatchingFunc) {
+func (rm *RoleManager) AddDomainMatchingFunc(fn MatchingFunc) {
 	rm.hasDomainPattern = true
 	rm.domainMatchingFunc = fn
 }


Test patch:
diff --git a/model_test.go b/model_test.go
index 70ef865..7018830 100644
--- a/model_test.go
+++ b/model_test.go
@@ -296,7 +296,7 @@ func TestRBACModelWithPattern(t *testing.T) {
 	// You can see in policy that: "g2, /book/:id, book_group", so in "g2()" function in the matcher, instead
 	// of checking whether "/book/:id" equals the obj: "/book/1", it checks whether the pattern matches.
 	// You can see it as normal RBAC: "/book/:id" == "/book/1" becomes KeyMatch2("/book/:id", "/book/1")
-	e.rm.(*defaultrolemanager.RoleManager).AddMatchingFunc("KeyMatch2", util.KeyMatch2)
+	e.rm.(*defaultrolemanager.RoleManager).AddMatchingFunc(util.KeyMatch2)
 	testEnforce(t, e, "alice", "/book/1", "GET", true)
 	testEnforce(t, e, "alice", "/book/2", "GET", true)
 	testEnforce(t, e, "alice", "/pen/1", "GET", true)
@@ -308,7 +308,7 @@ func TestRBACModelWithPattern(t *testing.T) {
 
 	// AddMatchingFunc() is actually setting a function because only one function is allowed,
 	// so when we set "KeyMatch3", we are actually replacing "KeyMatch2" with "KeyMatch3".
-	e.rm.(*defaultrolemanager.RoleManager).AddMatchingFunc("KeyMatch3", util.KeyMatch3)
+	e.rm.(*defaultrolemanager.RoleManager).AddMatchingFunc(util.KeyMatch3)
 	testEnforce(t, e, "alice", "/book2/1", "GET", true)
 	testEnforce(t, e, "alice", "/book2/2", "GET", true)
 	testEnforce(t, e, "alice", "/pen2/1", "GET", true)
@@ -575,7 +575,7 @@ func TestCommentModel(t *testing.T) {
 
 func TestDomainMatchModel(t *testing.T) {
 	e, _ := NewEnforcer("examples/rbac_with_domain_pattern_model.conf", "examples/rbac_with_domain_pattern_policy.csv")
-	e.rm.(*defaultrolemanager.RoleManager).AddDomainMatchingFunc("keyMatch2", util.KeyMatch2)
+	e.rm.(*defaultrolemanager.RoleManager).AddDomainMatchingFunc(util.KeyMatch2)
 
 	testDomainEnforce(t, e, "alice", "domain1", "data1", "read", true)
 	testDomainEnforce(t, e, "alice", "domain1", "data1", "write", true)
@@ -591,8 +591,8 @@ func TestDomainMatchModel(t *testing.T) {
 
 func TestAllMatchModel(t *testing.T) {
 	e, _ := NewEnforcer("examples/rbac_with_all_pattern_model.conf", "examples/rbac_with_all_pattern_policy.csv")
-	e.rm.(*defaultrolemanager.RoleManager).AddMatchingFunc("keyMatch2", util.KeyMatch2)
-	e.rm.(*defaultrolemanager.RoleManager).AddDomainMatchingFunc("keyMatch2", util.KeyMatch2)
+	e.rm.(*defaultrolemanager.RoleManager).AddMatchingFunc(util.KeyMatch2)
+	e.rm.(*defaultrolemanager.RoleManager).AddDomainMatchingFunc(util.KeyMatch2)
 
 	testDomainEnforce(t, e, "alice", "domain1", "/book/1", "read", true)
 	testDomainEnforce(t, e, "alice", "domain1", "/book/1", "write", false)
diff --git a/rbac/default-role-manager/role_manager_test.go b/rbac/default-role-manager/role_manager_test.go
index 4413385..690193f 100644
--- a/rbac/default-role-manager/role_manager_test.go
+++ b/rbac/default-role-manager/role_manager_test.go
@@ -233,7 +233,7 @@ func TestClear(t *testing.T) {
 
 func TestDomainPartternRole(t *testing.T) {
 	rm := NewRoleManager(10)
-	rm.(*RoleManager).AddDomainMatchingFunc("keyMatch2", util.KeyMatch2)
+	rm.(*RoleManager).AddDomainMatchingFunc(util.KeyMatch2)
 
 	_ = rm.AddLink("u1", "g1", "domain1")
 	_ = rm.AddLink("u2", "g1", "domain2")
@@ -263,8 +263,8 @@ func TestDomainPartternRole(t *testing.T) {
 
 func TestAllMatchingFunc(t *testing.T) {
 	rm := NewRoleManager(10)
-	rm.(*RoleManager).AddMatchingFunc("keyMatch2", util.KeyMatch2)
-	rm.(*RoleManager).AddDomainMatchingFunc("keyMatch2", util.KeyMatch2)
+	rm.(*RoleManager).AddMatchingFunc(util.KeyMatch2)
+	rm.(*RoleManager).AddDomainMatchingFunc(util.KeyMatch2)
 
 	_ = rm.AddLink("/book/:id", "book_group", "*")
 	// Current role inheritance tree after deleting the links:
diff --git a/rbac_api_test.go b/rbac_api_test.go
index 882789a..44d4713 100644
--- a/rbac_api_test.go
+++ b/rbac_api_test.go
@@ -226,7 +226,7 @@ func TestImplicitRoleAPI(t *testing.T) {
 
 	e, _ = NewEnforcer("examples/rbac_with_pattern_model.conf", "examples/rbac_with_pattern_policy.csv")
 
-	e.GetRoleManager().(*defaultrolemanager.RoleManager).AddMatchingFunc("matcher", util.KeyMatch)
+	e.GetRoleManager().(*defaultrolemanager.RoleManager).AddMatchingFunc(util.KeyMatch)
 
 	testGetImplicitRoles(t, e, "cathy", []string{"/book/1/2/3/4/5", "pen_admin", "/book/*", "book_group"})
 	testGetRoles(t, e, []string{"/book/1/2/3/4/5", "pen_admin"}, "cathy")