Skip to content

Commit

Permalink
Translator initializer. Added options
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed May 14, 2024
1 parent c60a685 commit 5be3ffe
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/translator/linguleo.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Lingualeo struct {
}

// New initialize lingualeo client
func New(version string) (Lingualeo, error) {
func New(version string, options ...Option) (Lingualeo, error) {
client, err := prepareArgs(version)
if err != nil {
return client, err
Expand All @@ -82,8 +82,7 @@ func New(version string) (Lingualeo, error) {
return client, err
}
client.mergeConfigs(configArgs)
err = client.checkArgs()
if err != nil {
if err = client.checkArgs(); err != nil {
return client, err
}
if client.Debug {
Expand All @@ -96,13 +95,26 @@ func New(version string) (Lingualeo, error) {
}
logger.Prepare(level)
client.checkMediaPlayer()
client.Translator, err = api.New(client.Email, client.Password, client.Debug)
if err != nil {
return client, err

for _, option := range options {
if err = option(&client); err != nil {
return client, err
}
}
if client.Translator == nil {
if client.Translator, err = api.New(client.Email, client.Password, client.Debug); err != nil {
return client, err
}
}
if client.Pronouncer == nil {
client.Pronouncer = player.New(client.Player)
}
if client.Downloader == nil {
client.Downloader = files.New()
}
if client.Visualizer == nil {
client.Visualizer = browser.New()
}
client.Pronouncer = player.New(client.Player)
client.Downloader = files.New()
client.Visualizer = browser.New()
return client, nil
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/translator/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package translator

type Option func(*Lingualeo) error

func WithTranslator(t Translator) Option {
return func(l *Lingualeo) error {
l.Translator = t
return nil
}
}

func WithVisualizer(v Visualizer) Option {
return func(l *Lingualeo) error {
l.Visualizer = v
return nil
}
}

func WithDownloader(d Downloader) Option {
return func(l *Lingualeo) error {
l.Downloader = d
return nil
}
}

func WithPronouncer(p Pronouncer) Option {
return func(l *Lingualeo) error {
l.Pronouncer = p
return nil
}
}

0 comments on commit 5be3ffe

Please sign in to comment.