Skip to content

Commit

Permalink
Implement HasDependencies interface and Provider (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp authored Aug 22, 2024
1 parent bc653f5 commit bd9aa34
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 21 deletions.
44 changes: 24 additions & 20 deletions depresolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,6 @@ func (d dependencyNode) SpecificationName() SpecificationName {

type dependencyGraph []dependencyNode

//// merge Allows merging this dependency graph with another one and returns the result.
//func (g dependencyGraph) merge(o dependencyGraph) dependencyGraph {
// var lookup = make(map[SpecificationName]bool)
// var merge []dependencyNode
//
// for _, node := range g {
// merge = append(merge, node)
// lookup[node.SpecificationName()] = true
// }
//
// for _, node := range o {
// if _, found := lookup[node.SpecificationName()]; found {
// continue
// }
// merge = append(merge, node)
// }
//
// return newDependencyGraph(merge...)
//}

func newDependencyGraph(specifications ...dependencyNode) dependencyGraph {
return append(dependencyGraph{}, specifications...)
}
Expand Down Expand Up @@ -214,3 +194,27 @@ func (g dependencyGraph) resolve() (ResolvedDependencies, error) {

return resolved, nil
}

// HasDependencies is an interface that can be implemented by specifications
// that define their dependencies from their field values.
// This interface can be used in conjunction with the HasDependenciesProvider
// to easily resolve dependencies.
type HasDependencies interface {
Specification
Dependencies() []SpecificationName
}

type HasDependenciesProvider struct{}

func (h HasDependenciesProvider) Supports(s Specification) bool {
_, ok := s.(HasDependencies)
return ok
}

func (h HasDependenciesProvider) Provide(s Specification) []SpecificationName {
d, ok := s.(HasDependencies)
if !ok {
return nil
}
return d.Dependencies()
}
79 changes: 79 additions & 0 deletions depresolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,82 @@ func TestDependencyResolutionProcessor_Name(t *testing.T) {
p := DependencyResolutionProcessor{}
assert.NotEqual(t, "", p.Name())
}

type hasDependencySpec struct {
source Source
dependencies []SpecificationName
}

func (h *hasDependencySpec) Name() SpecificationName {
return "spec"
}

func (h *hasDependencySpec) Type() SpecificationType {
return "spec"
}

func (h *hasDependencySpec) Description() string {
return "description"
}

func (h *hasDependencySpec) Source() Source {
return h.source
}

func (h *hasDependencySpec) SetSource(s Source) {
h.source = s
}

func (h *hasDependencySpec) Dependencies() []SpecificationName {
return h.dependencies
}

func TestHasDependenciesProvider_Supports(t *testing.T) {
tests := []struct {
name string
given Specification
then bool
}{
{
name: "GIVEN specification not implementing HasDependencies THEN return false",
given: &GenericSpecification{},
then: false,
},
{
name: "GIVEN specification implementing HasDependencies THEN return false",
given: &hasDependencySpec{},
then: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := HasDependenciesProvider{}
assert.Equal(t, tt.then, h.Supports(tt.given))
})
}
}

func TestHasDependenciesProvider_Provide(t *testing.T) {
tests := []struct {
name string
given Specification
then []SpecificationName
}{
{
name: "GIVEN specification not implementing HasDependencies THEN return nil",
given: &GenericSpecification{},
then: nil,
},
{
name: "GIVEN specification implementing HasDependencies THEN return dependencies",
given: &hasDependencySpec{dependencies: []SpecificationName{"spec1"}},
then: []SpecificationName{"spec1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := HasDependenciesProvider{}
assert.Equal(t, tt.then, h.Provide(tt.given))
})
}
}
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// done, it returns nil.
//
// This function is useful for early exits in long-running or blocking
// operations when you want to respond to context cancellations in a clean
// operations when you then to respond to context cancellations in a clean
// and consistent manner.
func CheckContextDone(ctx context.Context) error {
select {
Expand Down

0 comments on commit bd9aa34

Please sign in to comment.