From 82175b5de95d7d90c9b1847b2d617a6d0efe366a Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 11 Oct 2021 20:25:02 +0800 Subject: [PATCH] rename gctx.Value/WithValue to CtxId/WithCtxId --- os/gctx/gctx.go | 16 ++++++++-------- os/gctx/gctx_test.go | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/os/gctx/gctx.go b/os/gctx/gctx.go index 9eb91f5e6..7e56ba78e 100644 --- a/os/gctx/gctx.go +++ b/os/gctx/gctx.go @@ -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 } diff --git a/os/gctx/gctx_test.go b/os/gctx/gctx_test.go index d190d5a7a..a5cf9fc3c 100644 --- a/os/gctx/gctx_test.go +++ b/os/gctx/gctx_test.go @@ -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) }) }