mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 18:10:29 +00:00
feat: task separate proxy (#727)
This commit is contained in:
22 files changed
+447
-67
No files matched your search
@@ -12,3 +12,17 @@ func MapToStruct(s any, v any) error {
|
||||
}
|
||||
return json.Unmarshal(b, v)
|
||||
}
|
||||
|
||||
func DeepClone[T any](v *T) *T {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var t T
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return &t
|
||||
}
|
||||
json.Unmarshal(b, &t)
|
||||
return &t
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeepClone(t *testing.T) {
|
||||
type user struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
|
||||
v int
|
||||
}
|
||||
|
||||
type args[T any] struct {
|
||||
v *T
|
||||
}
|
||||
type testCase[T any] struct {
|
||||
name string
|
||||
args args[T]
|
||||
want *T
|
||||
}
|
||||
tests := []testCase[user]{
|
||||
{
|
||||
name: "case 1",
|
||||
args: args[user]{
|
||||
v: &user{
|
||||
Name: "test",
|
||||
Age: 10,
|
||||
},
|
||||
},
|
||||
want: &user{
|
||||
Name: "test",
|
||||
Age: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "case 2",
|
||||
args: args[user]{
|
||||
v: &user{
|
||||
Name: "test",
|
||||
Age: 10,
|
||||
v: 1,
|
||||
},
|
||||
},
|
||||
want: &user{
|
||||
Name: "test",
|
||||
Age: 10,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := DeepClone(tt.args.v); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("DeepClone() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user