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