-
Notifications
You must be signed in to change notification settings - Fork 122
/
db_opts.go
64 lines (52 loc) · 1.62 KB
/
db_opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"context"
"github.com/doug-martin/goqu/v9"
"github.com/cerbos/cerbos/internal/policy"
"github.com/cerbos/cerbos/internal/util"
)
// DBOpt defines database driver options.
type DBOpt func(*dbOpt)
type (
upsertPolicyFunc func(ctx context.Context, tx *goqu.TxDatabase, p policy.Wrapper) error
upsertSchemaFunc func(ctx context.Context, tx *goqu.TxDatabase, schema Schema) error
)
type dbOpt struct {
upsertPolicy upsertPolicyFunc
upsertSchema upsertSchemaFunc
regexpCache *util.RegexpCache
sourceAttributes []policy.SourceAttribute
}
func newDbOpt() *dbOpt {
return &dbOpt{
regexpCache: util.NewRegexpCache(),
}
}
// WithUpsertSchema sets custom upsert schema function.
func WithUpsertSchema(f upsertSchemaFunc) DBOpt {
return func(opt *dbOpt) {
opt.upsertSchema = f
}
}
// WithUpsertPolicy sets custom upsert policy function.
func WithUpsertPolicy(f upsertPolicyFunc) DBOpt {
return func(opt *dbOpt) {
opt.upsertPolicy = f
}
}
// WithRegexpCacheOverride overrides the default regexp cache for DB queries requiring compiled expressions.
// This is only required for DB drivers that require access to the cache (e.g. The SQLite driver retrieves the compiled
// expressions for the application-defined function).
func WithRegexpCacheOverride(c *util.RegexpCache) DBOpt {
return func(opt *dbOpt) {
opt.regexpCache = c
}
}
// WithSourceAttributes sets the policy source attributes.
func WithSourceAttributes(attr ...policy.SourceAttribute) DBOpt {
return func(opt *dbOpt) {
opt.sourceAttributes = attr
}
}