fix: json marshaling

This commit is contained in:
yusing 2025-04-26 01:31:22 +08:00
parent db6fc65876
commit 03d609e4e1
8 changed files with 28 additions and 22 deletions

View file

@ -1,6 +1,7 @@
package idlewatcher
import (
"encoding/json"
"iter"
"strconv"
@ -11,9 +12,9 @@ type watcherDebug struct {
*Watcher
}
func (w watcherDebug) MarshalMap() map[string]any {
func (w watcherDebug) MarshalJSON() ([]byte, error) {
state := w.state.Load()
return map[string]any{
return json.Marshal(map[string]any{
"name": w.Name(),
"state": map[string]string{
"status": string(state.status),
@ -23,7 +24,7 @@ func (w watcherDebug) MarshalMap() map[string]any {
"expires": strutils.FormatTime(w.expires()),
"last_reset": strutils.FormatTime(w.lastReset.Load()),
"config": w.cfg,
}
})
}
func Watchers() iter.Seq2[string, watcherDebug] {

View file

@ -102,8 +102,8 @@ func checkUpdateState(key string) (w *Watcher, ready bool, err error) {
return w, false, nil
}
// MarshalMap implements health.HealthMonitor.
func (w *Watcher) MarshalMap() map[string]any {
// MarshalJSON implements health.HealthMonitor.
func (w *Watcher) MarshalJSON() ([]byte, error) {
url := w.hc.URL()
if url.Port() == "0" {
url = nil
@ -118,5 +118,5 @@ func (w *Watcher) MarshalMap() map[string]any {
Config: dummyHealthCheckConfig,
URL: url,
Detail: detail,
}).MarshalMap()
}).MarshalJSON()
}

View file

@ -237,8 +237,8 @@ func (lb *LoadBalancer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
lb.impl.ServeHTTP(srvs, rw, r)
}
// MarshalMap implements health.HealthMonitor.
func (lb *LoadBalancer) MarshalMap() map[string]any {
// MarshalJSON implements health.HealthMonitor.
func (lb *LoadBalancer) MarshalJSON() ([]byte, error) {
extra := make(map[string]any)
for _, srv := range lb.pool.Iter {
extra[srv.Key()] = srv
@ -252,11 +252,12 @@ func (lb *LoadBalancer) MarshalMap() map[string]any {
Detail: fmt.Sprintf("%d/%d servers are healthy", numHealthy, lb.pool.Size()),
Started: lb.startTime,
Uptime: lb.Uptime(),
Latency: lb.Latency(),
Extra: map[string]any{
"config": lb.Config,
"pool": extra,
},
}).MarshalMap()
}).MarshalJSON()
}
// Name implements health.HealthMonitor.

View file

@ -2,6 +2,7 @@ package proxmox
import (
"context"
"encoding/json"
"fmt"
"github.com/luthermonson/go-proxmox"
@ -45,9 +46,8 @@ func (c *Client) Name() string {
return c.Cluster.Name
}
// MarshalMap implements pool.Object
func (c *Client) MarshalMap() map[string]any {
return map[string]any{
func (c *Client) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"version": c.Version,
"cluster": map[string]any{
"name": c.Cluster.Name,
@ -56,7 +56,7 @@ func (c *Client) MarshalMap() map[string]any {
"nodes": c.Cluster.Nodes,
"quorate": c.Cluster.Quorate,
},
}
})
}
func (c *Client) NumNodes() int {

View file

@ -2,6 +2,7 @@ package proxmox
import (
"context"
"encoding/json"
"fmt"
"strings"
@ -38,11 +39,11 @@ func (n *Node) String() string {
return fmt.Sprintf("%s (%s)", n.name, n.id)
}
func (n *Node) MarshalMap() map[string]any {
return map[string]any{
func (n *Node) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"name": n.name,
"id": n.id,
}
})
}
func (n *Node) Get(ctx context.Context, path string, v any) error {

View file

@ -1,6 +1,7 @@
package health
import (
"encoding/json"
"net/url"
"strconv"
"time"
@ -21,7 +22,7 @@ type JSONRepresentation struct {
Extra map[string]any
}
func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any {
func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
var url string
if jsonRepr.URL != nil {
url = jsonRepr.URL.String()
@ -29,7 +30,7 @@ func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any {
if url == "http://:0" {
url = ""
}
return map[string]any{
return json.Marshal(map[string]any{
"name": jsonRepr.Name,
"config": jsonRepr.Config,
"started": jsonRepr.Started.Unix(),
@ -44,5 +45,5 @@ func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any {
"detail": jsonRepr.Detail,
"url": url,
"extra": jsonRepr.Extra,
}
})
}

View file

@ -179,8 +179,8 @@ func (mon *monitor) String() string {
return mon.Name()
}
// MarshalMap implements health.HealthMonitor.
func (mon *monitor) MarshalMap() map[string]any {
// MarshalJSON implements health.HealthMonitor.
func (mon *monitor) MarshalJSON() ([]byte, error) {
res := mon.lastResult.Load()
if res == nil {
res = &health.HealthCheckResult{
@ -198,7 +198,7 @@ func (mon *monitor) MarshalMap() map[string]any {
LastSeen: GetLastSeen(mon.service),
Detail: res.Detail,
URL: mon.url.Load(),
}).MarshalMap()
}).MarshalJSON()
}
func (mon *monitor) checkUpdateHealth() error {

View file

@ -1,6 +1,7 @@
package health
import (
"encoding/json"
"fmt"
"net/url"
"time"
@ -25,6 +26,7 @@ type (
fmt.Stringer
WithHealthInfo
Name() string
json.Marshaler
}
HealthChecker interface {
CheckHealth() (result *HealthCheckResult, err error)