-
Notifications
You must be signed in to change notification settings - Fork 44
/
context.go
51 lines (42 loc) · 1.28 KB
/
context.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
package saas
import (
"context"
)
type (
currentTenantCtx struct{}
tenantResolveRes struct{}
tenantConfigKey string
)
func NewCurrentTenant(ctx context.Context, id, name string) context.Context {
return NewCurrentTenantInfo(ctx, NewBasicTenantInfo(id, name))
}
func NewCurrentTenantInfo(ctx context.Context, info TenantInfo) context.Context {
return context.WithValue(ctx, currentTenantCtx{}, info)
}
func FromCurrentTenant(ctx context.Context) (TenantInfo, bool) {
value, ok := ctx.Value(currentTenantCtx{}).(TenantInfo)
if ok {
return value, true
}
return NewBasicTenantInfo("", ""), false
}
func NewTenantResolveRes(ctx context.Context, t *TenantResolveResult) context.Context {
return context.WithValue(ctx, tenantResolveRes{}, t)
}
func FromTenantResolveRes(ctx context.Context) *TenantResolveResult {
v, ok := ctx.Value(tenantResolveRes{}).(*TenantResolveResult)
if ok {
return v
}
return nil
}
func NewTenantConfigContext(ctx context.Context, tenantId string, cfg *TenantConfig) context.Context {
return context.WithValue(ctx, tenantConfigKey(tenantId), cfg)
}
func FromTenantConfigContext(ctx context.Context, tenantId string) (*TenantConfig, bool) {
v, ok := ctx.Value(tenantConfigKey(tenantId)).(*TenantConfig)
if ok {
return v, ok && v != nil
}
return nil, false
}