go.temporal.io/server@v1.23.0/common/searchattribute/search_attirbute.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 //go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination search_attribute_mock.go 26 27 package searchattribute 28 29 import ( 30 "context" 31 "errors" 32 "fmt" 33 34 commonpb "go.temporal.io/api/common/v1" 35 enumspb "go.temporal.io/api/enums/v1" 36 ) 37 38 const ( 39 MetadataType = "type" 40 ) 41 42 type ( 43 Provider interface { 44 GetSearchAttributes(indexName string, forceRefreshCache bool) (NameTypeMap, error) 45 } 46 47 Manager interface { 48 Provider 49 SaveSearchAttributes(ctx context.Context, indexName string, newCustomSearchAttributes map[string]enumspb.IndexedValueType) error 50 } 51 ) 52 53 var ( 54 ErrInvalidName = errors.New("invalid search attribute name") 55 ErrInvalidType = errors.New("invalid search attribute type") 56 ) 57 58 // ApplyTypeMap set type for all valid search attributes which don't have it. 59 // It doesn't do any validation and just skip invalid or already set search attributes. 60 func ApplyTypeMap(searchAttributes *commonpb.SearchAttributes, typeMap NameTypeMap) { 61 for saName, saPayload := range searchAttributes.GetIndexedFields() { 62 _, metadataHasValueType := saPayload.Metadata[MetadataType] 63 if metadataHasValueType { 64 continue 65 } 66 valueType, err := typeMap.getType(saName, customCategory|predefinedCategory) 67 if err != nil { 68 continue 69 } 70 setMetadataType(saPayload, valueType) 71 } 72 } 73 74 func setMetadataType(p *commonpb.Payload, t enumspb.IndexedValueType) { 75 if t == enumspb.INDEXED_VALUE_TYPE_UNSPECIFIED { 76 return 77 } 78 79 _, isValidT := enumspb.IndexedValueType_name[int32(t)] 80 if !isValidT { 81 panic(fmt.Sprintf("unknown index value type %v", t)) 82 } 83 p.Metadata[MetadataType] = []byte(enumspb.IndexedValueType(t).String()) 84 } 85 86 // This may mutate saPtr and *saPtr 87 func AddSearchAttribute(saPtr **commonpb.SearchAttributes, key string, value *commonpb.Payload) { 88 if *saPtr == nil { 89 *saPtr = &commonpb.SearchAttributes{} 90 } 91 if (*saPtr).IndexedFields == nil { 92 (*saPtr).IndexedFields = make(map[string]*commonpb.Payload) 93 } 94 (*saPtr).IndexedFields[key] = value 95 }