diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-02 14:56:50 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-02 14:56:50 +0200 |
commit | e6a64b4985f5d342b8e75fecaf4d138f4f50487e (patch) | |
tree | f28a1fa4f84d9f67076711b3bf972804f3b551d1 /cache/cache.go | |
parent | e5a6a71b78b0d8c5ef0d52c3eeb279d5738b9cf7 (diff) | |
download | git-bug-e6a64b4985f5d342b8e75fecaf4d138f4f50487e.tar.gz |
cache: some refactoring
Diffstat (limited to 'cache/cache.go')
-rw-r--r-- | cache/cache.go | 153 |
1 files changed, 63 insertions, 90 deletions
diff --git a/cache/cache.go b/cache/cache.go index 4d6a93d1..cbd57802 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -27,23 +27,23 @@ type RepoCacher interface { ResolveBugPrefix(prefix string) (BugCacher, error) AllBugIds() ([]string, error) ClearAllBugs() - Commit(bug BugCacher) error // Mutations - NewBug(title string, message string) (BugCacher, error) - - AddComment(repoRef *string, prefix string, message string) (BugCacher, error) - ChangeLabels(repoRef *string, prefix string, added []string, removed []string) (BugCacher, error) - Open(repoRef *string, prefix string) (BugCacher, error) - Close(repoRef *string, prefix string) (BugCacher, error) - SetTitle(repoRef *string, prefix string, title string) (BugCacher, error) } type BugCacher interface { Snapshot() *bug.Snapshot ClearSnapshot() - bug() *bug.Bug + + // Mutations + AddComment(message string) (BugCacher, error) + ChangeLabels(added []string, removed []string) (BugCacher, error) + Open() (BugCacher, error) + Close() (BugCacher, error) + SetTitle(title string) (BugCacher, error) + + Commit() (BugCacher, error) } // Cacher ------------------------ @@ -135,7 +135,7 @@ func (c *RepoCache) ResolveBug(id string) (BugCacher, error) { return nil, err } - cached = NewBugCache(b) + cached = NewBugCache(c.repo, b) c.bugs[id] = cached return cached, nil @@ -168,7 +168,7 @@ func (c *RepoCache) ResolveBugPrefix(prefix string) (BugCacher, error) { return nil, err } - cached := NewBugCache(b) + cached := NewBugCache(c.repo, b) c.bugs[b.Id()] = cached return cached, nil @@ -182,14 +182,6 @@ func (c *RepoCache) ClearAllBugs() { c.bugs = make(map[string]BugCacher) } -func (c *RepoCache) Commit(bug BugCacher) error { - err := bug.bug().Commit(c.repo) - if err != nil { - return err - } - return nil -} - func (c *RepoCache) NewBug(title string, message string) (BugCacher, error) { author, err := bug.GetUser(c.repo) if err != nil { @@ -206,135 +198,116 @@ func (c *RepoCache) NewBug(title string, message string) (BugCacher, error) { return nil, err } - cached := NewBugCache(b) + cached := NewBugCache(c.repo, b) c.bugs[b.Id()] = cached return cached, nil } -func (c *RepoCache) AddComment(repoRef *string, prefix string, message string) (BugCacher, error) { - author, err := bug.GetUser(c.repo) - if err != nil { - return nil, err +// Bug ------------------------ + +type BugCache struct { + repo repository.Repo + bug *bug.Bug + snap *bug.Snapshot +} + +func NewBugCache(repo repository.Repo, b *bug.Bug) BugCacher { + return &BugCache{ + repo: repo, + bug: b, + } +} + +func (c *BugCache) Snapshot() *bug.Snapshot { + if c.snap == nil { + snap := c.bug.Compile() + c.snap = &snap } + return c.snap +} - cached, err := c.ResolveBugPrefix(prefix) +func (c *BugCache) ClearSnapshot() { + c.snap = nil +} + +func (c *BugCache) AddComment(message string) (BugCacher, error) { + author, err := bug.GetUser(c.repo) if err != nil { return nil, err } - operations.Comment(cached.bug(), author, message) + operations.Comment(c.bug, author, message) // TODO: perf --> the snapshot could simply be updated with the new op - cached.ClearSnapshot() + c.ClearSnapshot() - return cached, nil + return c, nil } -func (c *RepoCache) ChangeLabels(repoRef *string, prefix string, added []string, removed []string) (BugCacher, error) { +func (c *BugCache) ChangeLabels(added []string, removed []string) (BugCacher, error) { author, err := bug.GetUser(c.repo) if err != nil { return nil, err } - cached, err := c.ResolveBugPrefix(prefix) - if err != nil { - return nil, err - } - - err = operations.ChangeLabels(nil, cached.bug(), author, added, removed) + err = operations.ChangeLabels(nil, c.bug, author, added, removed) if err != nil { return nil, err } // TODO: perf --> the snapshot could simply be updated with the new op - cached.ClearSnapshot() + c.ClearSnapshot() - return cached, nil + return c, nil } -func (c *RepoCache) Open(repoRef *string, prefix string) (BugCacher, error) { +func (c *BugCache) Open() (BugCacher, error) { author, err := bug.GetUser(c.repo) if err != nil { return nil, err } - cached, err := c.ResolveBugPrefix(prefix) - if err != nil { - return nil, err - } - - operations.Open(cached.bug(), author) + operations.Open(c.bug, author) // TODO: perf --> the snapshot could simply be updated with the new op - cached.ClearSnapshot() + c.ClearSnapshot() - return cached, nil + return c, nil } -func (c *RepoCache) Close(repoRef *string, prefix string) (BugCacher, error) { +func (c *BugCache) Close() (BugCacher, error) { author, err := bug.GetUser(c.repo) if err != nil { return nil, err } - cached, err := c.ResolveBugPrefix(prefix) - if err != nil { - return nil, err - } - - operations.Close(cached.bug(), author) + operations.Close(c.bug, author) // TODO: perf --> the snapshot could simply be updated with the new op - cached.ClearSnapshot() + c.ClearSnapshot() - return cached, nil + return c, nil } -func (c *RepoCache) SetTitle(repoRef *string, prefix string, title string) (BugCacher, error) { +func (c *BugCache) SetTitle(title string) (BugCacher, error) { author, err := bug.GetUser(c.repo) if err != nil { return nil, err } - cached, err := c.ResolveBugPrefix(prefix) - if err != nil { - return nil, err - } - - operations.SetTitle(cached.bug(), author, title) + operations.SetTitle(c.bug, author, title) // TODO: perf --> the snapshot could simply be updated with the new op - cached.ClearSnapshot() - - return cached, nil -} - -// Bug ------------------------ - -type BugCache struct { - b *bug.Bug - snap *bug.Snapshot -} + c.ClearSnapshot() -func NewBugCache(b *bug.Bug) BugCacher { - return &BugCache{ - b: b, - } + return c, nil } -func (c *BugCache) Snapshot() *bug.Snapshot { - if c.snap == nil { - snap := c.b.Compile() - c.snap = &snap +func (c *BugCache) Commit() (BugCacher, error) { + err := c.bug.Commit(c.repo) + if err != nil { + return nil, err } - return c.snap -} - -func (c *BugCache) ClearSnapshot() { - c.snap = nil -} - -func (c *BugCache) bug() *bug.Bug { - return c.b + return c, nil } |