github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/settings/base_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 settings 13 14 import ( 15 "errors" 16 "fmt" 17 18 "github.com/weaviate/weaviate/entities/moduletools" 19 ) 20 21 const ( 22 DefaultPropertyIndexed = true 23 DefaultVectorizeClassName = true 24 DefaultVectorizePropertyName = false 25 ) 26 27 type BaseClassSettings struct { 28 cfg moduletools.ClassConfig 29 propertyHelper *classPropertyValuesHelper 30 } 31 32 func NewBaseClassSettings(cfg moduletools.ClassConfig) *BaseClassSettings { 33 return &BaseClassSettings{cfg: cfg, propertyHelper: &classPropertyValuesHelper{}} 34 } 35 36 func (s *BaseClassSettings) PropertyIndexed(propName string) bool { 37 if s.cfg == nil { 38 return DefaultPropertyIndexed 39 } 40 41 if len(s.Properties()) > 0 { 42 return s.isPropertyIndexed(propName) 43 } 44 45 vcn, ok := s.cfg.Property(propName)["skip"] 46 if !ok { 47 return DefaultPropertyIndexed 48 } 49 50 asBool, ok := vcn.(bool) 51 if !ok { 52 return DefaultPropertyIndexed 53 } 54 55 return !asBool 56 } 57 58 func (s *BaseClassSettings) VectorizePropertyName(propName string) bool { 59 if s.cfg == nil { 60 return DefaultVectorizePropertyName 61 } 62 63 vcn, ok := s.cfg.Property(propName)["vectorizePropertyName"] 64 if !ok { 65 return DefaultVectorizePropertyName 66 } 67 68 asBool, ok := vcn.(bool) 69 if !ok { 70 return DefaultVectorizePropertyName 71 } 72 73 return asBool 74 } 75 76 func (s *BaseClassSettings) VectorizeClassName() bool { 77 if s.cfg == nil { 78 return DefaultVectorizeClassName 79 } 80 81 vcn, ok := s.cfg.Class()["vectorizeClassName"] 82 if !ok { 83 return DefaultVectorizeClassName 84 } 85 86 asBool, ok := vcn.(bool) 87 if !ok { 88 return DefaultVectorizeClassName 89 } 90 91 return asBool 92 } 93 94 func (s *BaseClassSettings) Properties() []string { 95 if s.cfg == nil || len(s.cfg.Class()) == 0 { 96 return nil 97 } 98 99 field, ok := s.cfg.Class()["properties"] 100 if !ok { 101 return nil 102 } 103 104 asArray, ok := field.([]interface{}) 105 if ok { 106 asStringArray := make([]string, len(asArray)) 107 for i := range asArray { 108 asStringArray[i] = asArray[i].(string) 109 } 110 return asStringArray 111 } 112 113 asStringArray, ok := field.([]string) 114 if ok { 115 return asStringArray 116 } 117 118 return nil 119 } 120 121 func (s *BaseClassSettings) Validate() error { 122 if s.cfg != nil && len(s.cfg.Class()) > 0 { 123 if field, ok := s.cfg.Class()["properties"]; ok { 124 fieldsArray, fieldsArrayOk := field.([]interface{}) 125 if fieldsArrayOk { 126 if len(fieldsArray) == 0 { 127 return errors.New("properties field needs to have at least 1 property defined") 128 } 129 for _, value := range fieldsArray { 130 _, ok := value.(string) 131 if !ok { 132 return fmt.Errorf("properties field value: %v must be a string", value) 133 } 134 } 135 } 136 stringArray, stringArrayOk := field.([]string) 137 if stringArrayOk && len(stringArray) == 0 { 138 return errors.New("properties field needs to have at least 1 property defined") 139 } 140 if !fieldsArrayOk && !stringArrayOk { 141 return fmt.Errorf("properties field needs to be of array type, got: %T", field) 142 } 143 } 144 } 145 return nil 146 } 147 148 func (s *BaseClassSettings) isPropertyIndexed(propName string) bool { 149 for _, name := range s.Properties() { 150 if propName == name { 151 return true 152 } 153 } 154 return false 155 } 156 157 func (s *BaseClassSettings) GetPropertyAsInt64(name string, defaultValue *int64) *int64 { 158 return s.propertyHelper.GetPropertyAsInt64(s.cfg, name, defaultValue) 159 } 160 161 func (s *BaseClassSettings) GetPropertyAsString(name, defaultValue string) string { 162 return s.propertyHelper.GetPropertyAsString(s.cfg, name, defaultValue) 163 } 164 165 func (s *BaseClassSettings) GetPropertyAsBool(name string, defaultValue bool) bool { 166 return s.propertyHelper.GetPropertyAsBool(s.cfg, name, defaultValue) 167 } 168 169 func (s *BaseClassSettings) GetNumber(in interface{}) (float32, error) { 170 return s.propertyHelper.GetNumber(in) 171 }