MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / cloneRuntimeValue

Function cloneRuntimeValue

internal/config/clone.go:21–85  ·  view source on GitHub ↗
(v reflect.Value)

Source from the content-addressed store, hash-verified

19}
20
21func cloneRuntimeValue(v reflect.Value) reflect.Value {
22 if !v.IsValid() {
23 return v
24 }
25
26 if v.Type() == yamlNodeType {
27 node := v.Interface().(yaml.Node)
28 return reflect.ValueOf(*deepCopyNode(&node))
29 }
30
31 switch v.Kind() {
32 case reflect.Pointer:
33 if v.IsNil() {
34 return reflect.Zero(v.Type())
35 }
36 out := reflect.New(v.Type().Elem())
37 out.Elem().Set(cloneRuntimeValue(v.Elem()))
38 return out
39 case reflect.Interface:
40 if v.IsNil() {
41 return reflect.Zero(v.Type())
42 }
43 return cloneRuntimeValue(v.Elem())
44 case reflect.Struct:
45 if hasUnsettableStructFields(v) && v.Type().PkgPath() != configPackagePath {
46 return v
47 }
48 out := reflect.New(v.Type()).Elem()
49 for i := 0; i < v.NumField(); i++ {
50 dst := out.Field(i)
51 if !dst.CanSet() {
52 continue
53 }
54 dst.Set(cloneRuntimeValue(v.Field(i)))
55 }
56 return out
57 case reflect.Slice:
58 if v.IsNil() {
59 return reflect.Zero(v.Type())
60 }
61 out := reflect.MakeSlice(v.Type(), v.Len(), v.Len())
62 for i := 0; i < v.Len(); i++ {
63 out.Index(i).Set(cloneRuntimeValue(v.Index(i)))
64 }
65 return out
66 case reflect.Array:
67 out := reflect.New(v.Type()).Elem()
68 for i := 0; i < v.Len(); i++ {
69 out.Index(i).Set(cloneRuntimeValue(v.Index(i)))
70 }
71 return out
72 case reflect.Map:
73 if v.IsNil() {
74 return reflect.Zero(v.Type())
75 }
76 out := reflect.MakeMapWithSize(v.Type(), v.Len())
77 iter := v.MapRange()
78 for iter.Next() {

Callers 1

CloneForRuntimeMethod · 0.85

Calls 5

deepCopyNodeFunction · 0.85
LenMethod · 0.80
NextMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected