Skip to content

Commit

Permalink
feat(bleve): add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Jun 30, 2022
1 parent 68dc9f4 commit 8f4b5b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/storage/search/internal/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
"github.com/blevesearch/bleve/v2/registry"
"github.com/iyear/searchx/pkg/utils"
"github.com/iyear/searchx/pkg/validator"
"github.com/mitchellh/mapstructure"
"path"
)
Expand All @@ -16,8 +17,8 @@ type Bleve struct {
}

type Options struct {
Path string `mapstructure:"path"`
Dict string `mapstructure:"dict"`
Path string `mapstructure:"path" validate:"required"`
Dict string `mapstructure:"dict" validate:"required"`
}

const (
Expand All @@ -34,6 +35,10 @@ func New(options map[string]interface{}) (*Bleve, error) {
return nil, err
}

if err := validator.Struct(&ops); err != nil {
return nil, err
}

mapping := bleve.NewIndexMapping()
err := mapping.AddCustomTokenizer(jieba, map[string]interface{}{
"file": ops.Dict,
Expand Down
13 changes: 13 additions & 0 deletions pkg/validator/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package validator

import "github.com/go-playground/validator/v10"

var v *validator.Validate

func init() {
v = validator.New()
}

func Struct(s interface{}) error {
return v.Struct(s)
}

0 comments on commit 8f4b5b9

Please sign in to comment.