mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-20 00:25:02 +01:00
refactor: refactor to adapt new custom json marshaler
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package atomic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/yusing/go-proxy/pkg/json"
|
||||
)
|
||||
|
||||
type Value[T any] struct {
|
||||
@@ -29,6 +30,6 @@ func (a *Value[T]) Swap(v T) T {
|
||||
return zero
|
||||
}
|
||||
|
||||
func (a *Value[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(a.Load())
|
||||
func (a *Value[T]) MarshalJSONTo(buf []byte) []byte {
|
||||
return json.MarshalTo(a.Load(), buf)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
@@ -12,6 +11,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yusing/go-proxy/pkg/json"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/goccy/go-yaml"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
@@ -22,12 +23,15 @@ import (
|
||||
type SerializedObject = map[string]any
|
||||
|
||||
type (
|
||||
MapMarshaller interface {
|
||||
MapMarshaler interface {
|
||||
MarshalMap() map[string]any
|
||||
}
|
||||
MapUnmarshaller interface {
|
||||
UnmarshalMap(m map[string]any) gperr.Error
|
||||
}
|
||||
MapUnmarshaler interface {
|
||||
UnmarshalMap(m map[string]any) gperr.Error
|
||||
}
|
||||
Marshaler interface {
|
||||
MarshalJSONTo(buf *bytes.Buffer) error
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -51,11 +55,8 @@ var (
|
||||
typeURL = reflect.TypeFor[url.URL]()
|
||||
typeCIDR = reflect.TypeFor[net.IPNet]()
|
||||
|
||||
typeMapMarshaller = reflect.TypeFor[MapMarshaller]()
|
||||
typeMapUnmarshaler = reflect.TypeFor[MapUnmarshaller]()
|
||||
typeJSONMarshaller = reflect.TypeFor[json.Marshaler]()
|
||||
|
||||
typeAny = reflect.TypeOf((*any)(nil)).Elem()
|
||||
typeMapUnmarshaler = reflect.TypeFor[MapUnmarshaler]()
|
||||
typeStrParser = reflect.TypeFor[strutils.Parser]()
|
||||
)
|
||||
|
||||
var defaultValues = functional.NewMapOf[reflect.Type, func() any]()
|
||||
@@ -92,14 +93,14 @@ func extractFields(t reflect.Type) (all, anonymous []reflect.StructField) {
|
||||
if !field.IsExported() {
|
||||
continue
|
||||
}
|
||||
// not checking tagJSON because json:"-" is for skipping json.Marshal
|
||||
if field.Tag.Get(tagDeserialize) == "-" {
|
||||
continue
|
||||
}
|
||||
if field.Anonymous {
|
||||
f1, f2 := extractFields(field.Type)
|
||||
fields = append(fields, f1...)
|
||||
nested, _ := extractFields(field.Type)
|
||||
fields = append(fields, nested...)
|
||||
anonymous = append(anonymous, field)
|
||||
anonymous = append(anonymous, f2...)
|
||||
} else {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
@@ -215,7 +216,7 @@ func MapUnmarshalValidate(src SerializedObject, dst any) (err gperr.Error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return dstV.Addr().Interface().(MapUnmarshaller).UnmarshalMap(src)
|
||||
return dstV.Addr().Interface().(MapUnmarshaler).UnmarshalMap(src)
|
||||
}
|
||||
|
||||
dstV, dstT, err = dive(dstV)
|
||||
|
||||
Reference in New Issue
Block a user