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
36 lines
820 B
Go
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
|
|
}
|