Skip to content

Commit 634e6df

Browse files
authored
feat: pass context down to xorm (#65)
* feat: pass context down to xorm * fix: update README
1 parent a5eaefe commit 634e6df

File tree

6 files changed

+41
-249
lines changed

6 files changed

+41
-249
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ func main() {
106106
`xormadapter` supports adapter with context, the following is a timeout control implemented using context
107107

108108
```go
109-
ca, _ := NewContextAdapter("mysql", "root:@tcp(127.0.0.1:3306)/", "casbin")
109+
a, _ := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source.
110110
// Limited time 300s
111111
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
112112
defer cancel()
113-
err := ca.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
113+
err := a.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
114114
if err != nil {
115115
panic(err)
116116
}

adapter.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package xormadapter
1616

1717
import (
18+
"context"
1819
"errors"
1920
"log"
2021
"runtime"
@@ -271,9 +272,14 @@ func loadPolicyLine(line *CasbinRule, model model.Model) {
271272

272273
// LoadPolicy loads policy from database.
273274
func (a *Adapter) LoadPolicy(model model.Model) error {
275+
return a.LoadPolicyCtx(context.Background(), model)
276+
}
277+
278+
// LoadPolicyCtx loads policy from database.
279+
func (a *Adapter) LoadPolicyCtx(ctx context.Context, model model.Model) error {
274280
lines := make([]*CasbinRule, 0, 64)
275281

276-
if err := a.engine.Table(&CasbinRule{tableName: a.getFullTableName()}).Find(&lines); err != nil {
282+
if err := a.engine.Context(ctx).Table(&CasbinRule{tableName: a.getFullTableName()}).Find(&lines); err != nil {
277283
return err
278284
}
279285

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

313319
// SavePolicy saves policy to database.
314320
func (a *Adapter) SavePolicy(model model.Model) error {
321+
return a.SavePolicyCtx(context.Background(), model)
322+
}
323+
324+
// SavePolicyCtx saves policy to database.
325+
func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
315326
err := a.dropTable()
316327
if err != nil {
317328
return err
@@ -342,15 +353,20 @@ func (a *Adapter) SavePolicy(model model.Model) error {
342353
return nil
343354
}
344355

345-
_, err = a.engine.Insert(&lines)
356+
_, err = a.engine.Context(ctx).Insert(&lines)
346357

347358
return err
348359
}
349360

350361
// AddPolicy adds a policy rule to the storage.
351362
func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
363+
return a.AddPolicyCtx(context.Background(), sec, ptype, rule)
364+
}
365+
366+
// AddPolicyCtx adds a policy rule to the storage.
367+
func (a *Adapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
352368
line := a.genPolicyLine(ptype, rule)
353-
_, err := a.engine.InsertOne(line)
369+
_, err := a.engine.Context(ctx).InsertOne(line)
354370
return err
355371
}
356372

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

372388
// RemovePolicy removes a policy rule from the storage.
373389
func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
390+
return a.RemovePolicyCtx(context.Background(), sec, ptype, rule)
391+
}
392+
393+
// RemovePolicyCtx removes a policy rule from the storage.
394+
func (a *Adapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
374395
line := a.genPolicyLine(ptype, rule)
375-
_, err := a.engine.Delete(line)
396+
_, err := a.engine.Context(ctx).Delete(line)
376397
return err
377398
}
378399

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

394415
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
395416
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
417+
return a.RemoveFilteredPolicyCtx(context.Background(), sec, ptype, fieldIndex, fieldValues...)
418+
}
419+
420+
// RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage.
421+
func (a *Adapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error {
396422
line := CasbinRule{Ptype: ptype, tableName: a.getFullTableName()}
397423

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

418-
_, err := a.engine.Delete(&line)
444+
_, err := a.engine.Context(ctx).Delete(&line)
419445
return err
420446
}
421447

context_adapter.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

context_adapter_test.go

Lines changed: 0 additions & 152 deletions
This file was deleted.

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ module github.com/casbin/xorm-adapter/v3
33
go 1.12
44

55
require (
6-
github.com/agiledragon/gomonkey/v2 v2.10.1
76
github.com/casbin/casbin/v2 v2.77.2
87
github.com/go-sql-driver/mysql v1.6.0
98
github.com/lib/pq v1.10.2
10-
github.com/stretchr/testify v1.7.0
119
xorm.io/xorm v1.3.2
1210
)

0 commit comments

Comments
 (0)