diff --git a/common/json/badjson/merge.go b/common/json/badjson/merge.go index a2b8667da..43b878840 100644 --- a/common/json/badjson/merge.go +++ b/common/json/badjson/merge.go @@ -8,27 +8,6 @@ import ( "github.com/sagernet/sing/common/json" ) -func Merge[T any](source T, destination T) (T, error) { - rawSource, err := json.Marshal(source) - if err != nil { - return common.DefaultValue[T](), E.Cause(err, "marshal source") - } - rawDestination, err := json.Marshal(destination) - if err != nil { - return common.DefaultValue[T](), E.Cause(err, "marshal destination") - } - rawMerged, err := MergeJSON(rawSource, rawDestination) - if err != nil { - return common.DefaultValue[T](), E.Cause(err, "merge options") - } - var merged T - err = json.Unmarshal(rawMerged, &merged) - if err != nil { - return common.DefaultValue[T](), E.Cause(err, "unmarshal merged options") - } - return merged, nil -} - func Omitempty[T any](value T) (T, error) { objectContent, err := json.Marshal(value) if err != nil { @@ -50,6 +29,31 @@ func Omitempty[T any](value T) (T, error) { return newObject, nil } +func Merge[T any](source T, destination T) (T, error) { + rawSource, err := json.Marshal(source) + if err != nil { + return common.DefaultValue[T](), E.Cause(err, "marshal source") + } + return MergeFromJSON(rawSource, destination) +} + +func MergeFromJSON[T any](rawSource json.RawMessage, destination T) (T, error) { + rawDestination, err := json.Marshal(destination) + if err != nil { + return common.DefaultValue[T](), E.Cause(err, "marshal destination") + } + rawMerged, err := MergeJSON(rawSource, rawDestination) + if err != nil { + return common.DefaultValue[T](), E.Cause(err, "merge options") + } + var merged T + err = json.Unmarshal(rawMerged, &merged) + if err != nil { + return common.DefaultValue[T](), E.Cause(err, "unmarshal merged options") + } + return merged, nil +} + func MergeJSON(rawSource json.RawMessage, rawDestination json.RawMessage) (json.RawMessage, error) { source, err := Decode(rawSource) if err != nil {