-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
60 lines (47 loc) · 1.14 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
52
53
54
55
56
57
58
59
60
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package sox
import "context"
type userdataGetter[T any] interface {
getUserdata() T
}
type userdataSetter[T any] interface {
setUserdata(data T)
}
type innerCtxGetter interface {
innerCtx() context.Context
}
type userdataCtx[T any] struct {
context.Context
userdata T
}
func (ctx *userdataCtx[T]) getUserdata() T {
return ctx.userdata
}
func (ctx *userdataCtx[T]) setUserdata(data T) {
ctx.userdata = data
}
func (ctx *userdataCtx[T]) innerCtx() context.Context {
return ctx.Context
}
func ContextWithUserdata[T any](parent context.Context, userdata T) context.Context {
if uc, ok := parent.(userdataSetter[T]); ok {
uc.setUserdata(userdata)
return parent
}
return &userdataCtx[T]{Context: parent, userdata: userdata}
}
func ContextUserdata[T any](ctx context.Context) (ret T) {
for ctx != nil {
if uc, ok := ctx.(userdataGetter[T]); ok {
return uc.getUserdata()
}
if uc, ok := ctx.(innerCtxGetter); ok {
ctx = uc.innerCtx()
} else {
break
}
}
return
}