feat(html): add context identity setter
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
831a47f1d3
commit
19ab9d246a
2 changed files with 29 additions and 0 deletions
12
context.go
12
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").
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue