feat: custom json marshaling implementation, replace json and yaml library (#89)

* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic

* fix: yaml unmarshal panic

* feat: custom json marshaler implementation

* chore: fix import and err marshal handling

---------

Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
Yuzerion
2025-04-16 15:02:11 +08:00
committed by GitHub
parent 57292f0fe8
commit 80bc018a7f
65 changed files with 1749 additions and 205 deletions

View File

@@ -5,6 +5,7 @@ import (
"strconv"
"github.com/docker/docker/client"
"github.com/goccy/go-yaml"
"github.com/rs/zerolog"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/docker"
@@ -14,7 +15,6 @@ import (
U "github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils/strutils"
"github.com/yusing/go-proxy/internal/watcher"
"gopkg.in/yaml.v3"
)
type DockerProvider struct {

View File

@@ -4,9 +4,9 @@ import (
"testing"
"github.com/docker/docker/api/types"
"github.com/goccy/go-yaml"
"github.com/yusing/go-proxy/internal/docker"
. "github.com/yusing/go-proxy/internal/utils/testing"
"gopkg.in/yaml.v3"
_ "embed"
)

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 {