feedkit now contains a reusable core, while weatherfeeder is a concrete implementation that includes weather-specific functions.
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
// feedkit/config/params.go
|
|
package config
|
|
|
|
import "strings"
|
|
|
|
// ParamString returns the first non-empty string found for any of the provided keys.
|
|
// Values must actually be strings in the decoded config; other types are ignored.
|
|
//
|
|
// This keeps cfg.Params flexible (map[string]any) while letting callers stay type-safe.
|
|
func (cfg SourceConfig) ParamString(keys ...string) (string, bool) {
|
|
if cfg.Params == nil {
|
|
return "", false
|
|
}
|
|
for _, k := range keys {
|
|
v, ok := cfg.Params[k]
|
|
if !ok || v == nil {
|
|
continue
|
|
}
|
|
s, ok := v.(string)
|
|
if !ok {
|
|
continue
|
|
}
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
continue
|
|
}
|
|
return s, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// ParamStringDefault returns ParamString(keys...) if present; otherwise it returns def.
|
|
// This is the “polite default” helper used by drivers for optional fields like user-agent.
|
|
func (cfg SourceConfig) ParamStringDefault(def string, keys ...string) string {
|
|
if s, ok := cfg.ParamString(keys...); ok {
|
|
return s
|
|
}
|
|
return strings.TrimSpace(def)
|
|
}
|
|
|
|
// ParamString returns the first non-empty string found for any of the provided keys
|
|
// in SinkConfig.Params. (Same rationale as SourceConfig.ParamString.)
|
|
func (cfg SinkConfig) ParamString(keys ...string) (string, bool) {
|
|
if cfg.Params == nil {
|
|
return "", false
|
|
}
|
|
for _, k := range keys {
|
|
v, ok := cfg.Params[k]
|
|
if !ok || v == nil {
|
|
continue
|
|
}
|
|
s, ok := v.(string)
|
|
if !ok {
|
|
continue
|
|
}
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
continue
|
|
}
|
|
return s, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// ParamStringDefault returns ParamString(keys...) if present; otherwise it returns def.
|
|
// Symmetric helper for sink implementations.
|
|
func (cfg SinkConfig) ParamStringDefault(def string, keys ...string) string {
|
|
if s, ok := cfg.ParamString(keys...); ok {
|
|
return s
|
|
}
|
|
return strings.TrimSpace(def)
|
|
}
|