mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-11-29 09:00:21 +08:00
* feat(internal): add ListOr[T any] type This is a utility type that lets you decode a JSON T or list of T as a single value. This will be used with Redis Sentinel config so that you can specify multiple sentinel addresses. Ref TecharoHQ/botstopper#24 Assisted-by: GLM 4.6 via Claude Code Signed-off-by: Xe Iaso <me@xeiaso.net> * feat(store/valkey): add Redis(R) Sentinel support Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: spelling check-spelling run (pull_request) for Xe/redis-sentinel Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com> on-behalf-of: @check-spelling <check-spelling-bot@check-spelling.dev> * chore(store/valkey): remove pointless comments Signed-off-by: Xe Iaso <me@xeiaso.net> * docs: document the Redis™ Sentinel configuration options Signed-off-by: Xe Iaso <me@xeiaso.net> * fix(store/valkey): Redis™ Sentinel doesn't require a password Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: spelling Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: spelling Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com>
39 lines
No EOL
928 B
Go
39 lines
No EOL
928 B
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// ListOr[T any] is a slice that can contain either a single T or multiple T values.
|
|
// During JSON unmarshaling, it checks if the first character is '[' to determine
|
|
// whether to treat the JSON as an array or a single value.
|
|
type ListOr[T any] []T
|
|
|
|
func (lo *ListOr[T]) UnmarshalJSON(data []byte) error {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Check if first non-whitespace character is '['
|
|
firstChar := data[0]
|
|
for i := 0; i < len(data); i++ {
|
|
if data[i] != ' ' && data[i] != '\t' && data[i] != '\n' && data[i] != '\r' {
|
|
firstChar = data[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
if firstChar == '[' {
|
|
// It's an array, unmarshal directly
|
|
return json.Unmarshal(data, (*[]T)(lo))
|
|
} else {
|
|
// It's a single value, unmarshal as a single item in a slice
|
|
var single T
|
|
if err := json.Unmarshal(data, &single); err != nil {
|
|
return err
|
|
}
|
|
*lo = ListOr[T]{single}
|
|
}
|
|
|
|
return nil
|
|
} |