go.temporal.io/server/common/searchattribute/stringify.go
210 LOC · 135 covered · 75 uncovered · 47 ranges · 64 concepts · 33 introducers · 36 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package searchattribute
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/server/common/searchattribute/sadefs"
)
// Stringify converts search attributes to map of strings using (in order):
// 1. type from MetadataType field,
// 2. type from typeMap (can be nil).
// In case of error, it will continue to next search attribute and return last error.
// Single values are converted using strconv, arrays are converted using json.Marshal.
// Search attributes with `nil` values are skipped.
func Stringify(searchAttributes *commonpb.SearchAttributes, typeMap *NameTypeMap) (map[string]string, error) {
stringify.go ×1
if len(searchAttributes.GetIndexedFields()) == 0 {
}
var lastErr error
for saName, saPayload := range searchAttributes.GetIndexedFields() {
saType := enumspb.INDEXED_VALUE_TYPE_UNSPECIFIED
if typeMap != nil {
saType, _ = typeMap.getType(saName, customCategory|predefinedCategory)
}
saValue, err := sadefs.DecodeValue(saPayload, saType, true)
if err != nil {
result[saName] = string(saPayload.GetData())
lastErr = err
continue
}
continue
}
result[saName] = saTypedValue
result[saName] = strconv.FormatInt(saTypedValue, 10)
case float64:
result[saName] = strconv.FormatFloat(saTypedValue, 'f', -1, 64)
result[saName] = strconv.FormatBool(saTypedValue)
case time.Time:
result[saName] = saTypedValue.Format(time.RFC3339Nano)
switch reflect.TypeOf(saValue).Kind() {
case reflect.Slice, reflect.Array:
valBytes, err := json.Marshal(saValue)
if err != nil {
result[saName] = string(saPayload.GetData())
lastErr = err
continue
}
default:
result[saName] = fmt.Sprintf("%v", saTypedValue)
}
}
}
}
// Parse converts maps of search attribute strings to search attributes.
// typeMap can be nil (values will be parsed with strconv and MetadataType field won't be set).
// In case of error, it will continue to next search attribute and return last error.
// Single values are parsed using strconv, arrays are parsed using json.Unmarshal.
func Parse(searchAttributesStr map[string]string, typeMap *NameTypeMap) (*commonpb.SearchAttributes, error) {
stringify.go ×1
if len(searchAttributesStr) == 0 {
}
IndexedFields: make(map[string]*commonpb.Payload, len(searchAttributesStr)),
}
var lastErr error
for saName, saValStr := range searchAttributesStr {
saType := enumspb.INDEXED_VALUE_TYPE_UNSPECIFIED
if typeMap != nil {
}
if err != nil {
}
}
}
func parseValueOrArray(valStr string, t enumspb.IndexedValueType) (*commonpb.Payload, error) {
stringify.go ×2
var val any
if isJSONArray(valStr) {
val, err = parseJSONArray(valStr, t)
if err != nil {
}
var err error
val, err = parseValueTyped(valStr, t)
if err != nil {
}
}
}
func parseValueTyped(valStr string, t enumspb.IndexedValueType) (any, error) {
stringify.go ×2
var val any
var err error
switch t {
case enumspb.INDEXED_VALUE_TYPE_TEXT,
enumspb.INDEXED_VALUE_TYPE_KEYWORD,
val = valStr
val, err = strconv.ParseInt(valStr, 10, 64)
val, err = strconv.ParseFloat(valStr, 64)
val, err = strconv.ParseBool(valStr)
val, err = time.Parse(time.RFC3339Nano, valStr)
val = parseValueUnspecified(valStr)
default:
err = fmt.Errorf("%w: %v", sadefs.ErrInvalidType, t)
}
}
var val any
var err error
if val, err = strconv.ParseInt(valStr, 10, 64); err == nil {
} else if val, err = strconv.ParseBool(valStr); err == nil {
} else if val, err = strconv.ParseFloat(valStr, 64); err == nil {
} else if val, err = time.Parse(time.RFC3339Nano, valStr); err == nil {
} else if isJSONArray(valStr) {
if err != nil {
val = valStr
val = arr
}
val = valStr
}
}
str = strings.TrimSpace(str)
return strings.HasPrefix(str, "[") && strings.HasSuffix(str, "]")
}
switch t {
case enumspb.INDEXED_VALUE_TYPE_TEXT,
enumspb.INDEXED_VALUE_TYPE_KEYWORD,
var result []string
err := json.Unmarshal([]byte(str), &result)
return result, err
var result []int64
err := json.Unmarshal([]byte(str), &result)
return result, err
var result []float64
err := json.Unmarshal([]byte(str), &result)
return result, err
var result []bool
err := json.Unmarshal([]byte(str), &result)
return result, err
var result []time.Time
err := json.Unmarshal([]byte(str), &result)
return result, err
var result []any
err := json.Unmarshal([]byte(str), &result)
return result, err
default:
return nil, fmt.Errorf("%w: %v", sadefs.ErrInvalidType, t)
}
}