master
go 45 lines 858 Bytes
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package icecast
4
5 import (
6 "encoding/json"
7 "fmt"
8 )
9
10 type (
11 serverStats struct {
12 IceStats *struct {
13 Source iceSource `json:"source"`
14 } `json:"icestats"`
15 }
16 iceSource []sourceStats
17 sourceStats struct {
18 ServerName string `json:"server_name"`
19 StreamStart string `json:"stream_start"`
20 Listeners int64 `json:"listeners"`
21 }
22 )
23
24 func (i *iceSource) UnmarshalJSON(data []byte) error {
25 var v any
26 if err := json.Unmarshal(data, &v); err != nil {
27 return err
28 }
29
30 switch v.(type) {
31 case []any:
32 type plain iceSource
33 return json.Unmarshal(data, (*plain)(i))
34 case map[string]any:
35 var s sourceStats
36 if err := json.Unmarshal(data, &s); err != nil {
37 return err
38 }
39 *i = []sourceStats{s}
40 default:
41 return fmt.Errorf("invalid source data type: expected array or object")
42 }
43
44 return nil
45 }