fix(loader): wire context into HTTP request, guard nil opts in Redis list/hash

- HTTPLoader.Load: http.NewRequest → NewRequestWithContext so caller
  cancellation/deadlines propagate to the outgoing request
- RedisListLoader, RedisHashLoader: add nil-guard before calling opt()
  (already present in StringLoader, SetLoader, and HTTPLoader)
- Add doc comments for all 10 exported symbols (package, interfaces,
  constructors, option types, option funcs, DefaultRedisKey)
- Add loader_test.go with 32 tests covering FileLoader, HTTPLoader
  (context cancel, error status, empty/large body), Redis options,
  nil-guard safety, and interface satisfaction checks
This commit is contained in:
ginuerzh
2026-05-24 18:04:02 +08:00
parent b991baaf72
commit 51455da96f
4 changed files with 419 additions and 3 deletions
+12 -2
View File
@@ -11,6 +11,7 @@ import (
)
const (
// DefaultRedisKey is the default Redis key used by loaders.
DefaultRedisKey = "gost"
)
@@ -21,26 +22,31 @@ type redisLoaderOptions struct {
key string
}
// RedisLoaderOption configures a Redis loader.
type RedisLoaderOption func(opts *redisLoaderOptions)
// DBRedisLoaderOption sets the Redis database number.
func DBRedisLoaderOption(db int) RedisLoaderOption {
return func(opts *redisLoaderOptions) {
opts.db = db
}
}
// UsernameRedisLoaderOption sets the Redis username.
func UsernameRedisLoaderOption(username string) RedisLoaderOption {
return func(opts *redisLoaderOptions) {
opts.username = username
}
}
// PasswordRedisLoaderOption sets the Redis password.
func PasswordRedisLoaderOption(password string) RedisLoaderOption {
return func(opts *redisLoaderOptions) {
opts.password = password
}
}
// KeyRedisLoaderOption sets the Redis key.
func KeyRedisLoaderOption(key string) RedisLoaderOption {
return func(opts *redisLoaderOptions) {
opts.key = key
@@ -145,7 +151,9 @@ type redisListLoader struct {
func RedisListLoader(addr string, opts ...RedisLoaderOption) Loader {
var options redisLoaderOptions
for _, opt := range opts {
opt(&options)
if opt != nil {
opt(&options)
}
}
key := options.key
@@ -190,7 +198,9 @@ type redisHashLoader struct {
func RedisHashLoader(addr string, opts ...RedisLoaderOption) Loader {
var options redisLoaderOptions
for _, opt := range opts {
opt(&options)
if opt != nil {
opt(&options)
}
}
key := options.key