fix(i18n): ignore nil handlers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
65dea1d4c9
commit
79f23264ef
3 changed files with 59 additions and 2 deletions
19
handler.go
19
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{}
|
||||
|
|
|
|||
37
i18n_test.go
37
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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue