apply fieldName validation to index.CreateField

This commit is contained in:
Travis Turner 2018-09-10 11:49:40 -05:00
parent 3d7fc0c1c6
commit 8b99414029
No known key found for this signature in database
GPG key ID: 7F08008DFD9314C9
2 changed files with 18 additions and 3 deletions

View file

@ -159,12 +159,17 @@ func OptFieldTypeMutex(cacheType string, cacheSize uint32) FieldOption {
func NewField(path, index, name string, opts FieldOption) (*Field, error) {
err := validateName(name)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "validating name")
}
return newField(path, index, name, opts)
}
// newField returns a new instance of field (without name validation).
func newField(path, index, name string, opts FieldOption) (*Field, error) {
// Apply functional option.
fo := FieldOptions{}
err = opts(&fo)
err := opts(&fo)
if err != nil {
return nil, errors.Wrap(err, "applying option")
}

View file

@ -289,6 +289,11 @@ func (i *Index) recalculateCaches() {
// CreateField creates a field.
func (i *Index) CreateField(name string, opts ...FieldOption) (*Field, error) {
err := validateName(name)
if err != nil {
return nil, errors.Wrap(err, "validating name")
}
i.mu.Lock()
defer i.mu.Unlock()
@ -311,6 +316,11 @@ func (i *Index) CreateField(name string, opts ...FieldOption) (*Field, error) {
// CreateFieldIfNotExists creates a field with the given options if it doesn't exist.
func (i *Index) CreateFieldIfNotExists(name string, opts ...FieldOption) (*Field, error) {
err := validateName(name)
if err != nil {
return nil, errors.Wrap(err, "validating name")
}
i.mu.Lock()
defer i.mu.Unlock()
@ -379,7 +389,7 @@ func (i *Index) createField(name string, opt FieldOptions) (*Field, error) {
}
func (i *Index) newField(path, name string) (*Field, error) {
f, err := NewField(path, i.name, name, OptFieldTypeDefault())
f, err := newField(path, i.name, name, OptFieldTypeDefault())
if err != nil {
return nil, err
}