-
Notifications
You must be signed in to change notification settings - Fork 44
/
tenant_resolver.go
39 lines (34 loc) · 980 Bytes
/
tenant_resolver.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
package saas
import "context"
type TenantResolver interface {
Resolve(ctx context.Context) (TenantResolveResult, context.Context, error)
}
type DefaultTenantResolver struct {
//options
o *TenantResolveOption
}
func NewDefaultTenantResolver(opt ...ResolveOption) TenantResolver {
o := NewTenantResolveOption(&ContextContrib{})
for _, resolveOption := range opt {
resolveOption(o)
}
return &DefaultTenantResolver{
o: o,
}
}
func (d *DefaultTenantResolver) Resolve(ctx context.Context) (TenantResolveResult, context.Context, error) {
res := TenantResolveResult{}
trCtx := NewTenantResolveContext(ctx)
for _, resolver := range d.o.Resolvers {
if err := resolver.Resolve(trCtx); err != nil {
return res, trCtx.Context(), err
}
res.AppliedResolvers = append(res.AppliedResolvers, resolver.Name())
if trCtx.HasResolved() {
break
}
}
res.TenantIdOrName = trCtx.TenantIdOrName
ctx = NewTenantResolveRes(trCtx.Context(), &res)
return res, ctx, nil
}