forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_copy.go
47 lines (40 loc) · 895 Bytes
/
deep_copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package object
import (
"github.com/bytedance/sonic"
)
type DeepCopyable interface {
DeepCopy() interface{}
}
func (obj *Obj) DeepCopy() *Obj {
newObj := &Obj{
Type: obj.Type,
LastAccessedAt: obj.LastAccessedAt,
}
// Use the DeepCopyable interface to deep copy the value
if copier, ok := obj.Value.(DeepCopyable); ok {
newObj.Value = copier.DeepCopy()
} else {
// Handle types that are not DeepCopyable
sourceType := obj.Type
switch sourceType {
case ObjTypeString:
sourceValue := obj.Value.(string)
newObj.Value = sourceValue
case ObjTypeJSON:
sourceValue := obj.Value
jsonStr, err := sonic.MarshalString(sourceValue)
if err != nil {
return nil
}
var value interface{}
err = sonic.UnmarshalString(jsonStr, &value)
if err != nil {
return nil
}
newObj.Value = value
default:
return nil
}
}
return newObj
}