This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
157 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package nut | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type AddDependencyError struct { | ||
ex, add *Dependency | ||
} | ||
|
||
// check interface | ||
var ( | ||
_ error = &AddDependencyError{} | ||
) | ||
|
||
func (e *AddDependencyError) Error() string { | ||
return fmt.Sprintf("Can't add %s to existing dependecy %s", e.add, e.ex) | ||
} | ||
|
||
type Dependencies struct { | ||
d map[string]Dependency // import path to dependency | ||
} | ||
|
||
// check interfaces | ||
var ( | ||
_ fmt.Stringer = &Dependencies{} | ||
_ json.Marshaler = &Dependencies{} | ||
_ json.Unmarshaler = &Dependencies{} | ||
) | ||
|
||
func NewDependencies() (deps *Dependencies) { | ||
deps = new(Dependencies) | ||
deps.Clear() | ||
return | ||
} | ||
|
||
func (deps *Dependencies) String() string { | ||
paths := deps.ImportPaths() | ||
s := make([]string, len(paths)) | ||
for i, path := range paths { | ||
s[i] = deps.Get(path).String() | ||
} | ||
return strings.Join(s, ", ") | ||
} | ||
|
||
func (deps *Dependencies) Clear() { | ||
deps.d = make(map[string]Dependency) | ||
} | ||
|
||
func (deps *Dependencies) Add(d *Dependency) (err error) { | ||
e := deps.Get(d.ImportPath) | ||
if e == nil { | ||
deps.d[d.ImportPath] = *d | ||
return | ||
} | ||
|
||
e.parse() | ||
e.parsed.majorMin = maxSection(e.parsed.majorMin, d.MajorMin()) | ||
e.parsed.majorMax = minSection(e.parsed.majorMax, d.MajorMax()) | ||
e.parsed.minorMin = maxSection(e.parsed.minorMin, d.MinorMin()) | ||
e.parsed.minorMax = minSection(e.parsed.minorMax, d.MinorMax()) | ||
e.parsed.patchMin = maxSection(e.parsed.patchMin, d.PatchMin()) | ||
e.parsed.patchMax = minSection(e.parsed.patchMax, d.PatchMax()) | ||
|
||
if !e.parsed.valid() { | ||
err = &AddDependencyError{ex: deps.Get(d.ImportPath), add: d} | ||
return | ||
} | ||
|
||
e.Version = e.parsed.String() | ||
deps.d[d.ImportPath] = *e | ||
return | ||
} | ||
|
||
func (deps *Dependencies) Get(importPath string) (dep *Dependency) { | ||
d, ok := deps.d[importPath] | ||
if ok { | ||
dep = &d | ||
} | ||
return | ||
} | ||
|
||
func (deps *Dependencies) ImportPaths() (paths []string) { | ||
paths = make([]string, 0, len(deps.d)) | ||
for i := range deps.d { | ||
paths = append(paths, i) | ||
} | ||
sort.Strings(paths) | ||
return | ||
} | ||
|
||
func (deps *Dependencies) MarshalJSON() (b []byte, err error) { | ||
b = make([]byte, 0, 50*len(deps.d)) | ||
b = append(b, '{') | ||
for _, p := range deps.ImportPaths() { | ||
d := deps.Get(p) | ||
b = append(b, '"') | ||
b = append(b, d.ImportPath...) | ||
b = append(b, `":"`...) | ||
b = append(b, d.Version...) | ||
b = append(b, `",`...) | ||
} | ||
b[len(b)-1] = '}' | ||
return | ||
} | ||
|
||
func (deps *Dependencies) UnmarshalJSON(b []byte) (err error) { | ||
m := make(map[string]string) | ||
err = json.Unmarshal(b, &m) | ||
if err != nil { | ||
return | ||
} | ||
deps.Clear() // also make | ||
var d *Dependency | ||
for p, v := range m { | ||
d, err = NewDependency(p, v) | ||
if err != nil { | ||
return | ||
} | ||
err = deps.Add(d) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package nut_test | ||
|
||
import ( | ||
. "." | ||
. "launchpad.net/gocheck" | ||
) | ||
|
||
type Ds struct{} | ||
|
||
var _ = Suite(&Ds{}) | ||
|
||
func (*Ds) TestDependenciesAdd(c *C) { | ||
deps := NewDependencies() | ||
for _, v := range []string{"1.>=1.*", "1.>=2.*", "1.*.*"} { | ||
dep, err := NewDependency("gonuts.io/debug/crazy", v) | ||
c.Check(err, IsNil) | ||
err = deps.Add(dep) | ||
c.Check(err, IsNil) | ||
} | ||
c.Check(deps.Get("gonuts.io/debug/crazy").String(), Equals, "gonuts.io/debug/crazy (1.>=2.*)") | ||
|
||
dep, err := NewDependency("gonuts.io/debug/crazy", "2.*.*") | ||
c.Check(err, IsNil) | ||
err = deps.Add(dep) | ||
c.Check(err, FitsTypeOf, &AddDependencyError{}) | ||
c.Check(err.Error(), Equals, "Can't add gonuts.io/debug/crazy (2.*.*) to existing dependecy gonuts.io/debug/crazy (1.>=2.*)") | ||
c.Check(deps.Get("gonuts.io/debug/crazy").String(), Equals, "gonuts.io/debug/crazy (1.>=2.*)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters