diff --git a/utils/categories/categories.go b/utils/categories/categories.go index 239e1bb8..6bc4c87f 100644 --- a/utils/categories/categories.go +++ b/utils/categories/categories.go @@ -18,55 +18,56 @@ type Categories []Category var categories Categories var Index map[string]int -func init() { - if len(categories) == 0 { - var cats map[string]string - if config.IsSukebei() { - cats = config.Conf.Torrents.SukebeiCategories - } else { - cats = config.Conf.Torrents.CleanCategories - } +func initCategories() { + var cats map[string]string + if config.IsSukebei() { + cats = config.Conf.Torrents.SukebeiCategories + } else { + cats = config.Conf.Torrents.CleanCategories + } - // Sorting categories alphabetically - var index []string - ids := make(map[string]string) - Index = make(map[string]int, len(cats)) - for id, name := range cats { - index = append(index, name) - ids[name] = id - } - sort.Strings(index) + // Sorting categories alphabetically + var index []string + ids := make(map[string]string) + Index = make(map[string]int, len(cats)) + for id, name := range cats { + index = append(index, name) + ids[name] = id + } + sort.Strings(index) - // Creating index of categories - for k, name := range index { - categories = append(categories, Category{ids[name], name}) - Index[ids[name]] = k - } + // Creating index of categories + for k, name := range index { + categories = append(categories, Category{ids[name], name}) + Index[ids[name]] = k } } // All : function to get all categories depending on the actual website from config/categories.go func All() Categories { + if len(categories) == 0 { + initCategories() + } return categories } // Get : function to get a category by the key in the index array func Get(key int) Category { - return categories[key] + return All()[key] } // Get : function to get a category by the id of the category from the database func GetByID(id string) (Category, bool) { if key, ok := Index[id]; ok { - return categories[key], true + return All()[key], true } return Category{"", ""}, false } // Exists : Check if a category exist in config -func (cats Categories) Exists(category string) bool { +func (cats Categories) Exists(id string) bool { for _, cat := range cats { - if cat.Name == category { + if cat.ID == id { return true } } @@ -75,17 +76,15 @@ func (cats Categories) Exists(category string) bool { // Exists : Check if a category exist in config func Exists(category string) bool { - return categories.Exists(category) + return All().Exists(category) } // GetSelect : Format categories in map ordered alphabetically func GetSelect(keepParent bool, keepChild bool) Categories { - catSelect := make(Categories, len(categories)) - k := 0 - for _, v := range categories { + var catSelect Categories + for _, v := range All() { if (keepParent && keepChild) || (len(v.ID) > 2 && !keepParent) || (len(v.ID) <= 2 && !keepChild) { - catSelect[k] = v - k++ + catSelect = append(catSelect, v) } } return catSelect diff --git a/utils/categories/categories_test.go b/utils/categories/categories_test.go index e777cfea..117b8e9e 100644 --- a/utils/categories/categories_test.go +++ b/utils/categories/categories_test.go @@ -20,10 +20,15 @@ var _ = func() (_ struct{}) { }() func TestGetCategories(t *testing.T) { - cats := All() + cats := make(map[string]string) + + for _, v := range All() { + cats[v.ID] = v.Name + } if len(cats) == 0 { t.Skip("Couldn't load categories to test Categories") } + if !reflect.DeepEqual(cats, config.Conf.Torrents.CleanCategories) && !reflect.DeepEqual(cats, config.Conf.Torrents.SukebeiCategories) { t.Error("Categories doesn't correspond to the configured ones") }