From 2cbb000b2928a07eab904e53c889e1631f48e795 Mon Sep 17 00:00:00 2001 From: lianxmfor <1034552569@qq.com> Date: Mon, 22 Nov 2021 19:52:47 +0800 Subject: [PATCH] fix(oomstore/apply): add validate --- pkg/oomstore/apply.go | 12 ++++++++++++ pkg/oomstore/types/apply/apply.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pkg/oomstore/apply.go b/pkg/oomstore/apply.go index d89292c60..ece941f69 100644 --- a/pkg/oomstore/apply.go +++ b/pkg/oomstore/apply.go @@ -71,6 +71,10 @@ func (s *OomStore) Apply(ctx context.Context, opt apply.ApplyOpt) error { } func (s *OomStore) applyEntity(ctx context.Context, txStore metadata.WriteStore, newEntity apply.Entity) (int, error) { + if err := newEntity.Validate(); err != nil { + return 0, err + } + entity, err := s.metadata.GetEntityByName(ctx, newEntity.Name) if err != nil { if !errdefs.IsNotFound(err) { @@ -99,6 +103,10 @@ func (s *OomStore) applyEntity(ctx context.Context, txStore metadata.WriteStore, } func (s *OomStore) applyGroup(ctx context.Context, txStore metadata.WriteStore, newGroup apply.Group) (int, error) { + if err := newGroup.Validate(); err != nil { + return 0, err + } + group, err := s.metadata.GetGroupByName(ctx, newGroup.Name) if err != nil { if !errdefs.IsNotFound(err) { @@ -126,6 +134,10 @@ func (s *OomStore) applyGroup(ctx context.Context, txStore metadata.WriteStore, } func (s *OomStore) applyFeature(ctx context.Context, txStore metadata.WriteStore, newFeature apply.Feature) error { + if err := newFeature.Validate(); err != nil { + return err + } + feature, err := s.metadata.GetFeatureByName(ctx, newFeature.Name) if err != nil { if !errdefs.IsNotFound(err) { diff --git a/pkg/oomstore/types/apply/apply.go b/pkg/oomstore/types/apply/apply.go index f3004d52d..0b80c17c1 100644 --- a/pkg/oomstore/types/apply/apply.go +++ b/pkg/oomstore/types/apply/apply.go @@ -1,6 +1,11 @@ package apply -import "io" +import ( + "fmt" + "io" + + "github.com/oom-ai/oomstore/pkg/errdefs" +) type ApplyOpt struct { R io.Reader @@ -29,6 +34,16 @@ type Feature struct { Description string `mapstructure:"description"` } +func (f *Feature) Validate() error { + if f.Name == "" { + return errdefs.InvalidAttribute(fmt.Errorf("the name of feature should not be empty")) + } + if f.DBValueType == "" { + return errdefs.InvalidAttribute(fmt.Errorf("the db value type of feature should not be empty")) + } + return nil +} + type Group struct { Kind string `mapstructure:"kind"` Group string `mapstructure:"group"` @@ -40,6 +55,13 @@ type Group struct { Features []Feature `mapstructure:"features"` } +func (g *Group) Validate() error { + if g.Name == "" { + return errdefs.InvalidAttribute(fmt.Errorf("the name of group should not be empty")) + } + return nil +} + type Entity struct { Kind string `mapstructure:"kind"` Name string `mapstructure:"name"` @@ -49,3 +71,10 @@ type Entity struct { BatchFeatures []Group `mapstructure:"batch-features"` StreamFeatures []Feature `mapstructure:"stream-features"` } + +func (e *Entity) Validate() error { + if e.Name == "" { + return errdefs.InvalidAttribute(fmt.Errorf("the name of entity should not be empty")) + } + return nil +}