diff --git a/go.mod b/go.mod index d5395c922..16602c13f 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/uuid v1.3.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/jinzhu/copier v0.3.2 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect diff --git a/go.sum b/go.sum index f916d6273..06986544b 100644 --- a/go.sum +++ b/go.sum @@ -517,6 +517,8 @@ github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf/go.mod h1 github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA= github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451 h1:WAvSpGf7MsFuzAtK4Vk7R4EVe+liW4x83r4oWu0WHKw= github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds= +github.com/jinzhu/copier v0.3.2 h1:QdBOCbaouLDYaIPFfi1bKv5F5tPpeTwXe4sD0jqtz5w= +github.com/jinzhu/copier v0.3.2/go.mod h1:24xnZezI2Yqac9J61UC6/dG/k76ttpq0DdJI3QmUvro= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= diff --git a/internal/database/metadatav2/informer/informer.go b/internal/database/metadatav2/informer/informer.go index 4f39e239f..bc6fd527e 100644 --- a/internal/database/metadatav2/informer/informer.go +++ b/internal/database/metadatav2/informer/informer.go @@ -99,7 +99,7 @@ func (f *Informer) GetEntity(ctx context.Context, id int16) (*typesv2.Entity, er }); entity == nil { return nil, fmt.Errorf("feature entity %d not found", id) } else { - return entity, nil + return entity.Copy(), nil } } @@ -109,7 +109,7 @@ func (f *Informer) GetEntityByName(ctx context.Context, name string) (*typesv2.E }); entity == nil { return nil, fmt.Errorf("feature entity '%s' not found", name) } else { - return entity, nil + return entity.Copy(), nil } } @@ -119,7 +119,7 @@ func (f *Informer) GetFeature(ctx context.Context, id int16) (*typesv2.Feature, }); feature == nil { return nil, fmt.Errorf("feature %d not found", id) } else { - return feature, nil + return feature.Copy(), nil } } @@ -129,7 +129,7 @@ func (f *Informer) GetFeatureByName(ctx context.Context, name string) (*typesv2. }); feature == nil { return nil, fmt.Errorf("feature '%s' not found", name) } else { - return feature, nil + return feature.Copy(), nil } } @@ -139,7 +139,7 @@ func (f *Informer) GetFeatureGroup(ctx context.Context, id int16) (*typesv2.Feat }); featureGroup == nil { return nil, fmt.Errorf("feature group %d not found", id) } else { - return featureGroup, nil + return featureGroup.Copy(), nil } } @@ -149,7 +149,7 @@ func (f *Informer) GetFeatureGroupByName(ctx context.Context, name string) (*typ }); featureGroup == nil { return nil, fmt.Errorf("feature group '%s' not found", name) } else { - return featureGroup, nil + return featureGroup.Copy(), nil } } @@ -159,7 +159,7 @@ func (f *Informer) GetRevision(ctx context.Context, id int32) (*typesv2.Revision }); revision == nil { return nil, fmt.Errorf("revision not found") } else { - return revision, nil + return revision.Copy(), nil } } @@ -169,7 +169,7 @@ func (f *Informer) GetRevisionBy(ctx context.Context, groupID int16, revision in }); revision == nil { return nil, fmt.Errorf("revision not found") } else { - return revision, nil + return revision.Copy(), nil } } diff --git a/pkg/oomstore/typesv2/entity.go b/pkg/oomstore/typesv2/entity.go index 0fa32eb00..848cb0fbf 100644 --- a/pkg/oomstore/typesv2/entity.go +++ b/pkg/oomstore/typesv2/entity.go @@ -16,6 +16,14 @@ type Entity struct { ModifyTime time.Time `db:"modify_time"` } +func (e *Entity) Copy() *Entity { + if e == nil { + return nil + } + copied := *e + return &copied +} + type EntityList []*Entity func (l *EntityList) Find(find func(*Entity) bool) *Entity { diff --git a/pkg/oomstore/typesv2/feature.go b/pkg/oomstore/typesv2/feature.go index 6de607d61..f21b00217 100644 --- a/pkg/oomstore/typesv2/feature.go +++ b/pkg/oomstore/typesv2/feature.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "time" + + "github.com/jinzhu/copier" ) type Feature struct { @@ -20,6 +22,15 @@ type Feature struct { Group *FeatureGroup } +func (f *Feature) Copy() *Feature { + if f == nil { + return nil + } + var copied Feature + copier.Copy(f, copied) + return &copied +} + func (f *Feature) Entity() *Entity { return f.Group.Entity } diff --git a/pkg/oomstore/typesv2/feature_group.go b/pkg/oomstore/typesv2/feature_group.go index d47876375..15d241f24 100644 --- a/pkg/oomstore/typesv2/feature_group.go +++ b/pkg/oomstore/typesv2/feature_group.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "time" + + "github.com/jinzhu/copier" ) type FeatureGroup struct { @@ -22,6 +24,15 @@ type FeatureGroup struct { OnlineRevision *Revision } +func (fg *FeatureGroup) Copy() *FeatureGroup { + if fg == nil { + return nil + } + var copied FeatureGroup + copier.Copy(fg, copied) + return &copied +} + type FeatureGroupList []*FeatureGroup func (l *FeatureGroupList) Find(find func(*FeatureGroup) bool) *FeatureGroup { diff --git a/pkg/oomstore/typesv2/revision.go b/pkg/oomstore/typesv2/revision.go index 0924ed389..6ec03ef45 100644 --- a/pkg/oomstore/typesv2/revision.go +++ b/pkg/oomstore/typesv2/revision.go @@ -1,6 +1,10 @@ package typesv2 -import "time" +import ( + "time" + + "github.com/jinzhu/copier" +) type Revision struct { ID int32 `db:"id"` @@ -16,6 +20,15 @@ type Revision struct { Group *FeatureGroup } +func (r *Revision) Copy() *Revision { + if r == nil { + return nil + } + var copied Revision + copier.Copy(r, copied) + return &copied +} + type RevisionList []*Revision func (l *RevisionList) Find(find func(*Revision) bool) *Revision {