feat(html): add cloneable identity helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
c56924d95c
commit
9b7626eb91
2 changed files with 32 additions and 0 deletions
11
context.go
11
context.go
|
|
@ -155,6 +155,17 @@ func (ctx *Context) WithData(key string, value any) *Context {
|
|||
return clone
|
||||
}
|
||||
|
||||
// WithIdentity returns a cloned context with a different identity value.
|
||||
// Example: next := ctx.WithIdentity("user-123").
|
||||
func (ctx *Context) WithIdentity(identity string) *Context {
|
||||
clone := ctx.Clone()
|
||||
if clone == nil {
|
||||
return nil
|
||||
}
|
||||
clone.SetIdentity(identity)
|
||||
return clone
|
||||
}
|
||||
|
||||
// SetLocale updates the context locale and reapplies it to the active
|
||||
// translator.
|
||||
// Example: ctx.SetLocale("en-US").
|
||||
|
|
|
|||
|
|
@ -181,6 +181,27 @@ func TestContext_WithDataReturnsClonedContext(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext_WithIdentityReturnsClonedContext(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
ctx.SetIdentity("user-001")
|
||||
ctx.SetData("theme", "dark")
|
||||
|
||||
next := ctx.WithIdentity("user-123")
|
||||
|
||||
if next == ctx {
|
||||
t.Fatal("WithIdentity should return a cloned context")
|
||||
}
|
||||
if got := ctx.Identity; got != "user-001" {
|
||||
t.Fatalf("WithIdentity should not mutate the original context, got %q", got)
|
||||
}
|
||||
if got := next.Identity; got != "user-123" {
|
||||
t.Fatalf("WithIdentity should set the requested identity on the clone, got %q", got)
|
||||
}
|
||||
if got := next.Data["theme"]; got != "dark" {
|
||||
t.Fatalf("WithIdentity should preserve existing data on the clone, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_Setters_NilReceiver(t *testing.T) {
|
||||
var ctx *Context
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue