From 79f23264ef5eae0c77fd1c8cf67cb9c3dd2a6fa0 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 07:33:52 +0000 Subject: [PATCH] fix(i18n): ignore nil handlers Co-Authored-By: Virgil --- handler.go | 19 +++++++++++++++++++ i18n_test.go | 37 +++++++++++++++++++++++++++++++++++++ service.go | 5 +++-- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/handler.go b/handler.go index fe2ffd4..f564d27 100644 --- a/handler.go +++ b/handler.go @@ -259,6 +259,9 @@ func contextArgText(values any) string { // RunHandlerChain executes a chain of handlers for a key. func RunHandlerChain(handlers []KeyHandler, key string, args []any, fallback func() string) string { for i, h := range handlers { + if h == nil { + continue + } if h.Match(key) { next := func() string { remaining := handlers[i+1:] @@ -273,6 +276,22 @@ func RunHandlerChain(handlers []KeyHandler, key string, args []any, fallback fun return fallback() } +func filterNilHandlers(handlers []KeyHandler) []KeyHandler { + if len(handlers) == 0 { + return nil + } + filtered := handlers[:0] + for _, h := range handlers { + if h != nil { + filtered = append(filtered, h) + } + } + if len(filtered) == 0 { + return nil + } + return filtered +} + var ( _ KeyHandler = LabelHandler{} _ KeyHandler = ProgressHandler{} diff --git a/i18n_test.go b/i18n_test.go index 73f769f..68ab2ea 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -302,6 +302,20 @@ func TestAddHandler_Good_Variadic(t *testing.T) { assert.IsType(t, ProgressHandler{}, handlers[1]) } +func TestAddHandler_Good_SkipsNil(t *testing.T) { + svc, err := New(WithHandlers()) + require.NoError(t, err) + _ = Init() + SetDefault(svc) + + var nilHandler KeyHandler + AddHandler(nilHandler, LabelHandler{}) + + handlers := svc.Handlers() + require.Len(t, handlers, 1) + assert.IsType(t, LabelHandler{}, handlers[0]) +} + func TestPrependHandler_Good(t *testing.T) { svc, err := New(WithHandlers()) // start with no handlers require.NoError(t, err) @@ -330,6 +344,20 @@ func TestPrependHandler_Good_Variadic(t *testing.T) { assert.IsType(t, ProgressHandler{}, handlers[1]) } +func TestPrependHandler_Good_SkipsNil(t *testing.T) { + svc, err := New(WithHandlers()) + require.NoError(t, err) + _ = Init() + SetDefault(svc) + + var nilHandler KeyHandler + PrependHandler(nilHandler, LabelHandler{}) + + handlers := svc.Handlers() + require.Len(t, handlers, 1) + assert.IsType(t, LabelHandler{}, handlers[0]) +} + func TestClearHandlers_Good(t *testing.T) { svc, err := New() require.NoError(t, err) @@ -347,6 +375,15 @@ func TestClearHandlers_Good(t *testing.T) { assert.Empty(t, svc.Handlers()) } +func TestNewWithHandlers_SkipsNil(t *testing.T) { + svc, err := New(WithHandlers(nil, LabelHandler{})) + require.NoError(t, err) + + handlers := svc.Handlers() + require.Len(t, handlers, 1) + assert.IsType(t, LabelHandler{}, handlers[0]) +} + // --- executeIntentTemplate --- func TestExecuteIntentTemplate_Good(t *testing.T) { diff --git a/service.go b/service.go index 09538c8..31c6978 100644 --- a/service.go +++ b/service.go @@ -56,7 +56,7 @@ func WithLocation(location string) Option { // WithHandlers sets custom handlers (replaces default handlers). func WithHandlers(handlers ...KeyHandler) Option { return func(s *Service) { - s.handlers = append([]KeyHandler(nil), handlers...) + s.handlers = filterNilHandlers(append([]KeyHandler(nil), handlers...)) } } @@ -339,12 +339,13 @@ func (s *Service) PluralCategory(n int) PluralCategory { func (s *Service) AddHandler(handlers ...KeyHandler) { s.mu.Lock() defer s.mu.Unlock() - s.handlers = append(s.handlers, handlers...) + s.handlers = append(s.handlers, filterNilHandlers(handlers)...) } func (s *Service) PrependHandler(handlers ...KeyHandler) { s.mu.Lock() defer s.mu.Unlock() + handlers = filterNilHandlers(handlers) if len(handlers) == 0 { return }