From 19ab9d246a67eb847e787091a876857304dff0fd Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 23:38:10 +0000 Subject: [PATCH] feat(html): add context identity setter Co-Authored-By: Virgil --- context.go | 12 ++++++++++++ context_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/context.go b/context.go index 8cb262a..abd55e6 100644 --- a/context.go +++ b/context.go @@ -102,6 +102,18 @@ func (ctx *Context) SetService(svc Translator) *Context { return ctx } +// SetIdentity updates the context identity and returns the same context. +// Example: ctx.SetIdentity("user-123"). +func (ctx *Context) SetIdentity(identity string) *Context { + if ctx == nil { + return nil + } + + ensureContextDefaults(ctx) + ctx.Identity = identity + return ctx +} + // SetLocale updates the context locale and reapplies it to the active // translator. // Example: ctx.SetLocale("en-US"). diff --git a/context_test.go b/context_test.go index b86d1a3..c282382 100644 --- a/context_test.go +++ b/context_test.go @@ -96,9 +96,26 @@ func TestContext_SetService_ReappliesCurrentLocale(t *testing.T) { } } +func TestContext_SetIdentity_UpdatesIdentity(t *testing.T) { + ctx := NewContext() + + if got := ctx.SetIdentity("user-123"); got != ctx { + t.Fatalf("SetIdentity should return the same context") + } + if ctx.Identity != "user-123" { + t.Fatalf("SetIdentity should update context identity, got %q", ctx.Identity) + } + if ctx.Data == nil { + t.Fatal("SetIdentity should preserve initialised Data map") + } +} + func TestContext_Setters_NilReceiver(t *testing.T) { var ctx *Context + if got := ctx.SetIdentity("user-123"); got != nil { + t.Fatalf("nil Context.SetIdentity should return nil, got %v", got) + } if got := ctx.SetLocale("en-GB"); got != nil { t.Fatalf("nil Context.SetLocale should return nil, got %v", got) }