mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-10-08 03:47:51 +00:00
Allow a single functional option for field options.
Move field type specific validation to functional options.
This commit is contained in:
parent
50794bf63b
commit
028e95d914
7 changed files with 19 additions and 39 deletions
13
api.go
13
api.go
|
|
@ -244,17 +244,18 @@ func (api *API) DeleteIndex(ctx context.Context, indexName string) error {
|
|||
}
|
||||
|
||||
// CreateField makes the named field in the named index with the given options.
|
||||
func (api *API) CreateField(ctx context.Context, indexName string, fieldName string, opts ...FieldOption) (*Field, error) {
|
||||
// This method currently only takes a single functional option, but that may be
|
||||
// changed in the future to support multiple options.
|
||||
func (api *API) CreateField(ctx context.Context, indexName string, fieldName string, opts FieldOption) (*Field, error) {
|
||||
if err := api.validate(apiCreateField); err != nil {
|
||||
return nil, errors.Wrap(err, "validating api method")
|
||||
}
|
||||
|
||||
// Apply functional option.
|
||||
fo := FieldOptions{}
|
||||
for _, opt := range opts {
|
||||
err := opt(&fo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "applying option")
|
||||
}
|
||||
err := opts(&fo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "applying option")
|
||||
}
|
||||
|
||||
// Find index.
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ type ImportCommand struct {
|
|||
|
||||
// Options for index & field to be created if they don't exist
|
||||
IndexOptions pilosa.IndexOptions
|
||||
//FieldOptions pilosa.FieldOptions
|
||||
|
||||
// CreateSchema ensures the schema exists before import
|
||||
CreateSchema bool
|
||||
|
|
|
|||
25
field.go
25
field.go
|
|
@ -90,6 +90,9 @@ func OptFieldTypeInt(min, max int64) FieldOption {
|
|||
if fo.Type != "" {
|
||||
return errors.Errorf("field type is already set to: %s", fo.Type)
|
||||
}
|
||||
if min > max {
|
||||
return ErrInvalidBSIGroupRange
|
||||
}
|
||||
fo.Type = FieldTypeInt
|
||||
fo.Min = min
|
||||
fo.Max = max
|
||||
|
|
@ -102,6 +105,9 @@ func OptFieldTypeTime(timeQuantum TimeQuantum) FieldOption {
|
|||
if fo.Type != "" {
|
||||
return errors.Errorf("field type is already set to: %s", fo.Type)
|
||||
}
|
||||
if !timeQuantum.Valid() {
|
||||
return ErrInvalidTimeQuantum
|
||||
}
|
||||
fo.Type = FieldTypeTime
|
||||
fo.TimeQuantum = timeQuantum
|
||||
return nil
|
||||
|
|
@ -1075,25 +1081,6 @@ func applyDefaultOptions(o FieldOptions) FieldOptions {
|
|||
return o
|
||||
}
|
||||
|
||||
// Validate ensures that FieldOption values are valid.
|
||||
func (o *FieldOptions) Validate() error {
|
||||
switch o.Type {
|
||||
case FieldTypeSet, "":
|
||||
// TODO: cacheType, cacheSize validation
|
||||
case FieldTypeInt:
|
||||
if o.Min > o.Max {
|
||||
return ErrInvalidBSIGroupRange
|
||||
}
|
||||
case FieldTypeTime:
|
||||
if o.TimeQuantum == "" || !o.TimeQuantum.Valid() {
|
||||
return ErrInvalidTimeQuantum
|
||||
}
|
||||
default:
|
||||
return errors.New("invalid field type")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encode converts o into its internal representation.
|
||||
func (o *FieldOptions) Encode() *internal.FieldOptions {
|
||||
return encodeFieldOptions(o)
|
||||
|
|
|
|||
|
|
@ -626,17 +626,17 @@ func (h *Handler) handlePostField(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// Convert json options into functional options.
|
||||
var fos []pilosa.FieldOption
|
||||
var fos pilosa.FieldOption
|
||||
switch req.Options.Type {
|
||||
case pilosa.FieldTypeSet:
|
||||
fos = append(fos, pilosa.OptFieldTypeSet(*req.Options.CacheType, *req.Options.CacheSize))
|
||||
fos = pilosa.OptFieldTypeSet(*req.Options.CacheType, *req.Options.CacheSize)
|
||||
case pilosa.FieldTypeInt:
|
||||
fos = append(fos, pilosa.OptFieldTypeInt(*req.Options.Min, *req.Options.Max))
|
||||
fos = pilosa.OptFieldTypeInt(*req.Options.Min, *req.Options.Max)
|
||||
case pilosa.FieldTypeTime:
|
||||
fos = append(fos, pilosa.OptFieldTypeTime(*req.Options.TimeQuantum))
|
||||
fos = pilosa.OptFieldTypeTime(*req.Options.TimeQuantum)
|
||||
}
|
||||
|
||||
_, err = h.API.CreateField(r.Context(), indexName, fieldName, fos...)
|
||||
_, err = h.API.CreateField(r.Context(), indexName, fieldName, fos)
|
||||
if err != nil {
|
||||
switch errors.Cause(err) {
|
||||
case pilosa.ErrIndexNotFound:
|
||||
|
|
|
|||
|
|
@ -104,8 +104,6 @@ func int64Ptr(i int64) *int64 {
|
|||
|
||||
// Test fieldOption validation.
|
||||
func TestFieldOptionValidation(t *testing.T) {
|
||||
//foo := "foo"
|
||||
//set := "set"
|
||||
timeQuantum := pilosa.TimeQuantum("YMD")
|
||||
defaultCacheSize := uint32(pilosa.DefaultCacheSize)
|
||||
tests := []struct {
|
||||
|
|
|
|||
5
index.go
5
index.go
|
|
@ -301,11 +301,6 @@ func (i *Index) createField(name string, opt FieldOptions) (*Field, error) {
|
|||
return nil, ErrInvalidCacheType
|
||||
}
|
||||
|
||||
// Validate options.
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, errors.Wrap(err, "validating options")
|
||||
}
|
||||
|
||||
// Initialize field.
|
||||
f, err := i.newField(i.FieldPath(name), name)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ func (h *Holder) MustCreateFieldIfNotExists(index, field string) *Field {
|
|||
// MustCreateRankedFragmentIfNotExists returns a given fragment with a ranked cache. Panic on error.
|
||||
func (h *Holder) MustCreateRankedFragmentIfNotExists(index, field, view string, slice uint64) *Fragment {
|
||||
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
|
||||
f, err := idx.CreateFieldIfNotExists(field, pilosa.FieldOptions{CacheType: pilosa.CacheTypeRanked})
|
||||
f, err := idx.CreateFieldIfNotExists(field, pilosa.FieldOptions{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue