Created
July 31, 2015 01:55
-
-
Save alapidas/9c18f9549f5d7c910418 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package model | |
// TODO: use tags here for JSON unmarshaling | |
// TODO: maybe use the receiver object pattern from http://blog.golang.org/json-and-go | |
// TODO: Figure out the problem with public structs (http://stackoverflow.com/questions/11126793/golang-json-and-dealing-with-unexported-fields) | |
import ( | |
"fmt" | |
trie "github.com/tchap/go-patricia/patricia" | |
) | |
var ( | |
ErrPackageNoExists = fmt.Errorf("package does not exist") | |
) | |
type Repoer interface { | |
AddPackage(path string, pkg Packager, replace bool) error // path + package | |
GetPackage(path string) (Packager, error) // path | |
DeletePackage(path string) error // path | |
//Query(path string) ([]Packager, error) | |
} | |
type Filesystem trie.Trie | |
type Repo struct { | |
*trie.Trie // TODO: this should really be a filesystem-like object | |
RootPath string | |
Name string `json:"name"` | |
//LocalPath string `json:"localPath"` | |
// other repo-related metadata | |
} | |
var _ Repoer = (*Repo)(nil) | |
func NewRepo(root, name string) (Repoer, error) { | |
return &Repo{Trie: trie.NewTrie(), RootPath: root, Name: name}, nil | |
} | |
func (repo *Repo) AddPackage(path string, pkg Packager, replace bool) error { | |
if pkg == nil { | |
return fmt.Errorf("Cannot add a nil package to a repo (at path %s)", path) | |
} | |
switch replace { | |
case false: | |
success := repo.Insert(trie.Prefix(path), pkg) | |
if !success { | |
return fmt.Errorf("Package already exists at path %s", path) | |
} | |
case true: | |
repo.Set(trie.Prefix(path), pkg) | |
return nil | |
} | |
return nil | |
} | |
func (repo *Repo) GetPackage(path string) (Packager, error) { | |
return repo.Get(trie.Prefix(path)), nil | |
} | |
func (repo *Repo) DeletePackage(path string) error { | |
if deleted := repo.Delete(trie.Prefix(path)); !deleted { | |
return ErrPackageNoExists | |
} | |
return nil | |
} | |
/* | |
type repos []*repo | |
type repo struct { | |
Name string `json:"name"` | |
LocalPath string `json:"localPath"` | |
} | |
type Repo struct { | |
*repo | |
} | |
func (self Repo) Name() string { | |
return self.repo.Name | |
} | |
func (self *repos) AddRepo(repo *Repo) error { | |
*self = append(*self, &Repo{repo}) | |
return nil | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment