mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
rename gctx.Value/WithValue to CtxId/WithCtxId
This commit is contained in:
@ -36,20 +36,20 @@ func WithCtx(ctx context.Context) context.Context {
|
||||
// WithPrefix creates and returns a context containing context id upon given parent context `ctx`.
|
||||
// The generated context id has custom prefix string specified by parameter `prefix`.
|
||||
func WithPrefix(ctx context.Context, prefix string) context.Context {
|
||||
return WithValue(ctx, prefix+getUniqueID())
|
||||
return WithCtxId(ctx, prefix+getUniqueID())
|
||||
}
|
||||
|
||||
// WithValue creates and returns a context containing context id upon given parent context `ctx`.
|
||||
// The generated context id value is specified by parameter `value`.
|
||||
func WithValue(ctx context.Context, value string) context.Context {
|
||||
if value == "" {
|
||||
// WithCtxId creates and returns a context containing context id upon given parent context `ctx`.
|
||||
// The generated context id value is specified by parameter `id`.
|
||||
func WithCtxId(ctx context.Context, id string) context.Context {
|
||||
if id == "" {
|
||||
return New()
|
||||
}
|
||||
return context.WithValue(ctx, CtxKey, value)
|
||||
return context.WithValue(ctx, CtxKey, id)
|
||||
}
|
||||
|
||||
// Value retrieves and returns the context id from context.
|
||||
func Value(ctx context.Context) string {
|
||||
// CtxId retrieves and returns the context id from context.
|
||||
func CtxId(ctx context.Context) string {
|
||||
s, _ := ctx.Value(CtxKey).(string)
|
||||
return s
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ func Test_New(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx := gctx.New()
|
||||
t.AssertNE(ctx, nil)
|
||||
t.AssertNE(gctx.Value(ctx), "")
|
||||
t.AssertNE(gctx.CtxId(ctx), "")
|
||||
})
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ func Test_WithCtx(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx := context.WithValue(context.TODO(), "TEST", 1)
|
||||
ctx = gctx.WithCtx(ctx)
|
||||
t.AssertNE(gctx.Value(ctx), "")
|
||||
t.AssertNE(gctx.CtxId(ctx), "")
|
||||
t.Assert(ctx.Value("TEST"), 1)
|
||||
})
|
||||
}
|
||||
@ -36,7 +36,7 @@ func Test_WithPrefix(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx := context.WithValue(context.TODO(), "TEST", 1)
|
||||
ctx = gctx.WithPrefix(ctx, "H-")
|
||||
t.Assert(gstr.Contains(gctx.Value(ctx), "H-"), true)
|
||||
t.Assert(gstr.Contains(gctx.CtxId(ctx), "H-"), true)
|
||||
t.Assert(ctx.Value("TEST"), 1)
|
||||
})
|
||||
}
|
||||
@ -44,8 +44,8 @@ func Test_WithPrefix(t *testing.T) {
|
||||
func Test_WithValue(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx := context.WithValue(context.TODO(), "TEST", 1)
|
||||
ctx = gctx.WithValue(ctx, "123")
|
||||
t.Assert(gctx.Value(ctx), "123")
|
||||
ctx = gctx.WithCtxId(ctx, "123")
|
||||
t.Assert(gctx.CtxId(ctx), "123")
|
||||
t.Assert(ctx.Value("TEST"), 1)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user