Skip to content

Commit

Permalink
fix: informer get api should return a deep copy
Browse files Browse the repository at this point in the history
  • Loading branch information
wfxr committed Nov 15, 2021
1 parent 6886da2 commit d876a65
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 8 additions & 8 deletions internal/database/metadatav2/informer/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/oomstore/typesv2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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 @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"time"

"github.com/jinzhu/copier"
)

type Feature struct {
Expand All @@ -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
}
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 @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"time"

"github.com/jinzhu/copier"
)

type FeatureGroup struct {
Expand All @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion pkg/oomstore/typesv2/revision.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package typesv2

import "time"
import (
"time"

"github.com/jinzhu/copier"
)

type Revision struct {
ID int32 `db:"id"`
Expand All @@ -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 {
Expand Down

0 comments on commit d876a65

Please sign in to comment.