forked from go-saas/saas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
53 lines (45 loc) · 1.33 KB
/
provider.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
package saas
import (
"context"
"github.com/go-saas/saas/data"
)
type (
//ClientProvider resolve by dsn string (connection string)
ClientProvider[TClient interface{}] interface {
Get(ctx context.Context, dsn string) (TClient, error)
}
// ClientProviderFunc see ClientProvider
ClientProviderFunc[TClient interface{}] func(ctx context.Context, dsn string) (TClient, error)
//DbProvider resolve TClient from user friendly key
DbProvider[TClient interface{}] interface {
// Get instance by key
Get(ctx context.Context, key string) TClient
}
//DefaultDbProvider resolve dsn from user friendly key by data.ConnStrResolver, then resolve TClient from dsn by ClientProvider
DefaultDbProvider[TClient interface{}] struct {
cs data.ConnStrResolver
cp ClientProvider[TClient]
}
)
func (c ClientProviderFunc[TClient]) Get(ctx context.Context, dsn string) (TClient, error) {
return c(ctx, dsn)
}
func NewDbProvider[TClient interface{}](cs data.ConnStrResolver, cp ClientProvider[TClient]) (d *DefaultDbProvider[TClient]) {
d = &DefaultDbProvider[TClient]{
cs: cs,
cp: cp,
}
return
}
func (d *DefaultDbProvider[TClient]) Get(ctx context.Context, key string) TClient {
//resolve connection string
s, err := d.cs.Resolve(ctx, key)
if err != nil {
panic(err)
}
c, err := d.cp.Get(ctx, s)
if err != nil {
panic(err)
}
return c
}