]> git.lizzy.rs Git - go-anidb.git/blobdiff - anime.go
anidb: Support searching for multiple Episodes in an Anime
[go-anidb.git] / anime.go
index b57fc0bd560128b67d46b0023183d57697d949f3..95b20cbd56d9fe3b89f740dd89cb8636631c9751 100644 (file)
--- a/anime.go
+++ b/anime.go
@@ -75,9 +75,9 @@ type Anime struct {
        Awards    []string
        Resources Resources
 
-       Incomplete bool      // Set if the only the UDP API part of the  query failed
-       Updated    time.Time // When the data was last modified in the server
-       Cached     time.Time // When the data was retrieved from the server
+       Incomplete bool      // Set if the UDP API part of the query failed.
+       Updated    time.Time // When the data was last modified in the server.
+       Cached     time.Time // When the data was retrieved from the server.
 }
 
 type EpisodeCount struct {
@@ -101,17 +101,34 @@ func (adb *AniDB) AnimeByNameFold(name string) <-chan *Anime {
        return adb.AnimeByID(SearchAnimeFold(name))
 }
 
+// Returns a list of all Episodes in this Anime's Episodes list
+// that are contained by the given EpisodeContainer.
+func (a *Anime) EpisodeList(c misc.EpisodeContainer) (eps []*Episode) {
+       if a == nil || c == nil {
+               return nil
+       }
+
+       for i, e := range a.Episodes {
+               if c.ContainsEpisodes(&e.Episode) {
+                       eps = append(eps, &a.Episodes[i])
+               }
+       }
+       return
+}
+
 // Searches for the given Episode in this Anime's Episodes list
 // and returns the match.
 //
 // Returns nil if there is no match.
 func (a *Anime) Episode(ep *misc.Episode) *Episode {
-       for i, e := range a.Episodes {
-               if ep.ContainsEpisodes(&e.Episode) {
-                       return &a.Episodes[i]
-               }
+       switch list := a.EpisodeList(ep); len(list) {
+       case 0:
+               return nil
+       case 1:
+               return list[0]
+       default:
+               panic("Single episode search returned more than one result")
        }
-       return nil
 }
 
 // Convenience method that parses the string into an Episode