Verified Commit 2c6306c4 authored by Loïc Dachary's avatar Loïc Dachary
Browse files

simplify generics

parent 8bec68a1
Pipeline #1081 passed with stage
in 2 minutes and 52 seconds
This diff is collapsed.
......@@ -40,7 +40,7 @@ func (o *Comment) SetID(id int64) {
o.Comment.ID = id
}
func (o Comment) Equals(other Comment) bool {
func (o *Comment) Equals(other *Comment) bool {
return o.Comment.Body == other.Comment.Body
}
......@@ -99,9 +99,11 @@ func (o *CommentProvider) ToFormat(comment *Comment) *format.Comment {
return f
}
func (o *CommentProvider) FromFormat(comment *Comment, f *format.Comment) {
func (o *CommentProvider) FromFormat(f *format.Comment) *Comment {
var comment Comment
comment.FromFormat(*f)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceComment, f, comment)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceComment, f, &comment)
return &comment
}
func (o *CommentProvider) StoreForeignReference(comment *Comment, f *format.Comment) {
......@@ -113,8 +115,8 @@ func (o *CommentProvider) Init(gitea *Gitea) *CommentProvider {
return o
}
func (o *CommentProvider) GetComments(commentable format.Commentable) []Comment {
allComments := make([]Comment, 0, o.g.perPage)
func (o *CommentProvider) GetComments(commentable format.Commentable) []*Comment {
allComments := make([]*Comment, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -126,7 +128,7 @@ func (o *CommentProvider) GetComments(commentable format.Commentable) []Comment
for _, comment := range comments {
reactions := getCommentReactions(o.g, o.project, comment.ID)
allComments = append(allComments, Comment{
allComments = append(allComments, &Comment{
Comment: *comment,
CommentableIndex: commentable.GetID(),
Reactions: reactions,
......
......@@ -36,7 +36,7 @@ func (o *Issue) SetID(id int64) {
o.Index = id
}
func (o Issue) Equals(other Issue) bool {
func (o *Issue) Equals(other *Issue) bool {
return o.Title == other.Title
}
......@@ -136,8 +136,10 @@ func (o *IssueProvider) ToFormat(issue *Issue) *format.Issue {
return issue.ToFormat()
}
func (o *IssueProvider) FromFormat(issue *Issue, i *format.Issue) {
func (o *IssueProvider) FromFormat(i *format.Issue) *Issue {
var issue Issue
issue.FromFormat(*i)
return &issue
}
func (o *IssueProvider) StoreForeignReference(issue *Issue, i *format.Issue) {
......@@ -148,8 +150,8 @@ func (o *IssueProvider) Init(gitea *Gitea) *IssueProvider {
return o
}
func (o *IssueProvider) GetIssues() []Issue {
allIssues := make([]Issue, 0, o.g.perPage)
func (o *IssueProvider) GetIssues() []*Issue {
allIssues := make([]*Issue, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -171,7 +173,7 @@ func (o *IssueProvider) GetIssues() []Issue {
Reactions: reactions,
}
allIssues = append(allIssues, i)
allIssues = append(allIssues, &i)
}
if len(issues) < o.g.perPage {
......
......@@ -34,7 +34,7 @@ func (o *Label) SetID(id int64) {
o.ID = id
}
func (o Label) Equals(other Label) bool {
func (o *Label) Equals(other *Label) bool {
return o.Name == other.Name
}
......@@ -67,9 +67,11 @@ func (o *LabelProvider) ToFormat(label *Label) *format.Label {
return m
}
func (o *LabelProvider) FromFormat(label *Label, m *format.Label) {
func (o *LabelProvider) FromFormat(m *format.Label) *Label {
var label Label
label.FromFormat(*m)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceLabel, m, label)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceLabel, m, &label)
return &label
}
func (o *LabelProvider) StoreForeignReference(label *Label, m *format.Label) {
......@@ -81,8 +83,8 @@ func (o *LabelProvider) Init(gitea *Gitea) *LabelProvider {
return o
}
func (o *LabelProvider) GetLabels() []Label {
allLabels := make([]Label, 0, o.g.perPage)
func (o *LabelProvider) GetLabels() []*Label {
allLabels := make([]*Label, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -95,7 +97,8 @@ func (o *LabelProvider) GetLabels() []Label {
}
for _, label := range labels {
allLabels = append(allLabels, Label(*label))
l := Label(*label)
allLabels = append(allLabels, &l)
}
if len(labels) < o.g.perPage {
......
......@@ -35,7 +35,7 @@ func (o *Milestone) SetID(id int64) {
o.ID = id
}
func (o Milestone) Equals(other Milestone) bool {
func (o *Milestone) Equals(other *Milestone) bool {
return o.Title == other.Title
}
......@@ -90,9 +90,11 @@ func (o *MilestoneProvider) ToFormat(milestone *Milestone) *format.Milestone {
return m
}
func (o *MilestoneProvider) FromFormat(milestone *Milestone, m *format.Milestone) {
func (o *MilestoneProvider) FromFormat(m *format.Milestone) *Milestone {
var milestone Milestone
milestone.FromFormat(*m)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceMilestone, m, milestone)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceMilestone, m, &milestone)
return &milestone
}
func (o *MilestoneProvider) StoreForeignReference(milestone *Milestone, m *format.Milestone) {
......@@ -104,8 +106,8 @@ func (o *MilestoneProvider) Init(gitea *Gitea) *MilestoneProvider {
return o
}
func (o *MilestoneProvider) GetMilestones() []Milestone {
allMilestones := make([]Milestone, 0, o.g.perPage)
func (o *MilestoneProvider) GetMilestones() []*Milestone {
allMilestones := make([]*Milestone, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -119,7 +121,8 @@ func (o *MilestoneProvider) GetMilestones() []Milestone {
}
for _, milestone := range milestones {
allMilestones = append(allMilestones, Milestone(*milestone))
m := Milestone(*milestone)
allMilestones = append(allMilestones, &m)
}
if len(milestones) < o.g.perPage {
......
......@@ -34,7 +34,7 @@ func (o *Project) SetID(id int64) {
o.ID = id
}
func (o Project) Equals(other Project) bool {
func (o *Project) Equals(other *Project) bool {
return (o.Name == other.Name &&
o.Description == other.Description)
}
......@@ -75,9 +75,11 @@ func (o *ProjectProvider) ToFormat(project *Project) *format.Project {
return p
}
func (o *ProjectProvider) FromFormat(project *Project, p *format.Project) {
func (o *ProjectProvider) FromFormat(p *format.Project) *Project {
var project Project
project.FromFormat(*p)
o.g.GetDb().ForeignToLocal(0, gofff.ForeignReferenceProject, p, project)
o.g.GetDb().ForeignToLocal(0, gofff.ForeignReferenceProject, p, &project)
return &project
}
func (o *ProjectProvider) StoreForeignReference(project *Project, f *format.Project) {
......
......@@ -41,7 +41,7 @@ func (o *PullRequest) SetID(id int64) {
o.Index = id
}
func (o PullRequest) Equals(other PullRequest) bool {
func (o *PullRequest) Equals(other *PullRequest) bool {
return o.Title == other.Title
}
......@@ -241,8 +241,10 @@ func (o *PullRequestProvider) ToFormat(pullRequest *PullRequest) *format.PullReq
return pullRequest.ToFormat()
}
func (o *PullRequestProvider) FromFormat(pullRequest *PullRequest, pr *format.PullRequest) {
func (o *PullRequestProvider) FromFormat(pr *format.PullRequest) *PullRequest {
var pullRequest PullRequest
pullRequest.FromFormat(*pr)
return &pullRequest
}
func (o *PullRequestProvider) StoreForeignReference(pullRequest *PullRequest, pr *format.PullRequest) {
......@@ -364,8 +366,8 @@ func (o *PullRequestProvider) updateGitForPullRequest(repository string, pr *Pul
return
}
func (o *PullRequestProvider) GetPullRequests(repository string) []PullRequest {
allPullRequests := make([]PullRequest, 0, o.g.perPage)
func (o *PullRequestProvider) GetPullRequests(repository string) []*PullRequest {
allPullRequests := make([]*PullRequest, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -376,7 +378,7 @@ func (o *PullRequestProvider) GetPullRequests(repository string) []PullRequest {
})
// When there are not pull requests it returns 404 instead of an empty list
if resp.StatusCode == 404 {
return []PullRequest{}
return []*PullRequest{}
}
if err != nil {
panic(fmt.Errorf("error while listing pullRequests: %v", err))
......@@ -396,7 +398,7 @@ func (o *PullRequestProvider) GetPullRequests(repository string) []PullRequest {
o.g.options.Logger.Warn(message)
}
allPullRequests = append(allPullRequests, pr)
allPullRequests = append(allPullRequests, &pr)
}
if len(pullRequests) < o.g.perPage {
......
......@@ -47,7 +47,7 @@ func (o *Attachment) SetID(id int64) {
o.ID = id
}
func (o Release) Equals(other Release) bool {
func (o *Release) Equals(other *Release) bool {
return cmp.Equal(o, other)
}
......@@ -149,13 +149,15 @@ func (o *ReleaseProvider) ToFormat(r *Release) *format.Release {
return rf
}
func (o *ReleaseProvider) FromFormat(r *Release, rf *format.Release) {
func (o *ReleaseProvider) FromFormat(rf *format.Release) *Release {
var r Release
r.FromFormat(*rf)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceRelease, rf, r)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceRelease, rf, &r)
for i, af := range rf.Assets {
a := r.Attachments[i]
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceAttachment, af, (*Attachment)(a))
}
return &r
}
func (o *ReleaseProvider) StoreForeignReference(r *Release, rf *format.Release) {
......@@ -166,8 +168,8 @@ func (o *ReleaseProvider) StoreForeignReference(r *Release, rf *format.Release)
}
}
func (o *ReleaseProvider) GetReleases() []Release {
allReleases := make([]Release, 0, o.g.perPage)
func (o *ReleaseProvider) GetReleases() []*Release {
allReleases := make([]*Release, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -180,7 +182,8 @@ func (o *ReleaseProvider) GetReleases() []Release {
}
for _, release := range releases {
allReleases = append(allReleases, Release(*release))
r := Release(*release)
allReleases = append(allReleases, &r)
}
if len(releases) < o.g.perPage {
......
......@@ -40,7 +40,7 @@ func (o *Review) SetID(id format.ReviewIndexType) {
o.ReviewableIndex = id.PullRequestID
}
func (o Review) Equals(other Review) bool {
func (o *Review) Equals(other *Review) bool {
return o.PullReview.Body == other.PullReview.Body
}
......@@ -140,9 +140,11 @@ func (o *ReviewProvider) ToFormat(review *Review) *format.Review {
return f
}
func (o *ReviewProvider) FromFormat(review *Review, f *format.Review) {
func (o *ReviewProvider) FromFormat(f *format.Review) *Review {
var review Review
review.FromFormat(*f)
o.g.GetDb().ForeignToLocal(o.project.ID, gofff.ForeignReferenceReview, f, (*ForeignReview)(&review.PullReview))
return &review
}
func (o *ReviewProvider) StoreForeignReference(review *Review, f *format.Review) {
......@@ -154,8 +156,8 @@ func (o *ReviewProvider) Init(gitea *Gitea) *ReviewProvider {
return o
}
func (o *ReviewProvider) GetReviews(reviewable format.Reviewable) []Review {
allReviews := make([]Review, 0, o.g.perPage)
func (o *ReviewProvider) GetReviews(reviewable format.Reviewable) []*Review {
allReviews := make([]*Review, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
......@@ -173,7 +175,7 @@ func (o *ReviewProvider) GetReviews(reviewable format.Reviewable) []Review {
panic(err)
}
allReviews = append(allReviews, Review{
allReviews = append(allReviews, &Review{
PullReview: *review,
Comments: comments,
ReviewableIndex: reviewable.GetID(),
......
......@@ -18,41 +18,36 @@ type CacheableConstraint[
Cacheable any,
Index comparable,
] interface {
*Cacheable
GetID() Index
SetID(Index)
Equals(Cacheable) bool
}
type Cache[
Cacheable any,
CacheablePtr CacheableConstraint[Cacheable, Index],
Cacheable CacheableConstraint[Cacheable, Index],
Index comparable,
] struct {
m map[Index]CacheablePtr
m map[Index]Cacheable
}
func (c *Cache[Cacheable, CacheablePtr, Index]) Init() {
c.m = make(map[Index]CacheablePtr)
func (c *Cache[Cacheable, Index]) Init() {
c.m = make(map[Index]Cacheable)
}
func (c *Cache[Cacheable, CacheablePtr, Index]) Store(cacheable CacheablePtr) {
func (c *Cache[Cacheable, Index]) Store(cacheable Cacheable) {
c.m[cacheable.GetID()] = cacheable
}
func (c *Cache[Cacheable, CacheablePtr, Index]) Load(id Index) CacheablePtr {
return c.m[id]
func (c *Cache[Cacheable, Index]) Load(id Index) (Cacheable, bool) {
v, ok := c.m[id]
return v, ok
}
type ProviderConstraint[
Bean any,
BeanPtr CacheableConstraint[Bean, Index],
Provider any,
Index comparable,
] interface {
*Provider
Get(Index) BeanPtr
Put(*Bean) BeanPtr
Get(Index) Bean
Put(Bean) Bean
}
func zero[Index comparable]() (zero Index) {
......@@ -60,26 +55,23 @@ func zero[Index comparable]() (zero Index) {
}
func UpsertBean[
Bean any,
Provider any,
Bean CacheableConstraint[Bean, Index],
Index comparable,
BeanPtr CacheableConstraint[Bean, Index],
ProviderPtr ProviderConstraint[Bean, BeanPtr, Provider, Index],
](cache *Cache[Bean, BeanPtr, Index], provider ProviderPtr, bean BeanPtr,
) (before, after BeanPtr) {
](cache *Cache[Bean, Index], provider ProviderConstraint[Bean, Index], bean Bean,
) (before, after Bean) {
id := bean.GetID()
var existing *Bean
var existing Bean
var ok bool
if id == zero[Index]() {
existing = (*Bean)(cache.Load(id))
if existing != nil && BeanPtr(existing).GetID() != id {
existing = (*Bean)(provider.Get(id))
existing, ok = cache.Load(id)
if ok && existing.GetID() != id {
existing = provider.Get(id)
}
}
if existing != nil && bean.Equals(*existing) {
if ok && bean.Equals(existing) {
return existing, existing
}
new := provider.Put((*Bean)(bean))
new := provider.Put(bean)
cache.Store(new)
return existing, new
}
......@@ -33,7 +33,7 @@ func (b *Thing) SetID(id int64) {
b.id = id
}
func (b *Thing) Equals(other Thing) bool {
func (b *Thing) Equals(other *Thing) bool {
return b.value == other.value
}
......@@ -56,7 +56,7 @@ type Application struct {
type Something struct {
application Application
things Cache[Thing, *Thing, int64]
things Cache[*Thing, int64]
}
func (s *Something) Init() {
......@@ -71,7 +71,7 @@ func TestInt64(t *testing.T) {
assert.NotEmpty(t, s.things)
thing := Thing{id: 1, value: 100}
before, after := UpsertBean[Thing, ThingProvider, int64, *Thing, *ThingProvider](&s.things, &s.application.thingProvider, &thing)
before, after := UpsertBean[*Thing, int64](&s.things, &s.application.thingProvider, &thing)
assert.Nil(t, before)
assert.EqualValues(t, thing, *after)
}
......@@ -94,7 +94,7 @@ func (b *IndexThing) SetID(id Index) {
b.id = id
}
func (b *IndexThing) Equals(other IndexThing) bool {
func (b *IndexThing) Equals(other *IndexThing) bool {
return b.value == other.value
}
......@@ -117,7 +117,7 @@ type IndexApplication struct {
type IndexSomething struct {
application IndexApplication
things Cache[IndexThing, *IndexThing, Index]
things Cache[*IndexThing, Index]
}
func (s *IndexSomething) Init() {
......@@ -132,7 +132,7 @@ func TestIndex(t *testing.T) {
assert.NotEmpty(t, s.things)
thing := IndexThing{id: Index{id1: 1, id2: 2}, value: 100}
before, after := UpsertBean[IndexThing, IndexThingProvider, Index, *IndexThing, *IndexThingProvider](&s.things, &s.application.thingProvider, &thing)
before, after := UpsertBean[*IndexThing, Index](&s.things, &s.application.thingProvider, &thing)
assert.Nil(t, before)
assert.EqualValues(t, thing, *after)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment