github.com/weaviate/weaviate@v1.24.6/entities/search/select_property.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 search 13 14 import ( 15 "fmt" 16 "regexp" 17 18 "github.com/weaviate/weaviate/entities/additional" 19 "github.com/weaviate/weaviate/entities/schema" 20 ) 21 22 type SelectProperty struct { 23 Name string `json:"name"` 24 25 IsPrimitive bool `json:"isPrimitive"` 26 27 IsObject bool `json:"isObject"` 28 29 // Include the __typename in all the Refs below. 30 IncludeTypeName bool `json:"includeTypeName"` 31 32 // Not a primitive nor nested type? Then select these properties. 33 Refs []SelectClass `json:"refs"` 34 35 // Nested type? Then select these properties. 36 Props []SelectProperty `json:"objs"` 37 } 38 39 type SelectClass struct { 40 ClassName string `json:"className"` 41 RefProperties SelectProperties `json:"refProperties"` 42 AdditionalProperties additional.Properties `json:"additionalProperties"` 43 } 44 45 // FindSelectClass by specifying the exact class name 46 func (sp SelectProperty) FindSelectClass(className schema.ClassName) *SelectClass { 47 for _, selectClass := range sp.Refs { 48 if selectClass.ClassName == string(className) { 49 return &selectClass 50 } 51 } 52 53 return nil 54 } 55 56 // FindSelectObject by specifying the exact object name 57 func (sp SelectProperty) FindSelectProperty(name string) *SelectProperty { 58 for _, selectProp := range sp.Props { 59 if selectProp.Name == name { 60 return &selectProp 61 } 62 } 63 64 return nil 65 } 66 67 // HasPeer returns true if any of the referenced classes are from the specified 68 // peer 69 func (sp SelectProperty) HasPeer(peerName string) bool { 70 r := regexp.MustCompile(fmt.Sprintf("^%s__", peerName)) 71 for _, selectClass := range sp.Refs { 72 if r.MatchString(selectClass.ClassName) { 73 return true 74 } 75 } 76 77 return false 78 } 79 80 type SelectProperties []SelectProperty 81 82 func (sp SelectProperties) HasRefs() bool { 83 for _, p := range sp { 84 if len(p.Refs) > 0 { 85 return true 86 } 87 } 88 return false 89 } 90 91 func (sp SelectProperties) HasProps() bool { 92 for _, p := range sp { 93 if len(p.Props) > 0 { 94 return true 95 } 96 } 97 return false 98 } 99 100 func (sp SelectProperties) ShouldResolve(path []string) (bool, error) { 101 if len(path)%2 != 0 || len(path) == 0 { 102 return false, fmt.Errorf("used incorrectly: path must have even number of segments in the form of " + 103 "refProp, className, refProp, className, etc.") 104 } 105 106 // the above gives us the guarantee that path contains at least two elements 107 property := path[0] 108 class := schema.ClassName(path[1]) 109 110 for _, p := range sp { 111 if p.IsPrimitive { 112 continue 113 } 114 115 if p.Name != property { 116 continue 117 } 118 119 selectClass := p.FindSelectClass(class) 120 if selectClass == nil { 121 continue 122 } 123 124 if len(path) > 2 { 125 // we're not done yet, this one's nested 126 return selectClass.RefProperties.ShouldResolve(path[2:]) 127 } 128 129 // we are done and found the path 130 return true, nil 131 } 132 133 return false, nil 134 } 135 136 func (sp SelectProperties) FindProperty(propName string) *SelectProperty { 137 for _, prop := range sp { 138 if prop.Name == propName { 139 return &prop 140 } 141 } 142 143 return nil 144 }