Skip to content

Commit

Permalink
Refactoring: make PackageQuery work on PackageLike objects (not neces…
Browse files Browse the repository at this point in the history
…sarily Packages). #71
  • Loading branch information
smira committed Mar 18, 2015
1 parent 3632678 commit 4d622e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
15 changes: 15 additions & 0 deletions deb/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ func (p *Package) MatchesDependency(dep Dependency) bool {
panic("unknown relation")
}

// GetName returns package name
func (p *Package) GetName() string {
return p.Name
}

// GetVersion returns package version
func (p *Package) GetVersion() string {
return p.Version
}

// GetArchitecture returns package arch
func (p *Package) GetArchitecture() string {
return p.Architecture
}

// GetDependencies compiles list of dependenices by flags from options
func (p *Package) GetDependencies(options int) (dependencies []string) {
deps := p.Deps()
Expand Down
28 changes: 19 additions & 9 deletions deb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"strings"
)

// PackageLike is something like Package :) To be refined later
type PackageLike interface {
GetField(string) string
MatchesDependency(Dependency) bool
MatchesArchitecture(string) bool
GetName() string
GetVersion() string
GetArchitecture() string
}

// PackageCatalog is abstraction on top of PackageCollection and PackageList
type PackageCatalog interface {
Scan(q PackageQuery) (result *PackageList)
Expand All @@ -18,7 +28,7 @@ type PackageCatalog interface {
// PackageQuery is interface of predicate on Package
type PackageQuery interface {
// Matches calculates match of condition against package
Matches(pkg *Package) bool
Matches(pkg PackageLike) bool
// Fast returns if search strategy is possible for this query
Fast(list PackageCatalog) bool
// Query performs search on package list
Expand Down Expand Up @@ -63,7 +73,7 @@ type DependencyQuery struct {
}

// Matches if any of L, R matches
func (q *OrQuery) Matches(pkg *Package) bool {
func (q *OrQuery) Matches(pkg PackageLike) bool {
return q.L.Matches(pkg) || q.R.Matches(pkg)
}

Expand All @@ -89,7 +99,7 @@ func (q *OrQuery) String() string {
}

// Matches if both of L, R matches
func (q *AndQuery) Matches(pkg *Package) bool {
func (q *AndQuery) Matches(pkg PackageLike) bool {
return q.L.Matches(pkg) && q.R.Matches(pkg)
}

Expand Down Expand Up @@ -120,7 +130,7 @@ func (q *AndQuery) String() string {
}

// Matches if not matches
func (q *NotQuery) Matches(pkg *Package) bool {
func (q *NotQuery) Matches(pkg PackageLike) bool {
return !q.Q.Matches(pkg)
}

Expand All @@ -141,9 +151,9 @@ func (q *NotQuery) String() string {
}

// Matches on generic field
func (q *FieldQuery) Matches(pkg *Package) bool {
func (q *FieldQuery) Matches(pkg PackageLike) bool {
if q.Field == "$Version" {
return pkg.MatchesDependency(Dependency{Pkg: pkg.Name, Relation: q.Relation, Version: q.Value, Regexp: q.Regexp})
return pkg.MatchesDependency(Dependency{Pkg: pkg.GetName(), Relation: q.Relation, Version: q.Value, Regexp: q.Regexp})
}
if q.Field == "$Architecture" && q.Relation == VersionEqual {
return pkg.MatchesArchitecture(q.Value)
Expand Down Expand Up @@ -218,7 +228,7 @@ func (q *FieldQuery) String() string {
}

// Matches on dependency condition
func (q *DependencyQuery) Matches(pkg *Package) bool {
func (q *DependencyQuery) Matches(pkg PackageLike) bool {
return pkg.MatchesDependency(q.Dep)
}

Expand Down Expand Up @@ -247,8 +257,8 @@ func (q *DependencyQuery) String() string {
}

// Matches on specific properties
func (q *PkgQuery) Matches(pkg *Package) bool {
return pkg.Name == q.Pkg && pkg.Version == q.Version && pkg.Architecture == q.Arch
func (q *PkgQuery) Matches(pkg PackageLike) bool {
return pkg.GetName() == q.Pkg && pkg.GetVersion() == q.Version && pkg.GetArchitecture() == q.Arch
}

// Fast is always true for package query
Expand Down

0 comments on commit 4d622e4

Please sign in to comment.