github.com/weaviate/weaviate@v1.24.6/modules/img2vec-neural/vectorizer/class_settings.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package vectorizer
    13  
    14  import (
    15  	"errors"
    16  
    17  	"github.com/weaviate/weaviate/entities/moduletools"
    18  )
    19  
    20  type classSettings struct {
    21  	cfg moduletools.ClassConfig
    22  }
    23  
    24  func NewClassSettings(cfg moduletools.ClassConfig) *classSettings {
    25  	return &classSettings{cfg: cfg}
    26  }
    27  
    28  func (ic *classSettings) ImageField(property string) bool {
    29  	if ic.cfg == nil {
    30  		// we would receive a nil-config on cross-class requests, such as Explore{}
    31  		return false
    32  	}
    33  
    34  	imageFields, ok := ic.cfg.Class()["imageFields"]
    35  	if !ok {
    36  		return false
    37  	}
    38  
    39  	imageFieldsArray, ok := imageFields.([]interface{})
    40  	if !ok {
    41  		return false
    42  	}
    43  
    44  	fieldNames := make([]string, len(imageFieldsArray))
    45  	for i, value := range imageFieldsArray {
    46  		fieldNames[i] = value.(string)
    47  	}
    48  
    49  	for i := range fieldNames {
    50  		if fieldNames[i] == property {
    51  			return true
    52  		}
    53  	}
    54  
    55  	return false
    56  }
    57  
    58  func (ic *classSettings) Validate() error {
    59  	if ic.cfg == nil {
    60  		// we would receive a nil-config on cross-class requests, such as Explore{}
    61  		return errors.New("empty config")
    62  	}
    63  
    64  	imageFields, ok := ic.cfg.Class()["imageFields"]
    65  	if !ok {
    66  		return errors.New("imageFields not present")
    67  	}
    68  
    69  	imageFieldsArray, ok := imageFields.([]interface{})
    70  	if !ok {
    71  		return errors.New("imageFields must be an array")
    72  	}
    73  
    74  	if len(imageFieldsArray) == 0 {
    75  		return errors.New("must contain at least one image field name in imageFields")
    76  	}
    77  
    78  	for _, value := range imageFieldsArray {
    79  		v, ok := value.(string)
    80  		if !ok {
    81  			return errors.New("imageField must be a string")
    82  		}
    83  		if len(v) == 0 {
    84  			return errors.New("imageField values cannot be empty")
    85  		}
    86  	}
    87  
    88  	return nil
    89  }