From bf429998e88210659e17456db6b94b15cd0094a6 Mon Sep 17 00:00:00 2001 From: yusing Date: Wed, 16 Apr 2025 12:26:19 +0800 Subject: [PATCH] fix: build error --- internal/api/v1/dockerapi/info.go | 2 +- internal/gperr/base.go | 6 +++--- internal/gperr/subject.go | 6 +++--- internal/gperr/utils.go | 2 +- internal/net/gphttp/gpwebsocket/utils.go | 3 +-- internal/utils/serialization.go | 10 +++++----- internal/watcher/health/types.go | 2 +- 7 files changed, 15 insertions(+), 16 deletions(-) diff --git a/internal/api/v1/dockerapi/info.go b/internal/api/v1/dockerapi/info.go index 373895d..a8bd08c 100644 --- a/internal/api/v1/dockerapi/info.go +++ b/internal/api/v1/dockerapi/info.go @@ -25,7 +25,7 @@ func (d *dockerInfo) MarshalJSON() ([]byte, error) { }, "images": d.Images, "n_cpu": d.NCPU, - "memory": strutils.FormatByteSizeWithUnit(d.MemTotal), + "memory": strutils.FormatByteSize(d.MemTotal), }) } diff --git a/internal/gperr/base.go b/internal/gperr/base.go index 65c5860..bc30107 100644 --- a/internal/gperr/base.go +++ b/internal/gperr/base.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/yusing/go-proxy/pkg/json" + "encoding/json" ) // baseError is an immutable wrapper around an error. @@ -49,6 +49,6 @@ func (err *baseError) Error() string { return err.Err.Error() } -func (err *baseError) MarshalJSONTo(buf []byte) []byte { - return json.MarshalTo(err.Err, buf) +func (err *baseError) MarshalJSON() ([]byte, error) { + return json.Marshal(err.Err) } diff --git a/internal/gperr/subject.go b/internal/gperr/subject.go index c6640de..4439951 100644 --- a/internal/gperr/subject.go +++ b/internal/gperr/subject.go @@ -5,7 +5,7 @@ import ( "slices" "strings" - "github.com/yusing/go-proxy/pkg/json" + "encoding/json" "github.com/yusing/go-proxy/internal/utils/strutils/ansi" ) @@ -94,7 +94,7 @@ func (err *withSubject) Error() string { return sb.String() } -func (err *withSubject) MarshalJSONTo(buf []byte) []byte { +func (err *withSubject) MarshalJSON() ([]byte, error) { subjects := slices.Clone(err.Subjects) slices.Reverse(subjects) @@ -102,5 +102,5 @@ func (err *withSubject) MarshalJSONTo(buf []byte) []byte { "subjects": subjects, "err": err.Err, } - return json.MarshalTo(reversed, buf) + return json.Marshal(reversed) } diff --git a/internal/gperr/utils.go b/internal/gperr/utils.go index 6b749f5..b26b683 100644 --- a/internal/gperr/utils.go +++ b/internal/gperr/utils.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/yusing/go-proxy/pkg/json" + "encoding/json" ) func newError(message string) error { diff --git a/internal/net/gphttp/gpwebsocket/utils.go b/internal/net/gphttp/gpwebsocket/utils.go index cc98592..be4fa74 100644 --- a/internal/net/gphttp/gpwebsocket/utils.go +++ b/internal/net/gphttp/gpwebsocket/utils.go @@ -12,7 +12,6 @@ import ( "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/net/gphttp" "github.com/yusing/go-proxy/internal/net/gphttp/httpheaders" - "github.com/yusing/go-proxy/internal/utils" ) func warnNoMatchDomains() { @@ -95,7 +94,7 @@ func WriteText(r *http.Request, conn *websocket.Conn, msg string) bool { func DynamicJSONHandler[ResultType any](w http.ResponseWriter, r *http.Request, getter func() ResultType, interval time.Duration) { if httpheaders.IsWebsocket(r.Header) { Periodic(w, r, interval, func(conn *websocket.Conn) error { - return wsjson.Write(r.Context(), conn, utils.ToJSONObject(getter())) + return wsjson.Write(r.Context(), conn, getter()) }) } else { gphttp.RespondJSON(w, r, getter()) diff --git a/internal/utils/serialization.go b/internal/utils/serialization.go index 995512e..5120e29 100644 --- a/internal/utils/serialization.go +++ b/internal/utils/serialization.go @@ -1,7 +1,6 @@ package utils import ( - "bytes" "encoding/json" "errors" "net" @@ -22,12 +21,12 @@ import ( type SerializedObject = map[string]any type ( - MapMarshaller interface { + MapMarshaler interface { MarshalMap() map[string]any } MapUnmarshaller interface { - UnmarshalMap(m map[string]any) gperr.Error -} + UnmarshalMap(m map[string]any) gperr.Error + } ) var ( @@ -51,9 +50,10 @@ var ( typeURL = reflect.TypeFor[url.URL]() typeCIDR = reflect.TypeFor[net.IPNet]() - typeMapMarshaller = reflect.TypeFor[MapMarshaller]() + typeMapMarshaller = reflect.TypeFor[MapMarshaler]() typeMapUnmarshaler = reflect.TypeFor[MapUnmarshaller]() typeJSONMarshaller = reflect.TypeFor[json.Marshaler]() + typeStrParser = reflect.TypeFor[strutils.Parser]() typeAny = reflect.TypeOf((*any)(nil)).Elem() ) diff --git a/internal/watcher/health/types.go b/internal/watcher/health/types.go index de7ab11..8f83143 100644 --- a/internal/watcher/health/types.go +++ b/internal/watcher/health/types.go @@ -24,7 +24,7 @@ type ( task.TaskStarter task.TaskFinisher fmt.Stringer - utils.MapMarshaller + utils.MapMarshaler WithHealthInfo Name() string }