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()
}