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 package idlewatcher
import ( import (
"encoding/json"
"iter" "iter"
"strconv" "strconv"
@ -11,9 +12,9 @@ type watcherDebug struct {
*Watcher *Watcher
} }
func (w watcherDebug) MarshalMap() map[string]any { func (w watcherDebug) MarshalJSON() ([]byte, error) {
state := w.state.Load() state := w.state.Load()
return map[string]any{ return json.Marshal(map[string]any{
"name": w.Name(), "name": w.Name(),
"state": map[string]string{ "state": map[string]string{
"status": string(state.status), "status": string(state.status),
@ -23,7 +24,7 @@ func (w watcherDebug) MarshalMap() map[string]any {
"expires": strutils.FormatTime(w.expires()), "expires": strutils.FormatTime(w.expires()),
"last_reset": strutils.FormatTime(w.lastReset.Load()), "last_reset": strutils.FormatTime(w.lastReset.Load()),
"config": w.cfg, "config": w.cfg,
} })
} }
func Watchers() iter.Seq2[string, watcherDebug] { 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 return w, false, nil
} }
// MarshalMap implements health.HealthMonitor. // MarshalJSON implements health.HealthMonitor.
func (w *Watcher) MarshalMap() map[string]any { func (w *Watcher) MarshalJSON() ([]byte, error) {
url := w.hc.URL() url := w.hc.URL()
if url.Port() == "0" { if url.Port() == "0" {
url = nil url = nil
@ -118,5 +118,5 @@ func (w *Watcher) MarshalMap() map[string]any {
Config: dummyHealthCheckConfig, Config: dummyHealthCheckConfig,
URL: url, URL: url,
Detail: detail, 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) lb.impl.ServeHTTP(srvs, rw, r)
} }
// MarshalMap implements health.HealthMonitor. // MarshalJSON implements health.HealthMonitor.
func (lb *LoadBalancer) MarshalMap() map[string]any { func (lb *LoadBalancer) MarshalJSON() ([]byte, error) {
extra := make(map[string]any) extra := make(map[string]any)
for _, srv := range lb.pool.Iter { for _, srv := range lb.pool.Iter {
extra[srv.Key()] = srv 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()), Detail: fmt.Sprintf("%d/%d servers are healthy", numHealthy, lb.pool.Size()),
Started: lb.startTime, Started: lb.startTime,
Uptime: lb.Uptime(), Uptime: lb.Uptime(),
Latency: lb.Latency(),
Extra: map[string]any{ Extra: map[string]any{
"config": lb.Config, "config": lb.Config,
"pool": extra, "pool": extra,
}, },
}).MarshalMap() }).MarshalJSON()
} }
// Name implements health.HealthMonitor. // Name implements health.HealthMonitor.

View file

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

View file

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

View file

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

View file

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

View file

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