mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-08-01 18:29:02 -04:00
query.Normalize resolves field names (query.ResolveField, from the derived index + a small alias overlay) and expands media-type restrictions (mimetype.Expand) once, between parse and backend compilation.
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package query
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
|
|
)
|
|
|
|
// aliases are KQL spellings the derived index can't produce (fields are plural).
|
|
var aliases = map[string]string{
|
|
"tag": "Tags",
|
|
"favorite": "Favorites",
|
|
}
|
|
|
|
// fieldIndex maps a lowercased KQL key to the real field name: derived once from
|
|
// the resource struct, overlaid with the explicit aliases.
|
|
var fieldIndex = sync.OnceValue(func() map[string]string {
|
|
idx := mapping.FieldNameIndex(
|
|
reflect.TypeFor[search.Resource](),
|
|
search.Resource{}.SearchFieldOverrides(),
|
|
)
|
|
for k, v := range aliases {
|
|
idx[k] = v
|
|
}
|
|
return idx
|
|
})
|
|
|
|
// ResolveField maps a KQL key to the index field name: empty -> Name, a known
|
|
// key (case-insensitive) -> its field, anything else unchanged.
|
|
func ResolveField(name string) string {
|
|
if name == "" {
|
|
return "Name"
|
|
}
|
|
if v, ok := fieldIndex()[strings.ToLower(name)]; ok {
|
|
return v
|
|
}
|
|
return name
|
|
}
|