-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
44 lines (40 loc) · 1.11 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package opensearchutil
import (
"reflect"
"strings"
)
func MakePtr[V any](v V) *V {
return &v
}
// getTagOptionValue gets a tag option value. For example, given a tag "type:keyword", getTagOptionValue("type")
// returns "keyword".
func getTagOptionValue(structField reflect.StructField, tagKey string, optionKey string) string {
const tagOptionSep = ","
const keyValSep = ":"
if tag := structField.Tag.Get(tagKey); tag != "" {
for _, kvs := range strings.Split(tag, tagOptionSep) {
kv := strings.Split(kvs, keyValSep)
if len(kv) > 1 {
if strings.Trim(kv[0], " ") == optionKey {
return strings.Join(kv[1:], keyValSep)
}
}
}
}
return ""
}
// parseCustomPropertyValue parses a string like "min_chars=2;foo=bar" into a map like
// map[string]string{"min_chars":"2", "foo":"bar"}.
func parseCustomPropertyValue(str string) map[string]string {
const keyValSep = "="
pairs := strings.Split(str, ";")
m := make(map[string]string, len(pairs))
for _, kvs := range pairs {
kv := strings.Split(kvs, keyValSep)
if len(kv) > 1 {
val := strings.Join(kv[1:], keyValSep)
m[kv[0]] = val
}
}
return m
}