Files
fancy/fancy-backend/internal/provider/dictionary/fallback.go
T
Ivan Rezchikov 628dd6384b
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
version 1
2026-09-18 00:45:51 +03:00

36 lines
820 B
Go

package dictionary
import (
"context"
"errors"
"fmt"
"github.com/dusiburg/fancy-api/internal/model"
)
type FallbackProvider struct {
primary Provider
fallback Provider
}
func NewFallbackProvider(primary, fallback Provider) *FallbackProvider {
return &FallbackProvider{primary: primary, fallback: fallback}
}
func (p *FallbackProvider) Lookup(ctx context.Context, word string, lang model.Language) (*model.DictionaryResult, error) {
result, err := p.primary.Lookup(ctx, word, lang)
if err == nil && hasDefinitions(result) {
return result, nil
}
if err != nil && !errors.Is(err, ErrWordNotFound) {
return nil, err
}
fallbackResult, err := p.fallback.Lookup(ctx, word, lang)
if err != nil {
return nil, fmt.Errorf("fallback lookup %q [%s]: %w", word, lang, err)
}
return fallbackResult, nil
}