go.temporal.io/server@v1.23.0/common/searchattribute/name_type_map.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 package searchattribute 26 27 import ( 28 "fmt" 29 30 enumspb "go.temporal.io/api/enums/v1" 31 32 persistencespb "go.temporal.io/server/api/persistence/v1" 33 ) 34 35 type ( 36 NameTypeMap struct { 37 // customSearchAttributes are defined by cluster admin per cluster level and passed and stored in SearchAttributes object. 38 customSearchAttributes map[string]enumspb.IndexedValueType 39 } 40 41 category int32 42 ) 43 44 const ( 45 systemCategory category = 1 << iota 46 predefinedCategory 47 customCategory 48 ) 49 50 func buildIndexNameTypeMap(indexSearchAttributes map[string]*persistencespb.IndexSearchAttributes) map[string]NameTypeMap { 51 indexNameTypeMap := make(map[string]NameTypeMap, len(indexSearchAttributes)) 52 for indexName, customSearchAttributes := range indexSearchAttributes { 53 indexNameTypeMap[indexName] = NameTypeMap{ 54 customSearchAttributes: customSearchAttributes.GetCustomSearchAttributes(), 55 } 56 } 57 return indexNameTypeMap 58 } 59 60 func (m NameTypeMap) System() map[string]enumspb.IndexedValueType { 61 allSystem := make(map[string]enumspb.IndexedValueType, len(system)+len(predefined)) 62 for saName, saType := range system { 63 allSystem[saName] = saType 64 } 65 for saName, saType := range predefined { 66 allSystem[saName] = saType 67 } 68 return allSystem 69 } 70 71 func (m NameTypeMap) Custom() map[string]enumspb.IndexedValueType { 72 return m.customSearchAttributes 73 } 74 75 func (m NameTypeMap) All() map[string]enumspb.IndexedValueType { 76 allSearchAttributes := make(map[string]enumspb.IndexedValueType, len(system)+len(m.customSearchAttributes)+len(predefined)) 77 for saName, saType := range system { 78 allSearchAttributes[saName] = saType 79 } 80 for saName, saType := range predefined { 81 allSearchAttributes[saName] = saType 82 } 83 for saName, saType := range m.customSearchAttributes { 84 allSearchAttributes[saName] = saType 85 } 86 return allSearchAttributes 87 } 88 89 // GetType returns type of search attribute from type map. 90 func (m NameTypeMap) GetType(name string) (enumspb.IndexedValueType, error) { 91 return m.getType(name, systemCategory|predefinedCategory|customCategory) 92 } 93 94 // GetType returns type of search attribute from type map. 95 func (m NameTypeMap) getType(name string, cat category) (enumspb.IndexedValueType, error) { 96 if cat|customCategory == cat && len(m.customSearchAttributes) != 0 { 97 if t, isCustom := m.customSearchAttributes[name]; isCustom { 98 return t, nil 99 } 100 } 101 if cat|predefinedCategory == cat { 102 if t, isPredefined := predefined[name]; isPredefined { 103 return t, nil 104 } 105 } 106 if cat|systemCategory == cat { 107 if t, isSystem := system[name]; isSystem { 108 return t, nil 109 } 110 } 111 return enumspb.INDEXED_VALUE_TYPE_UNSPECIFIED, fmt.Errorf("%w: %s", ErrInvalidName, name) 112 } 113 114 func (m NameTypeMap) IsDefined(name string) bool { 115 if _, err := m.GetType(name); err == nil { 116 return true 117 } 118 return false 119 }