refactor: refactor to adapt new custom json marshaler

This commit is contained in:
yusing
2025-04-16 14:39:26 +08:00
parent cdfc9d553b
commit c2b606e63e
43 changed files with 232 additions and 189 deletions

View File

@@ -26,8 +26,8 @@ func getHealthInfo(r route.Route) map[string]string {
}
type HealthInfoRaw struct {
Status health.Status
Latency time.Duration
Status health.Status `json:"status,string"`
Latency time.Duration `json:"latency"`
}
func getHealthInfoRaw(r route.Route) *HealthInfoRaw {

View File

@@ -300,7 +300,3 @@ func (cmd *Command) isBypass() bool {
func (cmd *Command) String() string {
return cmd.raw
}
func (cmd *Command) MarshalText() ([]byte, error) {
return []byte(cmd.String()), nil
}

View File

@@ -261,10 +261,6 @@ func (on *RuleOn) String() string {
return on.raw
}
func (on *RuleOn) MarshalText() ([]byte, error) {
return []byte(on.String()), nil
}
func parseOn(line string) (Checker, gperr.Error) {
ors := strutils.SplitRune(line, '|')

View File

@@ -1,8 +1,9 @@
package rules
import (
"encoding/json"
"net/http"
"github.com/yusing/go-proxy/pkg/json"
)
type (
@@ -40,8 +41,8 @@ type (
*/
Rule struct {
Name string `json:"name"`
On RuleOn `json:"on"`
Do Command `json:"do"`
On RuleOn `json:"on,string"`
Do Command `json:"do,string"`
}
)
@@ -102,12 +103,12 @@ func (rules Rules) BuildHandler(caller string, up http.Handler) http.HandlerFunc
}
}
func (rules Rules) MarshalJSON() ([]byte, error) {
func (rules Rules) MarshalJSONTo(buf []byte) []byte {
names := make([]string, len(rules))
for i, rule := range rules {
names[i] = rule.Name
}
return json.Marshal(names)
return json.MarshalTo(names, buf)
}
func (rule *Rule) String() string {