diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-21 22:12:04 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-27 23:03:05 +0200 |
commit | 2ab6381a94d55fa22b80acdbb18849d6b24951f9 (patch) | |
tree | 99942b000955623ea7466b9fa4cc7dab37645df6 /api/graphql/models/lazy_bug.go | |
parent | 5f72b04ef8e84b1c367ca6874519706318e351f5 (diff) | |
download | git-bug-2ab6381a94d55fa22b80acdbb18849d6b24951f9.tar.gz |
Reorganize the webUI and API code
Included in the changes:
- create a new /api root package to hold all API code, migrate /graphql in there
- git API handlers all use the cache instead of the repo directly
- git API handlers are now tested
- git API handlers now require a "repo" mux parameter
- lots of untangling of API/handlers/middleware
- less code in commands/webui.go
Diffstat (limited to 'api/graphql/models/lazy_bug.go')
-rw-r--r-- | api/graphql/models/lazy_bug.go | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/api/graphql/models/lazy_bug.go b/api/graphql/models/lazy_bug.go new file mode 100644 index 00000000..a7840df2 --- /dev/null +++ b/api/graphql/models/lazy_bug.go @@ -0,0 +1,215 @@ +package models + +import ( + "sync" + "time" + + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/entity" +) + +// BugWrapper is an interface used by the GraphQL resolvers to handle a bug. +// Depending on the situation, a Bug can already be fully loaded in memory or not. +// This interface is used to wrap either a lazyBug or a loadedBug depending on the situation. +type BugWrapper interface { + Id() entity.Id + LastEdit() time.Time + Status() bug.Status + Title() string + Comments() ([]bug.Comment, error) + Labels() []bug.Label + Author() (IdentityWrapper, error) + Actors() ([]IdentityWrapper, error) + Participants() ([]IdentityWrapper, error) + CreatedAt() time.Time + Timeline() ([]bug.TimelineItem, error) + Operations() ([]bug.Operation, error) + + IsAuthored() +} + +var _ BugWrapper = &lazyBug{} + +// lazyBug is a lazy-loading wrapper that fetch data from the cache (BugExcerpt) in priority, +// and load the complete bug and snapshot only when necessary. +type lazyBug struct { + cache *cache.RepoCache + excerpt *cache.BugExcerpt + + mu sync.Mutex + snap *bug.Snapshot +} + +func NewLazyBug(cache *cache.RepoCache, excerpt *cache.BugExcerpt) *lazyBug { + return &lazyBug{ + cache: cache, + excerpt: excerpt, + } +} + +func (lb *lazyBug) load() error { + if lb.snap != nil { + return nil + } + + lb.mu.Lock() + defer lb.mu.Unlock() + + b, err := lb.cache.ResolveBug(lb.excerpt.Id) + if err != nil { + return err + } + + lb.snap = b.Snapshot() + return nil +} + +func (lb *lazyBug) identity(id entity.Id) (IdentityWrapper, error) { + i, err := lb.cache.ResolveIdentityExcerpt(id) + if err != nil { + return nil, err + } + return &lazyIdentity{cache: lb.cache, excerpt: i}, nil +} + +// Sign post method for gqlgen +func (lb *lazyBug) IsAuthored() {} + +func (lb *lazyBug) Id() entity.Id { + return lb.excerpt.Id +} + +func (lb *lazyBug) LastEdit() time.Time { + return lb.excerpt.EditTime() +} + +func (lb *lazyBug) Status() bug.Status { + return lb.excerpt.Status +} + +func (lb *lazyBug) Title() string { + return lb.excerpt.Title +} + +func (lb *lazyBug) Comments() ([]bug.Comment, error) { + err := lb.load() + if err != nil { + return nil, err + } + return lb.snap.Comments, nil +} + +func (lb *lazyBug) Labels() []bug.Label { + return lb.excerpt.Labels +} + +func (lb *lazyBug) Author() (IdentityWrapper, error) { + return lb.identity(lb.excerpt.AuthorId) +} + +func (lb *lazyBug) Actors() ([]IdentityWrapper, error) { + result := make([]IdentityWrapper, len(lb.excerpt.Actors)) + for i, actorId := range lb.excerpt.Actors { + actor, err := lb.identity(actorId) + if err != nil { + return nil, err + } + result[i] = actor + } + return result, nil +} + +func (lb *lazyBug) Participants() ([]IdentityWrapper, error) { + result := make([]IdentityWrapper, len(lb.excerpt.Participants)) + for i, participantId := range lb.excerpt.Participants { + participant, err := lb.identity(participantId) + if err != nil { + return nil, err + } + result[i] = participant + } + return result, nil +} + +func (lb *lazyBug) CreatedAt() time.Time { + return lb.excerpt.CreateTime() +} + +func (lb *lazyBug) Timeline() ([]bug.TimelineItem, error) { + err := lb.load() + if err != nil { + return nil, err + } + return lb.snap.Timeline, nil +} + +func (lb *lazyBug) Operations() ([]bug.Operation, error) { + err := lb.load() + if err != nil { + return nil, err + } + return lb.snap.Operations, nil +} + +var _ BugWrapper = &loadedBug{} + +type loadedBug struct { + *bug.Snapshot +} + +func NewLoadedBug(snap *bug.Snapshot) *loadedBug { + return &loadedBug{Snapshot: snap} +} + +func (l *loadedBug) LastEdit() time.Time { + return l.Snapshot.EditTime() +} + +func (l *loadedBug) Status() bug.Status { + return l.Snapshot.Status +} + +func (l *loadedBug) Title() string { + return l.Snapshot.Title +} + +func (l *loadedBug) Comments() ([]bug.Comment, error) { + return l.Snapshot.Comments, nil +} + +func (l *loadedBug) Labels() []bug.Label { + return l.Snapshot.Labels +} + +func (l *loadedBug) Author() (IdentityWrapper, error) { + return NewLoadedIdentity(l.Snapshot.Author), nil +} + +func (l *loadedBug) Actors() ([]IdentityWrapper, error) { + res := make([]IdentityWrapper, len(l.Snapshot.Actors)) + for i, actor := range l.Snapshot.Actors { + res[i] = NewLoadedIdentity(actor) + } + return res, nil +} + +func (l *loadedBug) Participants() ([]IdentityWrapper, error) { + res := make([]IdentityWrapper, len(l.Snapshot.Participants)) + for i, participant := range l.Snapshot.Participants { + res[i] = NewLoadedIdentity(participant) + } + return res, nil +} + +func (l *loadedBug) CreatedAt() time.Time { + return l.Snapshot.CreateTime +} + +func (l *loadedBug) Timeline() ([]bug.TimelineItem, error) { + return l.Snapshot.Timeline, nil +} + +func (l *loadedBug) Operations() ([]bug.Operation, error) { + return l.Snapshot.Operations, nil +} |