package config import ( "encoding/json" "fmt" "strings" ) // ParseChannelAccounts decodes the CHANNELS_JSON value into a per-platform list // of ChannelAccount. The wire shape is map[platform]→[]ChannelAccount, for // example: // // {"telegram":[{"id":"conf0","config":{"bot_token":"..."},"denyUsers":["12345"]}]} // // An empty/blank input yields an empty (non-nil) map so a caller can iterate it // unconditionally. A malformed JSON or a non-array account value returns an // error so a bad config fails loudly at boot rather than silently dropping // channels. func ParseChannelAccounts(raw string) (map[string][]ChannelAccount, error) { if strings.TrimSpace(raw) == "" { return map[string][]ChannelAccount{}, nil } var m map[string][]ChannelAccount dec := json.NewDecoder(strings.NewReader(raw)) if err := dec.Decode(&m); err != nil { return nil, fmt.Errorf("config: decode CHANNELS_JSON: %w", err) } // Tolerate a top-level array of accounts that all share a blank platform name // by normalizing a nil map to an empty one. if m == nil { return map[string][]ChannelAccount{}, nil } return m, nil }