From 32471fb73bf9693023f1589dd536b082f61f725c Mon Sep 17 00:00:00 2001 From: Per Goncalves da Silva Date: Tue, 18 Jul 2023 15:39:19 +0200 Subject: [PATCH] address merge conflicts Signed-off-by: Per Goncalves da Silva --- cmd/dimacs/cmd.go | 5 +- cmd/sudoku/cmd.go | 5 +- pkg/deppy/input/cache_entity_source.go | 61 -------- pkg/deppy/input/entity_source_test.go | 205 ------------------------- 4 files changed, 2 insertions(+), 274 deletions(-) delete mode 100644 pkg/deppy/input/cache_entity_source.go delete mode 100644 pkg/deppy/input/entity_source_test.go diff --git a/cmd/dimacs/cmd.go b/cmd/dimacs/cmd.go index 43bffc9..b63d1d0 100644 --- a/cmd/dimacs/cmd.go +++ b/cmd/dimacs/cmd.go @@ -53,10 +53,7 @@ func solve(path string) error { } // build solver - so, err := solver.NewDeppySolver(NewDimacsVariableSource(dimacs)) - if err != nil { - return err - } + so := solver.NewDeppySolver(NewDimacsVariableSource(dimacs)) // get solution solution, err := so.Solve(context.Background(), solver.AddAllVariablesToSolution()) diff --git a/cmd/sudoku/cmd.go b/cmd/sudoku/cmd.go index 891746a..99e9dcf 100644 --- a/cmd/sudoku/cmd.go +++ b/cmd/sudoku/cmd.go @@ -24,10 +24,7 @@ func NewSudokuCommand() *cobra.Command { func solve() error { // build solver sudoku := &Sudoku{} - so, err := solver.NewDeppySolver(sudoku) - if err != nil { - return err - } + so := solver.NewDeppySolver(sudoku) // get solution solution, err := so.Solve(context.Background()) diff --git a/pkg/deppy/input/cache_entity_source.go b/pkg/deppy/input/cache_entity_source.go deleted file mode 100644 index de8070a..0000000 --- a/pkg/deppy/input/cache_entity_source.go +++ /dev/null @@ -1,61 +0,0 @@ -package input - -import ( - "context" - "fmt" - - "github.com/operator-framework/deppy/pkg/deppy" -) - -var _ EntitySource = &CacheEntitySource{} - -type CacheEntitySource struct { - // TODO: separate out a cache - entities map[deppy.Identifier]Entity -} - -func NewCacheQuerier(entities map[deppy.Identifier]Entity) *CacheEntitySource { - return &CacheEntitySource{ - entities: entities, - } -} - -func (c CacheEntitySource) Get(_ context.Context, id deppy.Identifier) (*Entity, error) { - if entity, ok := c.entities[id]; ok { - return &entity, nil - } - return nil, fmt.Errorf("entity with id: %s not found in the entity source", id.String()) -} - -func (c CacheEntitySource) Filter(_ context.Context, filter Predicate) (EntityList, error) { - resultSet := EntityList{} - for i := range c.entities { - entity := c.entities[i] - if filter(&entity) { - resultSet = append(resultSet, entity) - } - } - return resultSet, nil -} - -func (c CacheEntitySource) GroupBy(_ context.Context, fn GroupByFunction) (EntityListMap, error) { - resultSet := EntityListMap{} - for i := range c.entities { - entity := c.entities[i] - keys := fn(&entity) - for _, key := range keys { - resultSet[key] = append(resultSet[key], entity) - } - } - return resultSet, nil -} - -func (c CacheEntitySource) Iterate(_ context.Context, fn IteratorFunction) error { - for i := range c.entities { - entity := c.entities[i] - if err := fn(&entity); err != nil { - return err - } - } - return nil -} diff --git a/pkg/deppy/input/entity_source_test.go b/pkg/deppy/input/entity_source_test.go deleted file mode 100644 index 1c0f207..0000000 --- a/pkg/deppy/input/entity_source_test.go +++ /dev/null @@ -1,205 +0,0 @@ -package input_test - -import ( - "context" - "fmt" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/operator-framework/deppy/pkg/deppy/input" - - "github.com/operator-framework/deppy/pkg/deppy" - - . "github.com/onsi/gomega/gstruct" -) - -func TestInput(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Input Suite") -} - -// Test functions for filter -func byIndex(index string) input.Predicate { - return func(entity *input.Entity) bool { - i, ok := entity.Properties["index"] - if !ok { - return false - } - if i == index { - return true - } - return false - } -} -func bySource(source string) input.Predicate { - return func(entity *input.Entity) bool { - i, ok := entity.Properties["source"] - if !ok { - return false - } - if i == source { - return true - } - return false - } -} - -// Test function for iterate -var entityCheck map[deppy.Identifier]bool - -func check(entity *input.Entity) error { - checked, ok := entityCheck[entity.Identifier()] - Expect(ok).Should(BeTrue()) - Expect(checked).Should(BeFalse()) - entityCheck[entity.Identifier()] = true - return nil -} - -// Test function for GroupBy -func bySourceAndIndex(entity *input.Entity) []string { - switch entity.Identifier() { - case "1-1": - return []string{"source 1", "index 1"} - case "1-2": - return []string{"source 1", "index 2"} - case "2-1": - return []string{"source 2", "index 1"} - case "2-2": - return []string{"source 2", "index 2"} - } - return nil -} - -var _ = Describe("EntitySource", func() { - When("a group is created with multiple entity sources", func() { - var ( - entitySource input.EntitySource - ) - - BeforeEach(func() { - entities := map[deppy.Identifier]input.Entity{ - deppy.Identifier("1-1"): *input.NewEntity("1-1", map[string]string{"source": "1", "index": "1"}), - deppy.Identifier("1-2"): *input.NewEntity("1-2", map[string]string{"source": "1", "index": "2"}), - deppy.Identifier("2-1"): *input.NewEntity("2-1", map[string]string{"source": "2", "index": "1"}), - deppy.Identifier("2-2"): *input.NewEntity("2-2", map[string]string{"source": "2", "index": "2"}), - } - entitySource = input.NewCacheQuerier(entities) - }) - - Describe("Get", func() { - It("should return requested entity", func() { - e, err := entitySource.Get(context.Background(), "2-2") - Expect(err).ToNot(HaveOccurred()) - Expect(e).NotTo(BeNil()) - Expect(e.Identifier()).To(Equal(deppy.Identifier("2-2"))) - }) - - It("should return an error when the requested entity is not found", func() { - e, err := entitySource.Get(context.Background(), "random") - Expect(err).To(HaveOccurred()) - Expect(e).To(BeNil()) - Expect(err.Error()).To(BeEquivalentTo(fmt.Sprintf("entity with id: %s not found in the entity source", "random"))) - }) - }) - - Describe("Filter", func() { - It("should return entities that meet filter predicates", func() { - id := func(element interface{}) string { - return fmt.Sprintf("%v", element) - } - el, err := entitySource.Filter(context.Background(), input.Or(byIndex("2"), bySource("1"))) - Expect(err).ToNot(HaveOccurred()) - Expect(el).To(MatchAllElements(id, Elements{ - "{1-2 map[index:2 source:1]}": Not(BeNil()), - "{2-2 map[index:2 source:2]}": Not(BeNil()), - "{1-1 map[index:1 source:1]}": Not(BeNil()), - })) - ids := el.CollectIds() - Expect(ids).NotTo(BeNil()) - Expect(ids).To(MatchAllElements(id, Elements{ - "1-2": Not(BeNil()), - "2-2": Not(BeNil()), - "1-1": Not(BeNil()), - })) - - el, err = entitySource.Filter(context.Background(), input.And(byIndex("2"), bySource("1"))) - Expect(err).ToNot(HaveOccurred()) - Expect(el).To(MatchAllElements(id, Elements{ - "{1-2 map[index:2 source:1]}": Not(BeNil()), - })) - ids = el.CollectIds() - Expect(ids).NotTo(BeNil()) - Expect(ids).To(MatchAllElements(id, Elements{ - "1-2": Not(BeNil()), - })) - - el, err = entitySource.Filter(context.Background(), input.And(byIndex("2"), input.Not(bySource("1")))) - Expect(err).ToNot(HaveOccurred()) - Expect(el).To(MatchAllElements(id, Elements{ - "{2-2 map[index:2 source:2]}": Not(BeNil()), - })) - ids = el.CollectIds() - Expect(ids).NotTo(BeNil()) - Expect(ids).To(MatchAllElements(id, Elements{ - "2-2": Not(BeNil()), - })) - - }) - }) - - Describe("Iterate", func() { - It("should go through all entities", func() { - entityCheck = map[deppy.Identifier]bool{"1-1": false, "1-2": false, "2-1": false, "2-2": false} - err := entitySource.Iterate(context.Background(), check) - Expect(err).ToNot(HaveOccurred()) - for _, value := range entityCheck { - Expect(value).To(BeTrue()) - } - }) - }) - - Describe("GroupBy", func() { - It("should group entities by the keys provided by the groupBy function", func() { - id := func(element interface{}) string { - return fmt.Sprintf("%v", element) - } - grouped, err := entitySource.GroupBy(context.Background(), bySourceAndIndex) - Expect(err).ToNot(HaveOccurred()) - Expect(grouped).To(MatchAllKeys(Keys{ - "index 1": Not(BeNil()), - "index 2": Not(BeNil()), - "source 1": Not(BeNil()), - "source 2": Not(BeNil()), - })) - for key, value := range grouped { - switch key { - case "index 1": - Expect(value).To(MatchAllElements(id, Elements{ - "{1-1 map[index:1 source:1]}": Not(BeNil()), - "{2-1 map[index:1 source:2]}": Not(BeNil()), - })) - case "index 2": - Expect(value).To(MatchAllElements(id, Elements{ - "{1-2 map[index:2 source:1]}": Not(BeNil()), - "{2-2 map[index:2 source:2]}": Not(BeNil()), - })) - case "source 1": - Expect(value).To(MatchAllElements(id, Elements{ - "{1-1 map[index:1 source:1]}": Not(BeNil()), - "{1-2 map[index:2 source:1]}": Not(BeNil()), - })) - case "source 2": - Expect(value).To(MatchAllElements(id, Elements{ - "{2-1 map[index:1 source:2]}": Not(BeNil()), - "{2-2 map[index:2 source:2]}": Not(BeNil()), - })) - default: - Fail(fmt.Sprintf("unknown key %s", key)) - } - } - }) - }) - }) -})