1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-07-03 04:56:59 +02:00

optimized list.ToUniqueStringSlice

This commit is contained in:
Gani Georgiev
2022-12-10 12:08:59 +02:00
parent 68a9782c03
commit aa6eaa7319

View File

@ -80,7 +80,10 @@ func NonzeroUniques[T comparable](list []T) []T {
var zeroVal T var zeroVal T
for _, val := range list { for _, val := range list {
if _, ok := existMap[val]; ok || val == zeroVal { if val == zeroVal {
continue
}
if _, ok := existMap[val]; ok {
continue continue
} }
existMap[val] = struct{}{} existMap[val] = struct{}{}
@ -103,10 +106,15 @@ func ToUniqueStringSlice(value any) (result []string) {
} }
// check if it is a json encoded array of strings // check if it is a json encoded array of strings
if strings.Contains(val, "[") {
if err := json.Unmarshal([]byte(val), &result); err != nil { if err := json.Unmarshal([]byte(val), &result); err != nil {
// not a json array, just add the string as single array element // not a json array, just add the string as single array element
result = append(result, val) result = append(result, val)
} }
} else {
// just add the string as single array element
result = append(result, val)
}
case json.Marshaler: // eg. JsonArray case json.Marshaler: // eg. JsonArray
raw, _ := val.MarshalJSON() raw, _ := val.MarshalJSON()
_ = json.Unmarshal(raw, &result) _ = json.Unmarshal(raw, &result)