Skip to content

Commit

Permalink
refactor: result set return type
Browse files Browse the repository at this point in the history
  • Loading branch information
agukrapo committed Jul 24, 2024
1 parent ceb409c commit 71d765e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/agukrapo/playlist-creator/deezer"
"github.com/agukrapo/playlist-creator/internal/env"
"github.com/agukrapo/playlist-creator/internal/random"
"github.com/agukrapo/playlist-creator/internal/set"
"github.com/agukrapo/playlist-creator/internal/results"
"github.com/agukrapo/playlist-creator/playlists"
"github.com/agukrapo/playlist-creator/spotify"
)
Expand Down Expand Up @@ -52,16 +52,16 @@ func run() error {
name += " " + random.Name(20)
}

data := set.New(len(lines))
data := results.New(len(lines))

if err := manager.Gather(ctx, lines, func(i int, query string, matches []playlists.Track) {
if len(matches) == 0 {
warn(fmt.Sprintf("%q: %s", query, playlists.ErrTrackNotFound))
return
}
track := matches[0]
if err := data.Add(i, track.ID, track.Name); err != nil {
warn(err)
if !data.Add(i, track.ID, track.Name) {
warn(fmt.Sprintf("Duplicated result for %q: id %s, name %q", query, track.ID, track.Name))
}
}); err != nil {
return err
Expand Down
20 changes: 10 additions & 10 deletions cmd/gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/agukrapo/playlist-creator/deezer"
"github.com/agukrapo/playlist-creator/internal/env"
"github.com/agukrapo/playlist-creator/internal/random"
"github.com/agukrapo/playlist-creator/internal/set"
"github.com/agukrapo/playlist-creator/internal/results"
"github.com/agukrapo/playlist-creator/playlists"
)

Expand Down Expand Up @@ -112,7 +112,7 @@ func (a *application) renderResults(target playlists.Target, name string, songs
})
}

data := set.New(len(songs))
data := results.New(len(songs))
manager := playlists.NewManager(target, 100)

cnf := dialog.NewConfirm("Create playlist?", fmt.Sprintf("name %q", name), func(b bool) {
Expand Down Expand Up @@ -153,8 +153,8 @@ func (a *application) renderResults(target playlists.Target, name string, songs
if len(matches) == 1 {
track := matches[0]
var w fyne.CanvasObject = widget.NewLabel(track.Name)
if err := data.Add(i, track.ID, track.Name); err != nil {
w = errorLabel(query, err.Error())
if !data.Add(i, track.ID, track.Name) {
w = errorLabel(query, fmt.Sprintf("Duplicated result: id %s, name %q", track.ID, track.Name))
}
items[i].Widget = w
return
Expand All @@ -168,8 +168,8 @@ func (a *application) renderResults(target playlists.Target, name string, songs
s := widget.NewSelect(opts, nil)
s.OnChanged = func(_ string) {
track := matches[s.SelectedIndex()]
if err := data.Add(i, track.ID, track.Name); err != nil {
notify(a.window, err)
if !data.Add(i, track.ID, track.Name) {
notify(a.window, fmt.Sprintf("Duplicated track: id %s, Name %q", track.ID, track.Name))
}
}
s.SetSelectedIndex(0)
Expand All @@ -186,7 +186,7 @@ func (a *application) renderResults(target playlists.Target, name string, songs
}

func errorLabel(reason, msg string) fyne.CanvasObject {
fyne.LogError(reason, errors.New(msg))
_, _ = fmt.Fprintln(os.Stderr, reason, msg)
return container.NewHBox(widget.NewIcon(theme.ErrorIcon()),
widget.NewLabelWithStyle(msg, fyne.TextAlignLeading, fyne.TextStyle{Bold: true, Italic: true}))
}
Expand Down Expand Up @@ -257,9 +257,9 @@ func lines(in string) []string {
return out
}

func notify(parent fyne.Window, err error) {
_, _ = fmt.Fprintln(os.Stderr, "Error:", err)
dialog.ShowError(err, parent)
func notify(parent fyne.Window, msg any) {
_, _ = fmt.Fprintln(os.Stderr, "Error:", msg)
dialog.ShowError(fmt.Errorf("%v", msg), parent)
}

func page(title string, content fyne.CanvasObject) fyne.CanvasObject {
Expand Down
11 changes: 5 additions & 6 deletions internal/set/set.go → internal/results/set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package set
package results

import (
"fmt"
"sync"
)

Expand All @@ -18,18 +17,18 @@ func New(size int) *Set {
}
}

func (c *Set) Add(i int, id string, value string) error {
func (c *Set) Add(i int, id string, value string) bool {
c.mu.Lock()
defer c.mu.Unlock()

if old, ok := c.table[id]; ok {
return fmt.Errorf("id %s duplicated: new %q, old %q", id, value, old)
if _, ok := c.table[id]; ok {
return false
}

c.list[i] = id
c.table[id] = value

return nil
return true
}

func (c *Set) Slice() []string {
Expand Down

0 comments on commit 71d765e

Please sign in to comment.