Skip to content

Commit

Permalink
fix: Use paradigms to reduce logic duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
penqee committed Dec 10, 2024
1 parent a85a821 commit f727b70
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
19 changes: 3 additions & 16 deletions api/pack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pack
import (
api "github.com/west2-online/fzuhelper-server/api/model/model"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
"github.com/west2-online/fzuhelper-server/pkg/base"
)

func BuildTermList(termList *model.TermList) *api.TermList {
Expand All @@ -29,14 +30,7 @@ func BuildTermList(termList *model.TermList) *api.TermList {
}

func BuildTerms(termList []*model.Term) []*api.Term {
if len(termList) == 0 {
return nil
}
terms := make([]*api.Term, len(termList))
for i, term := range termList {
terms[i] = BuildTerm(term)
}
return terms
return base.BuildTypeList(termList, BuildTerm)
}

func BuildTerm(term *model.Term) *api.Term {
Expand Down Expand Up @@ -67,12 +61,5 @@ func BuildTermEvent(termEvent *model.TermEvent) *api.TermEvent {
}

func BuildTermEvents(termEvents []*model.TermEvent) []*api.TermEvent {
if len(termEvents) == 0 {
return nil
}
events := make([]*api.TermEvent, len(termEvents))
for i, termEvent := range termEvents {
events[i] = BuildTermEvent(termEvent)
}
return events
return base.BuildTypeList(termEvents, BuildTermEvent)
}
20 changes: 3 additions & 17 deletions internal/common/pack/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package pack

import (
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/jwch"
)

Expand Down Expand Up @@ -48,14 +49,7 @@ func BuildTerm(term jwch.CalTerm) *model.Term {
}

func BuildTerms(terms []jwch.CalTerm) []*model.Term {
if len(terms) == 0 {
return nil
}
termList := make([]*model.Term, len(terms))
for i, term := range terms {
termList[i] = BuildTerm(term)
}
return termList
return base.BuildTypeList(terms, BuildTerm)
}

func BuildTermEvent(term jwch.CalTermEvent) *model.TermEvent {
Expand All @@ -67,13 +61,5 @@ func BuildTermEvent(term jwch.CalTermEvent) *model.TermEvent {
}

func BuildTermEvents(events []jwch.CalTermEvent) []*model.TermEvent {
if len(events) == 0 {
return nil
}

termEvents := make([]*model.TermEvent, len(events))
for i, event := range events {
termEvents[i] = BuildTermEvent(event)
}
return termEvents
return base.BuildTypeList(events, BuildTermEvent)
}
12 changes: 12 additions & 0 deletions pkg/base/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,15 @@ func HandleJwchError(err error) error {
}
return err
}

func BuildTypeList[T any, U any](items []U, buildFunc func(U) T) []T {
if len(items) == 0 {
return nil
}

Check warning on line 93 in pkg/base/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/base/pack.go#L90-L93

Added lines #L90 - L93 were not covered by tests

list := make([]T, len(items))
for i, item := range items {
list[i] = buildFunc(item)
}
return list

Check warning on line 99 in pkg/base/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/base/pack.go#L95-L99

Added lines #L95 - L99 were not covered by tests
}

0 comments on commit f727b70

Please sign in to comment.