Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: check duplicate recommenders #937

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/gorse-in-one/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var oneCommand = &cobra.Command{
conf.Database.CacheStore = "sqlite://cache.db"
conf.Recommend.DataSource.PositiveFeedbackTypes = []string{"star", "like"}
conf.Recommend.DataSource.ReadFeedbackTypes = []string{"read"}
if err := conf.Validate(true); err != nil {
if err := conf.Validate(); err != nil {
log.Logger().Fatal("invalid config", zap.Error(err))
}

Expand Down
27 changes: 25 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/expr-lang/expr/parser"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
Expand Down Expand Up @@ -591,7 +592,7 @@ func LoadConfig(path string, oneModel bool) (*Config, error) {
}

// validate config file
if err := conf.Validate(oneModel); err != nil {
if err := conf.Validate(); err != nil {
return nil, errors.Trace(err)
}

Expand All @@ -605,7 +606,25 @@ func LoadConfig(path string, oneModel bool) (*Config, error) {
return &conf, nil
}

func (config *Config) Validate(oneModel bool) error {
func (config *Config) Validate() error {
// Check non-personalized recommenders
nonPersonalizedNames := mapset.NewSet[string]()
for _, nonPersonalized := range config.Recommend.NonPersonalized {
if nonPersonalizedNames.Contains(nonPersonalized.Name) {
return errors.Errorf("non-personalized recommender %v is duplicated", nonPersonalized.Name)
}
nonPersonalizedNames.Add(nonPersonalized.Name)
}

// Check item-to-item recommenders
itemToItemNames := mapset.NewSet[string]()
for _, itemToItem := range config.Recommend.ItemToItem {
if itemToItemNames.Contains(itemToItem.Name) {
return errors.Errorf("item-to-item recommender %v is duplicated", itemToItem.Name)
}
itemToItemNames.Add(itemToItem.Name)
}

validate := validator.New()
if err := validate.RegisterValidation("data_store", func(fl validator.FieldLevel) bool {
prefixes := []string{
Expand Down Expand Up @@ -649,6 +668,10 @@ func (config *Config) Validate(oneModel bool) error {
return errors.Trace(err)
}
if err := validate.RegisterValidation("item_expr", func(fl validator.FieldLevel) bool {
if fl.Field().String() == "" {
// Empty expression is legal.
return true
}
_, err := parser.Parse(fl.Field().String())
return err == nil
}); err != nil {
Expand Down
38 changes: 38 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/sclevine/yj/convert"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -443,3 +444,40 @@ func TestItemToItemConfig_Hash(t *testing.T) {
b = ItemToItemConfig{Column: "b"}
assert.NotEqual(t, a.Hash(), b.Hash())
}

type ValidateTestSuite struct {
suite.Suite
*Config
}

func (s *ValidateTestSuite) SetupTest() {
s.Config = GetDefaultConfig()
s.Database.CacheStore = "redis://localhost:6379/0"
s.Database.DataStore = "mysql://gorse:gorse_pass@tcp(localhost:3306)/gorse"
}

func (s *ValidateTestSuite) TestDuplicateNonPersonalized() {
s.Recommend.NonPersonalized = []NonPersonalizedConfig{{
Name: "most_starred_weekly",
Score: "count(feedback, .FeedbackType == 'star')",
}, {
Name: "most_starred_weekly",
Score: "count(feedback, .FeedbackType == 'star')",
}}
s.Error(s.Validate())
}

func (s *ValidateTestSuite) TestDuplicateItemToItem() {
s.Recommend.ItemToItem = []ItemToItemConfig{{
Name: "item_to_item",
Type: "users",
}, {
Name: "item_to_item",
Type: "users",
}}
s.Error(s.Validate())
}

func TestValidate(t *testing.T) {
suite.Run(t, new(ValidateTestSuite))
}
Loading