From 0d926c3c75d9f0850cd1d499de2aa214fd494689 Mon Sep 17 00:00:00 2001 From: Wenxuan Zhang Date: Mon, 15 Nov 2021 14:55:45 +0800 Subject: [PATCH] feat: implement `Copy` for `XXXList` --- .../database/metadatav2/informer/group.go | 2 +- .../database/metadatav2/informer/informer.go | 40 ++----------------- pkg/oomstore/typesv2/entity.go | 11 +++++ pkg/oomstore/typesv2/feature.go | 11 +++++ pkg/oomstore/typesv2/feature_group.go | 11 +++++ pkg/oomstore/typesv2/revision.go | 11 +++++ 6 files changed, 49 insertions(+), 37 deletions(-) diff --git a/internal/database/metadatav2/informer/group.go b/internal/database/metadatav2/informer/group.go index 8fd0f9f77..952811064 100644 --- a/internal/database/metadatav2/informer/group.go +++ b/internal/database/metadatav2/informer/group.go @@ -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 } diff --git a/internal/database/metadatav2/informer/informer.go b/internal/database/metadatav2/informer/informer.go index 724ff2e61..754553acf 100644 --- a/internal/database/metadatav2/informer/informer.go +++ b/internal/database/metadatav2/informer/informer.go @@ -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() } diff --git a/pkg/oomstore/typesv2/entity.go b/pkg/oomstore/typesv2/entity.go index 848cb0fbf..6ffe8661e 100644 --- a/pkg/oomstore/typesv2/entity.go +++ b/pkg/oomstore/typesv2/entity.go @@ -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) { diff --git a/pkg/oomstore/typesv2/feature.go b/pkg/oomstore/typesv2/feature.go index 3768d6f87..49b6732bb 100644 --- a/pkg/oomstore/typesv2/feature.go +++ b/pkg/oomstore/typesv2/feature.go @@ -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) { diff --git a/pkg/oomstore/typesv2/feature_group.go b/pkg/oomstore/typesv2/feature_group.go index 24e64974e..9b420eafa 100644 --- a/pkg/oomstore/typesv2/feature_group.go +++ b/pkg/oomstore/typesv2/feature_group.go @@ -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) { diff --git a/pkg/oomstore/typesv2/revision.go b/pkg/oomstore/typesv2/revision.go index 6d8a17c8e..6e9ed3810 100644 --- a/pkg/oomstore/typesv2/revision.go +++ b/pkg/oomstore/typesv2/revision.go @@ -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) {