Skip to content

Commit

Permalink
feat: implement Copy for XXXList
Browse files Browse the repository at this point in the history
  • Loading branch information
wfxr committed Nov 15, 2021
1 parent e99e519 commit 0d926c3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/database/metadatav2/informer/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *GroupCache) Enrich(entityCache *EntityCache) {
}
}

func (c *GroupCache) List(entityID *int16) []*typesv2.FeatureGroup {
func (c *GroupCache) List(entityID *int16) typesv2.FeatureGroupList {
if entityID == nil {
return c.FeatureGroupList
}
Expand Down
40 changes: 4 additions & 36 deletions internal/database/metadatav2/informer/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,49 +175,17 @@ func (f *Informer) GetRevisionBy(ctx context.Context, groupID int16, revision in

// List
func (f *Informer) ListEntity(ctx context.Context) typesv2.EntityList {
list := f.Cache().Entities.List()
if len(list) == 0 {
return nil
}
copied := make(typesv2.EntityList, 0, len(list))
for _, x := range list {
copied = append(copied, x.Copy())
}
return copied
return f.Cache().Entities.List().Copy()
}

func (f *Informer) ListFeature(ctx context.Context, opt metadatav2.ListFeatureOpt) typesv2.FeatureList {
list := f.Cache().Features.List(opt)
if len(list) == 0 {
return nil
}
copied := make(typesv2.FeatureList, 0, len(list))
for _, x := range list {
copied = append(copied, x.Copy())
}
return copied
return f.Cache().Features.List(opt).Copy()
}

func (f *Informer) ListFeatureGroup(ctx context.Context, entityID *int16) typesv2.FeatureGroupList {
list := f.Cache().Groups.List(entityID)
if len(list) == 0 {
return nil
}
copied := make(typesv2.FeatureGroupList, 0, len(list))
for _, x := range list {
copied = append(copied, x.Copy())
}
return copied
return f.Cache().Groups.List(entityID).Copy()
}

func (f *Informer) ListRevision(ctx context.Context, opt metadatav2.ListRevisionOpt) typesv2.RevisionList {
list := f.Cache().Revisions.List(opt)
if len(list) == 0 {
return nil
}
copied := make(typesv2.RevisionList, 0, len(list))
for _, x := range list {
copied = append(copied, x.Copy())
}
return copied
return f.Cache().Revisions.List(opt).Copy()
}
11 changes: 11 additions & 0 deletions pkg/oomstore/typesv2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func (e *Entity) Copy() *Entity {

type EntityList []*Entity

func (l EntityList) Copy() EntityList {
if len(l) == 0 {
return nil
}
copied := make(EntityList, 0, len(l))
for _, x := range l {
copied = append(copied, x.Copy())
}
return copied
}

func (l *EntityList) Find(find func(*Entity) bool) *Entity {
for _, e := range *l {
if find(e) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/oomstore/typesv2/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ func (f *Feature) OnlineRevisionID() *int32 {

type FeatureList []*Feature

func (l FeatureList) Copy() FeatureList {
if len(l) == 0 {
return nil
}
copied := make(FeatureList, 0, len(l))
for _, x := range l {
copied = append(copied, x.Copy())
}
return copied
}

func (l *FeatureList) Len() int { return len(*l) }

func (l *FeatureList) Names() (names []string) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/oomstore/typesv2/feature_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func (fg *FeatureGroup) Copy() *FeatureGroup {

type FeatureGroupList []*FeatureGroup

func (l FeatureGroupList) Copy() FeatureGroupList {
if len(l) == 0 {
return nil
}
copied := make(FeatureGroupList, 0, len(l))
for _, x := range l {
copied = append(copied, x.Copy())
}
return copied
}

func (l *FeatureGroupList) Find(find func(*FeatureGroup) bool) *FeatureGroup {
for _, g := range *l {
if find(g) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/oomstore/typesv2/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func (r *Revision) Copy() *Revision {

type RevisionList []*Revision

func (l RevisionList) Copy() RevisionList {
if len(l) == 0 {
return nil
}
copied := make(RevisionList, 0, len(l))
for _, x := range l {
copied = append(copied, x.Copy())
}
return copied
}

func (l *RevisionList) Find(find func(*Revision) bool) *Revision {
for _, r := range *l {
if find(r) {
Expand Down

0 comments on commit 0d926c3

Please sign in to comment.