From 0a2201503960a18a4308fcf9c13843c6b48569b0 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 29 Jun 2023 18:54:40 +0200 Subject: [PATCH] [F3] add GetLocalMatchingRemote with a default implementation --- services/f3/driver/asset.go | 2 +- services/f3/driver/base.go | 54 ++++++++++++++++++++++++++++++ services/f3/driver/comment.go | 2 +- services/f3/driver/driver.go | 26 +++++++------- services/f3/driver/issue.go | 3 +- services/f3/driver/label.go | 3 +- services/f3/driver/milestone.go | 3 +- services/f3/driver/project.go | 2 +- services/f3/driver/pull_request.go | 3 +- services/f3/driver/reaction.go | 2 +- services/f3/driver/release.go | 2 +- services/f3/driver/repository.go | 2 +- services/f3/driver/review.go | 2 +- services/f3/driver/topic.go | 2 +- services/f3/driver/user.go | 2 +- 15 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 services/f3/driver/base.go diff --git a/services/f3/driver/asset.go b/services/f3/driver/asset.go index 9463d2e20a..1aa49a101c 100644 --- a/services/f3/driver/asset.go +++ b/services/f3/driver/asset.go @@ -82,7 +82,7 @@ func (o *Asset) FromFormat(asset *format.ReleaseAsset) { } type AssetProvider struct { - g *Forgejo + BaseProvider } func (o *AssetProvider) ToFormat(ctx context.Context, asset *Asset) *format.ReleaseAsset { diff --git a/services/f3/driver/base.go b/services/f3/driver/base.go new file mode 100644 index 0000000000..eed508827b --- /dev/null +++ b/services/f3/driver/base.go @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: MIT + +package driver + +import ( + "context" + + "lab.forgefriends.org/friendlyforgeformat/gof3/forges/common" + "lab.forgefriends.org/friendlyforgeformat/gof3/format" +) + +type BaseProvider struct { + g *Forgejo +} + +func (o *BaseProvider) SetForgejo(g *Forgejo) { + o.g = g +} + +type BaseProviderConstraint[Provider any] interface { + *Provider + SetForgejo(*Forgejo) +} + +func NewProvider[T any, TPtr BaseProviderConstraint[T]](g *Forgejo) TPtr { + var p TPtr + p = new(T) + p.SetForgejo(g) + return p +} + +func (o *BaseProvider) GetLocalMatchingRemote(ctx context.Context, format format.Interface, parents ...common.ContainerObjectInterface) (string, bool) { + return "", false +} + +type BaseProviderWithProjectProvider struct { + BaseProvider + project *ProjectProvider +} + +func (o *BaseProviderWithProjectProvider) SetProjectProvider(project *ProjectProvider) { + o.project = project +} + +type BaseProviderWithProjectProviderConstraint[Provider any] interface { + BaseProviderConstraint[Provider] + SetProjectProvider(project *ProjectProvider) +} + +func NewProviderWithProjectProvider[T any, TPtr BaseProviderWithProjectProviderConstraint[T]](g *Forgejo, project *ProjectProvider) TPtr { + p := NewProvider[T, TPtr](g) + p.SetProjectProvider(project) + return p +} diff --git a/services/f3/driver/comment.go b/services/f3/driver/comment.go index 1ba740cc47..a489e45199 100644 --- a/services/f3/driver/comment.go +++ b/services/f3/driver/comment.go @@ -82,7 +82,7 @@ func (o *Comment) FromFormat(comment *format.Comment) { } type CommentProvider struct { - g *Forgejo + BaseProvider } func (o *CommentProvider) ToFormat(ctx context.Context, comment *Comment) *format.Comment { diff --git a/services/f3/driver/driver.go b/services/f3/driver/driver.go index 72d1d9fbb5..4f58e5bc5a 100644 --- a/services/f3/driver/driver.go +++ b/services/f3/driver/driver.go @@ -74,31 +74,31 @@ func (o *Forgejo) GetProvider(name string, parent common.ProviderInterface) comm } switch name { case driver.ProviderUser: - return driver.NewProvider[UserProvider, *UserProvider, User, *User, format.User, *format.User](driver.ProviderUser, &UserProvider{g: o}) + return driver.NewProvider[UserProvider, *UserProvider, User, *User, format.User, *format.User](driver.ProviderUser, NewProvider[UserProvider](o)) case driver.ProviderProject: - return driver.NewProviderWithParentOne[ProjectProvider, *ProjectProvider, Project, *Project, format.Project, *format.Project, User, *User](driver.ProviderProject, &ProjectProvider{g: o}) + return driver.NewProviderWithParentOne[ProjectProvider, *ProjectProvider, Project, *Project, format.Project, *format.Project, User, *User](driver.ProviderProject, NewProvider[ProjectProvider, *ProjectProvider](o)) case driver.ProviderMilestone: - return driver.NewProviderWithParentOneTwo[MilestoneProvider, *MilestoneProvider, Milestone, *Milestone, format.Milestone, *format.Milestone, User, *User, Project, *Project](driver.ProviderMilestone, &MilestoneProvider{g: o, project: parentImpl.(*ProjectProvider)}) + return driver.NewProviderWithParentOneTwo[MilestoneProvider, *MilestoneProvider, Milestone, *Milestone, format.Milestone, *format.Milestone, User, *User, Project, *Project](driver.ProviderMilestone, NewProviderWithProjectProvider[MilestoneProvider](o, parentImpl.(*ProjectProvider))) case driver.ProviderIssue: - return driver.NewProviderWithParentOneTwo[IssueProvider, *IssueProvider, Issue, *Issue, format.Issue, *format.Issue, User, *User, Project, *Project](driver.ProviderIssue, &IssueProvider{g: o, project: parentImpl.(*ProjectProvider)}) + return driver.NewProviderWithParentOneTwo[IssueProvider, *IssueProvider, Issue, *Issue, format.Issue, *format.Issue, User, *User, Project, *Project](driver.ProviderIssue, NewProviderWithProjectProvider[IssueProvider](o, parentImpl.(*ProjectProvider))) case driver.ProviderPullRequest: - return driver.NewProviderWithParentOneTwo[PullRequestProvider, *PullRequestProvider, PullRequest, *PullRequest, format.PullRequest, *format.PullRequest, User, *User, Project, *Project](driver.ProviderPullRequest, &PullRequestProvider{g: o, project: parentImpl.(*ProjectProvider)}) + return driver.NewProviderWithParentOneTwo[PullRequestProvider, *PullRequestProvider, PullRequest, *PullRequest, format.PullRequest, *format.PullRequest, User, *User, Project, *Project](driver.ProviderPullRequest, NewProviderWithProjectProvider[PullRequestProvider](o, parentImpl.(*ProjectProvider))) case driver.ProviderReview: - return driver.NewProviderWithParentOneTwoThree[ReviewProvider, *ReviewProvider, Review, *Review, format.Review, *format.Review, User, *User, Project, *Project, PullRequest, *PullRequest](driver.ProviderReview, &ReviewProvider{g: o}) + return driver.NewProviderWithParentOneTwoThree[ReviewProvider, *ReviewProvider, Review, *Review, format.Review, *format.Review, User, *User, Project, *Project, PullRequest, *PullRequest](driver.ProviderReview, NewProvider[ReviewProvider](o)) case driver.ProviderRepository: - return driver.NewProviderWithParentOneTwo[RepositoryProvider, *RepositoryProvider, Repository, *Repository, format.Repository, *format.Repository, User, *User, Project, *Project](driver.ProviderRepository, &RepositoryProvider{g: o}) + return driver.NewProviderWithParentOneTwo[RepositoryProvider, *RepositoryProvider, Repository, *Repository, format.Repository, *format.Repository, User, *User, Project, *Project](driver.ProviderRepository, NewProvider[RepositoryProvider](o)) case driver.ProviderTopic: - return driver.NewProviderWithParentOneTwo[TopicProvider, *TopicProvider, Topic, *Topic, format.Topic, *format.Topic, User, *User, Project, *Project](driver.ProviderTopic, &TopicProvider{g: o}) + return driver.NewProviderWithParentOneTwo[TopicProvider, *TopicProvider, Topic, *Topic, format.Topic, *format.Topic, User, *User, Project, *Project](driver.ProviderTopic, NewProvider[TopicProvider](o)) case driver.ProviderLabel: - return driver.NewProviderWithParentOneTwo[LabelProvider, *LabelProvider, Label, *Label, format.Label, *format.Label, User, *User, Project, *Project](driver.ProviderLabel, &LabelProvider{g: o, project: parentImpl.(*ProjectProvider)}) + return driver.NewProviderWithParentOneTwo[LabelProvider, *LabelProvider, Label, *Label, format.Label, *format.Label, User, *User, Project, *Project](driver.ProviderLabel, NewProviderWithProjectProvider[LabelProvider](o, parentImpl.(*ProjectProvider))) case driver.ProviderRelease: - return driver.NewProviderWithParentOneTwo[ReleaseProvider, *ReleaseProvider, Release, *Release, format.Release, *format.Release, User, *User, Project, *Project](driver.ProviderRelease, &ReleaseProvider{g: o}) + return driver.NewProviderWithParentOneTwo[ReleaseProvider, *ReleaseProvider, Release, *Release, format.Release, *format.Release, User, *User, Project, *Project](driver.ProviderRelease, NewProvider[ReleaseProvider](o)) case driver.ProviderAsset: - return driver.NewProviderWithParentOneTwoThree[AssetProvider, *AssetProvider, Asset, *Asset, format.ReleaseAsset, *format.ReleaseAsset, User, *User, Project, *Project, Release, *Release](driver.ProviderAsset, &AssetProvider{g: o}) + return driver.NewProviderWithParentOneTwoThree[AssetProvider, *AssetProvider, Asset, *Asset, format.ReleaseAsset, *format.ReleaseAsset, User, *User, Project, *Project, Release, *Release](driver.ProviderAsset, NewProvider[AssetProvider](o)) case driver.ProviderComment: - return driver.NewProviderWithParentOneTwoThreeInterface[CommentProvider, *CommentProvider, Comment, *Comment, format.Comment, *format.Comment, User, *User, Project, *Project](driver.ProviderComment, &CommentProvider{g: o}) + return driver.NewProviderWithParentOneTwoThreeInterface[CommentProvider, *CommentProvider, Comment, *Comment, format.Comment, *format.Comment, User, *User, Project, *Project](driver.ProviderComment, NewProvider[CommentProvider](o)) case driver.ProviderReaction: - return driver.NewProviderWithParentOneTwoRest[ReactionProvider, *ReactionProvider, Reaction, *Reaction, format.Reaction, *format.Reaction, User, *User, Project, *Project](driver.ProviderReaction, &ReactionProvider{g: o}) + return driver.NewProviderWithParentOneTwoRest[ReactionProvider, *ReactionProvider, Reaction, *Reaction, format.Reaction, *format.Reaction, User, *User, Project, *Project](driver.ProviderReaction, NewProvider[ReactionProvider](o)) default: panic(fmt.Sprintf("unknown provider name %s", name)) } diff --git a/services/f3/driver/issue.go b/services/f3/driver/issue.go index 180b8de044..55fb427ecb 100644 --- a/services/f3/driver/issue.go +++ b/services/f3/driver/issue.go @@ -125,8 +125,7 @@ func (o *Issue) FromFormat(issue *format.Issue) { } type IssueProvider struct { - g *Forgejo - project *ProjectProvider + BaseProviderWithProjectProvider } func (o *IssueProvider) ToFormat(ctx context.Context, issue *Issue) *format.Issue { diff --git a/services/f3/driver/label.go b/services/f3/driver/label.go index cd511b4418..71c87956cc 100644 --- a/services/f3/driver/label.go +++ b/services/f3/driver/label.go @@ -76,8 +76,7 @@ func (o *Label) FromFormat(label *format.Label) { } type LabelProvider struct { - g *Forgejo - project *ProjectProvider + BaseProviderWithProjectProvider } func (o *LabelProvider) ToFormat(ctx context.Context, label *Label) *format.Label { diff --git a/services/f3/driver/milestone.go b/services/f3/driver/milestone.go index 30e22f3db0..a86424fe32 100644 --- a/services/f3/driver/milestone.go +++ b/services/f3/driver/milestone.go @@ -119,8 +119,7 @@ func (o *Milestone) FromFormat(milestone *format.Milestone) { } type MilestoneProvider struct { - g *Forgejo - project *ProjectProvider + BaseProviderWithProjectProvider } func (o *MilestoneProvider) ToFormat(ctx context.Context, milestone *Milestone) *format.Milestone { diff --git a/services/f3/driver/project.go b/services/f3/driver/project.go index 3e94e05e0e..23b14a1b96 100644 --- a/services/f3/driver/project.go +++ b/services/f3/driver/project.go @@ -84,7 +84,7 @@ func (o *Project) FromFormat(project *format.Project) { } type ProjectProvider struct { - g *Forgejo + BaseProvider milestones f3_util.NameIDMap labels f3_util.NameIDMap } diff --git a/services/f3/driver/pull_request.go b/services/f3/driver/pull_request.go index 63e040d67b..a617443479 100644 --- a/services/f3/driver/pull_request.go +++ b/services/f3/driver/pull_request.go @@ -210,8 +210,7 @@ func (o *PullRequest) FromFormat(pullRequest *format.PullRequest) { } type PullRequestProvider struct { - g *Forgejo - project *ProjectProvider + BaseProviderWithProjectProvider prHeadCache f3_forgejo.PrHeadCache } diff --git a/services/f3/driver/reaction.go b/services/f3/driver/reaction.go index 75f356a814..87afe58ffc 100644 --- a/services/f3/driver/reaction.go +++ b/services/f3/driver/reaction.go @@ -76,7 +76,7 @@ func (o *Reaction) FromFormat(reaction *format.Reaction) { } type ReactionProvider struct { - g *Forgejo + BaseProvider } func (o *ReactionProvider) ToFormat(ctx context.Context, reaction *Reaction) *format.Reaction { diff --git a/services/f3/driver/release.go b/services/f3/driver/release.go index 1d0f8b370e..a732dcae6c 100644 --- a/services/f3/driver/release.go +++ b/services/f3/driver/release.go @@ -100,7 +100,7 @@ func (o *Release) FromFormat(release *format.Release) { } type ReleaseProvider struct { - g *Forgejo + BaseProvider } func (o *ReleaseProvider) ToFormat(ctx context.Context, release *Release) *format.Release { diff --git a/services/f3/driver/repository.go b/services/f3/driver/repository.go index 6874ea0a8e..c41d4808fa 100644 --- a/services/f3/driver/repository.go +++ b/services/f3/driver/repository.go @@ -35,7 +35,7 @@ func (o *Repository) FromFormat(repository *format.Repository) { } type RepositoryProvider struct { - g *Forgejo + BaseProvider } func (o *RepositoryProvider) ToFormat(ctx context.Context, repository *Repository) *format.Repository { diff --git a/services/f3/driver/review.go b/services/f3/driver/review.go index 1df9346283..76c021698e 100644 --- a/services/f3/driver/review.go +++ b/services/f3/driver/review.go @@ -151,7 +151,7 @@ func (o *Review) FromFormat(review *format.Review) { } type ReviewProvider struct { - g *Forgejo + BaseProvider } func (o *ReviewProvider) ToFormat(ctx context.Context, review *Review) *format.Review { diff --git a/services/f3/driver/topic.go b/services/f3/driver/topic.go index 8b2ffcca36..c31a91f1af 100644 --- a/services/f3/driver/topic.go +++ b/services/f3/driver/topic.go @@ -68,7 +68,7 @@ func (o *Topic) FromFormat(topic *format.Topic) { } type TopicProvider struct { - g *Forgejo + BaseProvider } func (o *TopicProvider) ToFormat(ctx context.Context, topic *Topic) *format.Topic { diff --git a/services/f3/driver/user.go b/services/f3/driver/user.go index 5ab94864ee..0d1a7719e4 100644 --- a/services/f3/driver/user.go +++ b/services/f3/driver/user.go @@ -76,7 +76,7 @@ func (o *User) FromFormat(user *format.User) { } type UserProvider struct { - g *Forgejo + BaseProvider } func (o *UserProvider) ToFormat(ctx context.Context, user *User) *format.User {