This is an automated email from the ASF dual-hosted git repository.

hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-ent-adapter.git


The following commit(s) were added to refs/heads/master by this push:
     new 1d09704  fix: resolve golangci-lint errcheck and unused issues
1d09704 is described below

commit 1d09704e462551685d3768b6ce81c154780a332c
Author: Yang Luo <[email protected]>
AuthorDate: Thu Aug 6 17:42:52 2026 +0800

    fix: resolve golangci-lint errcheck and unused issues
---
 adapter.go      | 14 +++++++-----
 adapter_test.go | 68 ++++++++++++++++++++++++---------------------------------
 2 files changed, 37 insertions(+), 45 deletions(-)

diff --git a/adapter.go b/adapter.go
index d006068..2d29291 100644
--- a/adapter.go
+++ b/adapter.go
@@ -119,7 +119,9 @@ func (a *Adapter) LoadPolicy(model model.Model) error {
                return err
        }
        for _, policy := range policies {
-               loadPolicyLine(policy, model)
+               if err := loadPolicyLine(policy, model); err != nil {
+                       return err
+               }
        }
        return nil
 }
@@ -133,7 +135,7 @@ func (a *Adapter) LoadFilteredPolicy(model model.Model, 
filter interface{}) erro
                return fmt.Errorf("invalid filter type: %v", 
reflect.TypeOf(filter))
        }
 
-       session := a.client.CasbinRule.Query()
+       session := a.client.CasbinRule.Query().Order(ent.Asc("id"))
        if len(filterValue.Ptype) != 0 {
                session.Where(casbinrule.PtypeIn(filterValue.Ptype...))
        }
@@ -162,7 +164,9 @@ func (a *Adapter) LoadFilteredPolicy(model model.Model, 
filter interface{}) erro
        }
 
        for _, line := range lines {
-               loadPolicyLine(line, model)
+               if err := loadPolicyLine(line, model); err != nil {
+                       return err
+               }
        }
        a.filtered = true
 
@@ -324,7 +328,7 @@ func (a *Adapter) WithTx(fn func(tx *ent.Tx) error) error {
        return nil
 }
 
-func loadPolicyLine(line *ent.CasbinRule, model model.Model) {
+func loadPolicyLine(line *ent.CasbinRule, model model.Model) error {
        var p = []string{line.Ptype,
                line.V0, line.V1, line.V2, line.V3, line.V4, line.V5}
 
@@ -343,7 +347,7 @@ func loadPolicyLine(line *ent.CasbinRule, model 
model.Model) {
                lineText = strings.Join(p[:2], ", ")
        }
 
-       persist.LoadPolicyLine(lineText, model)
+       return persist.LoadPolicyLine(lineText, model)
 }
 
 func (a *Adapter) toInstance(ptype string, rule []string) *ent.CasbinRule {
diff --git a/adapter_test.go b/adapter_test.go
index c8d3412..0a5fe67 100644
--- a/adapter_test.go
+++ b/adapter_test.go
@@ -164,9 +164,10 @@ func testAutoSave(t *testing.T, a *Adapter) {
 
        // Because AutoSave is disabled, the policy change only affects the 
policy in Casbin enforcer,
        // it doesn't affect the policy in the storage.
-       e.AddPolicy("alice", "data1", "write")
+       _, err := e.AddPolicy("alice", "data1", "write")
+       assert.Nil(t, err)
        // Reload the policy from the storage to see the effect.
-       e.LoadPolicy()
+       assert.Nil(t, e.LoadPolicy())
        // This is still the original policy.
        testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", 
"data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", 
"write"}})
 
@@ -175,58 +176,40 @@ func testAutoSave(t *testing.T, a *Adapter) {
 
        // Because AutoSave is enabled, the policy change not only affects the 
policy in Casbin enforcer,
        // but also affects the policy in the storage.
-       e.AddPolicy("alice", "data1", "write")
+       _, err = e.AddPolicy("alice", "data1", "write")
+       assert.Nil(t, err)
        // Reload the policy from the storage to see the effect.
-       e.LoadPolicy()
+       assert.Nil(t, e.LoadPolicy())
        // 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"}, {"alice", "data1", "write"}})
 
        // Remove the added rule.
-       e.RemovePolicy("alice", "data1", "write")
-       e.LoadPolicy()
+       _, err = e.RemovePolicy("alice", "data1", "write")
+       assert.Nil(t, err)
+       assert.Nil(t, e.LoadPolicy())
        testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", 
"data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", 
"write"}})
 
        // Remove "data2_admin" related policy rules via a filter.
        // Two rules: {"data2_admin", "data2", "read"}, {"data2_admin", 
"data2", "write"} are deleted.
-       e.RemoveFilteredPolicy(0, "data2_admin")
-       e.LoadPolicy()
+       _, err = e.RemoveFilteredPolicy(0, "data2_admin")
+       assert.Nil(t, err)
+       assert.Nil(t, e.LoadPolicy())
        testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", 
