Skip to content

Commit

Permalink
fix(oomstore/apply): add validate
Browse files Browse the repository at this point in the history
  • Loading branch information
lianxmfor committed Nov 22, 2021
1 parent f0f0b8d commit 2cbb000
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/oomstore/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 30 additions & 1 deletion pkg/oomstore/types/apply/apply.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package apply

import "io"
import (
"fmt"
"io"

"github.com/oom-ai/oomstore/pkg/errdefs"
)

type ApplyOpt struct {
R io.Reader
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
Expand All @@ -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
}

0 comments on commit 2cbb000

Please sign in to comment.