enforce unique names
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
5ef3669e7d
commit
d6e0482465
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -11,10 +12,29 @@ func main() {
|
|||
all = new(zoopb.Packages)
|
||||
// r = zoopb.LoadJSON("go.wit.com/lib/protobuf/zoopb")
|
||||
|
||||
newP := new(zoopb.Package)
|
||||
newP.Name = "bash"
|
||||
newP.Version = "5.2.21"
|
||||
all.Append(newP)
|
||||
new1 := new(zoopb.Package)
|
||||
new1.Name = "bash"
|
||||
new1.Version = "5.2.21"
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new2 := new(zoopb.Package)
|
||||
new2.Name = "go-clone"
|
||||
new2.Version = "0.6.8" // good version of the macos
|
||||
if all.Append(new2) {
|
||||
log.Info("added", new2.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new2.Name, "failed")
|
||||
}
|
||||
|
||||
if all.Append(new2) {
|
||||
log.Info("added", new2.Name, "ok (this is bad)")
|
||||
} else {
|
||||
log.Info("added", new2.Name, "failed (but ok)")
|
||||
}
|
||||
|
||||
fmt.Println("packages are:", len(all.Packages))
|
||||
}
|
||||
|
|
25
packages.go
25
packages.go
|
@ -70,12 +70,33 @@ func (r *Packages) SortByName() *PackageIterator {
|
|||
return iterator
|
||||
}
|
||||
|
||||
// should this be a pointer? what really happens here?
|
||||
func (r *Packages) Append(newP *Package) {
|
||||
// enforces no duplicate package names
|
||||
func (r *Packages) Append(newP *Package) bool {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
for _, p := range r.Packages {
|
||||
if p.Name == newP.Name {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
r.Packages = append(r.Packages, newP)
|
||||
return true
|
||||
}
|
||||
|
||||
// find a package by name
|
||||
func (r *Packages) FindByName(name string) *Package {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
for _, p := range r.Packages {
|
||||
if p.Name == name {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ByName []*Package
|
||||
|
|
Loading…
Reference in New Issue