"data2", "write"}})
 
-       e.RemovePolicies([][]string{{"alice", "data1", "read"}, {"bob", 
"data2", "write"}})
-       e.LoadPolicy()
+       _, err = e.RemovePolicies([][]string{{"alice", "data1", "read"}, 
{"bob", "data2", "write"}})
+       assert.Nil(t, err)
+       assert.Nil(t, e.LoadPolicy())
        testGetPolicy(t, e, [][]string{})
 }
 
-//func testFilteredPolicy(t *testing.T, a *Adapter) {
-//     // NewEnforcer() without an adapter will not auto load the policy
-//     e, _ := casbin.NewEnforcer("examples/rbac_model.conf")
-//     // Now set the adapter
-//     e.SetAdapter(a)
-//
-//     // Load only alice's policies
-//     assert.Nil(t, e.LoadFilteredPolicy(Filter{V0: []string{"alice"}}))
-//     testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}})
-//
-//     // Load only bob's policies
-//     assert.Nil(t, e.LoadFilteredPolicy(Filter{V0: []string{"bob"}}))
-//     testGetPolicy(t, e, [][]string{{"bob", "data2", "write"}})
-//
-//     // Load policies for data2_admin
-//     assert.Nil(t, e.LoadFilteredPolicy(Filter{V0: []string{"data2_admin"}}))
-//     testGetPolicy(t, e, [][]string{{"data2_admin", "data2", "read"}, 
{"data2_admin", "data2", "write"}})
-//
-//     // Load policies for alice and bob
-//     assert.Nil(t, e.LoadFilteredPolicy(Filter{V0: []string{"alice", 
"bob"}}))
-//     testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", 
"data2", "write"}})
-//}
-
 func testUpdatePolicy(t *testing.T, a *Adapter) {
        // NewEnforcer() will load the policy automatically.
        e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
 
        e.EnableAutoSave(true)
-       e.UpdatePolicy([]string{"alice", "data1", "read"}, []string{"alice", 
"data1", "write"})
-       e.LoadPolicy()
+       _, err := e.UpdatePolicy([]string{"alice", "data1", "read"}, 
[]string{"alice", "data1", "write"})
+       assert.Nil(t, err)
+       assert.Nil(t, e.LoadPolicy())
        testGetPolicy(t, e, [][]string{{"alice", "data1", "write"}, {"bob", 
"data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", 
"write"}})
 }
 
@@ -235,8 +218,9 @@ func testUpdatePolicies(t *testing.T, a *Adapter) {
        e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
 
        e.EnableAutoSave(true)
-       e.UpdatePolicies([][]string{{"alice", "data1", "write"}, {"bob", 
"data2", "write"}}, [][]string{{"alice", "data1", "read"}, {"bob", "data2", 
"read"}})
-       e.LoadPolicy()
+       _, err := e.UpdatePolicies([][]string{{"alice", "data1", "write"}, 
{"bob", "data2", "write"}}, [][]string{{"alice", "data1", "read"}, {"bob", 
"data2", "read"}})
+       assert.Nil(t, err)
+       assert.Nil(t, e.LoadPolicy())
        testGetPolicyWithoutOrder(t, e, [][]string{{"alice", "data1", "read"}, 
{"bob", "data2", "read"}, {"data2_admin", "data2", "read"}, {"data2_admin", 
"data2", "write"}})
 }
 
@@ -245,9 +229,11 @@ func testUpdateFilteredPolicies(t *testing.T, a *Adapter) {
        e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
 
        e.EnableAutoSave(true)
-       e.UpdateFilteredPolicies([][]string{{"alice", "data1", "write"}}, 0, 
"alice", "data1", "read")
-       e.UpdateFilteredPolicies([][]string{{"bob", "data2", "read"}}, 0, 
"bob", "data2")
-       e.LoadPolicy()
+       _, err := e.UpdateFilteredPolicies([][]string{{"alice", "data1", 
"write"}}, 0, "alice", "data1", "read")
+       assert.Nil(t, err)
+       _, err = e.UpdateFilteredPolicies([][]string{{"bob", "data2", "read"}}, 
0, "bob", "data2")
+       assert.Nil(t, err)
+       assert.Nil(t, e.LoadPolicy())
        testGetPolicyWithoutOrder(t, e, [][]string{{"alice", "data1", "write"}, 
{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"bob", 
"data2", "read"}})
 }
 
@@ -306,9 +292,11 @@ func TestAdapters(t *testing.T) {
        testUpdatePolicy(t, a)
        testUpdatePolicies(t, a)
        testUpdateFilteredPolicies(t, a)
+       testFilteredPolicy(t, a)
 
        a = initAdapter(t, "postgres", "user=postgres password=postgres 
host=127.0.0.1 port=5432 sslmode=disable dbname=casbin")
        testUpdatePolicy(t, a)
        testUpdatePolicies(t, a)
        testUpdateFilteredPolicies(t, a)
+       testFilteredPolicy(t, a)
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to