Skip to content

Commit

Permalink
fix: handle the returned error of LoadPolicyArray (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
JalinWang authored Sep 16, 2022
1 parent e114641 commit e12d797
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
9 changes: 6 additions & 3 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (a *adapter) dropTable() error {
return nil
}

func loadPolicyLine(line CasbinRule, model model.Model) {
func loadPolicyLine(line CasbinRule, model model.Model) error {
var p = []string{line.PType,
line.V0, line.V1, line.V2, line.V3, line.V4, line.V5}
var lineText string
Expand All @@ -241,7 +241,7 @@ func loadPolicyLine(line CasbinRule, model model.Model) {
lineText = strings.Join(p[:2], ", ")
}

persist.LoadPolicyLine(lineText, model)
return persist.LoadPolicyLine(lineText, model)
}

// LoadPolicy loads policy from database.
Expand Down Expand Up @@ -273,7 +273,10 @@ func (a *adapter) LoadFilteredPolicy(model model.Model, filter interface{}) erro
if err != nil {
return err
}
loadPolicyLine(line, model)
err = loadPolicyLine(line, model)
if err != nil {
return err
}
}

return cursor.Close(ctx)
Expand Down
17 changes: 2 additions & 15 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestAddPolicies(t *testing.T) {
}

func TestDeleteFilteredAdapter(t *testing.T) {
a, err := NewFilteredAdapter(getDbURL())
a, err := NewFilteredAdapter(getDbURL() + "/casbin_test_new")
if err != nil {
panic(err)
}
Expand All @@ -329,10 +329,6 @@ func TestDeleteFilteredAdapter(t *testing.T) {
}
// The policy has a new rule: {"alice", "data1", "write"}.
testGetPolicy(t, e, [][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"domain1", "alice", "data3", "read", "accept", "service1"},
{"domain1", "alice", "data3", "write", "accept", "service2"},
},
Expand All @@ -343,10 +339,6 @@ func TestDeleteFilteredAdapter(t *testing.T) {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
testGetPolicy(t, e, [][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"domain1", "alice", "data3", "write", "accept", "service2"},
},
)
Expand All @@ -355,12 +347,7 @@ func TestDeleteFilteredAdapter(t *testing.T) {
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
testGetPolicy(t, e, [][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
},
testGetPolicy(t, e, [][]string{},
)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/casbin/mongodb-adapter/v3
go 1.13

require (
github.com/casbin/casbin/v2 v2.31.2
github.com/casbin/casbin/v2 v2.55.0
go.mongodb.org/mongo-driver v1.5.3
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/aws/aws-sdk-go v1.34.28 h1:sscPpn/Ns3i0F4HPEWAVcwdIRaZZCuL7llJ2/60yPIk=
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/casbin/casbin/v2 v2.31.2 h1:L2RDbKhkspfUPkY12PvJ2oN2QtBQhJ5oV6bq9QfKGHo=
github.com/casbin/casbin/v2 v2.31.2/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/casbin/casbin/v2 v2.55.0 h1:RyU+OacnVzjxof1U3bmxHM7oCRdx9+gNnkclrvof/zI=
github.com/casbin/casbin/v2 v2.55.0/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit e12d797

Please sign in to comment.