Skip to content

Commit

Permalink
feat: pass context down to xorm (#65)
Browse files Browse the repository at this point in the history
* feat: pass context down to xorm

* fix: update README
  • Loading branch information
MuZhou233 authored Apr 9, 2024
1 parent a5eaefe commit 634e6df
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 249 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func main() {
`xormadapter` supports adapter with context, the following is a timeout control implemented using context

```go
ca, _ := NewContextAdapter("mysql", "root:@tcp(127.0.0.1:3306)/", "casbin")
a, _ := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source.
// Limited time 300s
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
err := ca.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
err := a.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
if err != nil {
panic(err)
}
Expand Down
36 changes: 31 additions & 5 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package xormadapter

import (
"context"
"errors"
"log"
"runtime"
Expand Down Expand Up @@ -271,9 +272,14 @@ func loadPolicyLine(line *CasbinRule, model model.Model) {

// LoadPolicy loads policy from database.
func (a *Adapter) LoadPolicy(model model.Model) error {
return a.LoadPolicyCtx(context.Background(), model)
}

// LoadPolicyCtx loads policy from database.
func (a *Adapter) LoadPolicyCtx(ctx context.Context, model model.Model) error {
lines := make([]*CasbinRule, 0, 64)

if err := a.engine.Table(&CasbinRule{tableName: a.getFullTableName()}).Find(&lines); err != nil {
if err := a.engine.Context(ctx).Table(&CasbinRule{tableName: a.getFullTableName()}).Find(&lines); err != nil {
return err
}

Expand Down Expand Up @@ -312,6 +318,11 @@ func (a *Adapter) genPolicyLine(ptype string, rule []string) *CasbinRule {

// SavePolicy saves policy to database.
func (a *Adapter) SavePolicy(model model.Model) error {
return a.SavePolicyCtx(context.Background(), model)
}

// SavePolicyCtx saves policy to database.
func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
err := a.dropTable()
if err != nil {
return err
Expand Down Expand Up @@ -342,15 +353,20 @@ func (a *Adapter) SavePolicy(model model.Model) error {
return nil
}

_, err = a.engine.Insert(&lines)
_, err = a.engine.Context(ctx).Insert(&lines)

return err
}

// AddPolicy adds a policy rule to the storage.
func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
return a.AddPolicyCtx(context.Background(), sec, ptype, rule)
}

// AddPolicyCtx adds a policy rule to the storage.
func (a *Adapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
line := a.genPolicyLine(ptype, rule)
_, err := a.engine.InsertOne(line)
_, err := a.engine.Context(ctx).InsertOne(line)
return err
}

Expand All @@ -371,8 +387,13 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error

// RemovePolicy removes a policy rule from the storage.
func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
return a.RemovePolicyCtx(context.Background(), sec, ptype, rule)
}

// RemovePolicyCtx removes a policy rule from the storage.
func (a *Adapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
line := a.genPolicyLine(ptype, rule)
_, err := a.engine.Delete(line)
_, err := a.engine.Context(ctx).Delete(line)
return err
}

Expand All @@ -393,6 +414,11 @@ func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) err

// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return a.RemoveFilteredPolicyCtx(context.Background(), sec, ptype, fieldIndex, fieldValues...)
}

// RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error {
line := CasbinRule{Ptype: ptype, tableName: a.getFullTableName()}

idx := fieldIndex + len(fieldValues)
Expand All @@ -415,7 +441,7 @@ func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int,
line.V5 = fieldValues[5-fieldIndex]
}

_, err := a.engine.Delete(&line)
_, err := a.engine.Context(ctx).Delete(&line)
return err
}

Expand Down
86 changes: 0 additions & 86 deletions context_adapter.go

This file was deleted.

152 changes: 0 additions & 152 deletions context_adapter_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module github.com/casbin/xorm-adapter/v3
go 1.12

require (
github.com/agiledragon/gomonkey/v2 v2.10.1
github.com/casbin/casbin/v2 v2.77.2
github.com/go-sql-driver/mysql v1.6.0
github.com/lib/pq v1.10.2
github.com/stretchr/testify v1.7.0
xorm.io/xorm v1.3.2
)
Loading

0 comments on commit 634e6df

Please sign in to comment.