• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

casbin / xorm-adapter / 6095110253

06 Sep 2023 09:13AM UTC coverage: 73.461% (+2.7%) from 70.737%
6095110253

push

github

web-flow
feat: Contextadapter (#59)

* feat: context adapter

* feat: context adapter

* feat: context adapter

37 of 37 new or added lines in 1 file covered. (100.0%)

346 of 471 relevant lines covered (73.46%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/context_adapter.go
1
// Copyright 2023 The casbin Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package xormadapter
16

17
import (
18
        "context"
19

20
        "github.com/casbin/casbin/v2/model"
21
)
22

23
type ContextAdapter struct {
24
        *Adapter
25
}
26

27
func NewContextAdapter(driverName string, dataSourceName string, dbSpecified ...bool) (*ContextAdapter, error) {
1✔
28
        a, err := NewAdapter(driverName, dataSourceName, dbSpecified...)
1✔
29
        return &ContextAdapter{
1✔
30
                a,
1✔
31
        }, err
1✔
32
}
1✔
33

34
// executeWithContext is a helper function to execute a function with context and return the result or error.
35
func executeWithContext(ctx context.Context, fn func() error) error {
1✔
36
        done := make(chan error)
1✔
37

1✔
38
        go func() {
2✔
39
                done <- fn()
1✔
40
        }()
1✔
41

42
        select {
1✔
43
        case <-ctx.Done():
×
44
                return ctx.Err()
×
45
        case err := <-done:
1✔
46
                return err
1✔
47
        }
48
}
49

50
// LoadPolicyCtx loads all policy rules from the storage with context.
51
func (ca *ContextAdapter) LoadPolicyCtx(ctx context.Context, model model.Model) error {
1✔
52
        return executeWithContext(ctx, func() error {
2✔
53
                return ca.LoadPolicy(model)
1✔
54
        })
1✔
55
}
56

57
// SavePolicyCtx saves all policy rules to the storage with context.
58
func (ca *ContextAdapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
1✔
59
        return executeWithContext(ctx, func() error {
2✔
60
                return ca.SavePolicy(model)
1✔
61
        })
1✔
62
}
63

64
// AddPolicyCtx adds a policy rule to the storage with context.
65
// This is part of the Auto-Save feature.
66
func (ca *ContextAdapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
1✔
67
        return executeWithContext(ctx, func() error {
2✔
68
                return ca.AddPolicy(sec, ptype, rule)
1✔
69
        })
1✔
70
}
71

72
// RemovePolicyCtx removes a policy rule from the storage with context.
73
// This is part of the Auto-Save feature.
74
func (ca *ContextAdapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
1✔
75
        return executeWithContext(ctx, func() error {
2✔
76
                return ca.RemovePolicy(sec, ptype, rule)
1✔
77
        })
1✔
78
}
79

80
// RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage with context.
81
// This is part of the Auto-Save feature.
82
func (ca *ContextAdapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error {
1✔
83
        return executeWithContext(ctx, func() error {
2✔
84
                return ca.RemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues...)
1✔
85
        })
1✔
86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc