Dependency to string.

This commit is contained in:
Andrey Smirnov
2014-01-09 14:18:28 +04:00
parent e37fe33203
commit 4e2fce7251
2 changed files with 30 additions and 0 deletions

20
debian/version.go vendored
View File

@@ -193,6 +193,26 @@ func (d *Dependency) Hash() string {
return fmt.Sprintf("%s:%s:%d:%s", d.Architecture, d.Pkg, d.Relation, d.Version)
}
// String produces human-readable representation
func (d *Dependency) String() string {
var rel string
switch d.Relation {
case VersionEqual:
rel = "="
case VersionGreater:
rel = ">>"
case VersionLess:
rel = "<<"
case VersionGreaterOrEqual:
rel = ">="
case VersionLessOrEqual:
rel = "<="
case VersionDontCare:
return fmt.Sprintf("%s [%s]", d.Pkg, d.Architecture)
}
return fmt.Sprintf("%s (%s %s) [%s]", d.Pkg, rel, d.Version, d.Architecture)
}
// parseDependency parses dependency in format "pkg (>= 1.35)" into parts
func parseDependency(dep string) (d Dependency, err error) {
if !strings.HasSuffix(dep, ")") {

View File

@@ -153,3 +153,13 @@ func (s *VersionSuite) TestParseDependency(c *C) {
d, e = parseDependency("dpkg==1.6)")
c.Check(e, ErrorMatches, "unable to parse.*")
}
func (s *VersionSuite) TestDependencyString(c *C) {
d, _ := parseDependency("dpkg(>>1.6)")
d.Architecture = "i386"
c.Check(d.String(), Equals, "dpkg (>> 1.6) [i386]")
d, _ = parseDependency("dpkg")
d.Architecture = "i386"
c.Check(d.String(), Equals, "dpkg [i386]")
}