/
deep_copy.go
110 lines (95 loc) · 3.98 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package helpers
import (
"reflect"
didcli "github.com/canow-co/cheqd-node/x/did/client/cli"
didtypes "github.com/canow-co/cheqd-node/x/did/types"
resourcetypes "github.com/canow-co/cheqd-node/x/resource/types"
)
// IDeepCopy is an interface for deep copy in the decorator pattern.
type IDeepCopy interface {
DeepCopy(src interface{}) interface{}
}
// TDeepCopy is a decorator for deep copy.
type TDeepCopy struct{}
func (d *TDeepCopy) DeepCopy(src interface{}) interface{} {
return deepCopy(src)
}
// DeepCopyCreateDid is a decorator for deep copy of type MsgCreateDidPayload.
type DeepCopyCreateDid struct {
TDeepCopy
}
func (d *DeepCopyCreateDid) DeepCopy(src didtypes.MsgCreateDidDocPayload) didtypes.MsgCreateDidDocPayload {
return deepCopy(src).(didtypes.MsgCreateDidDocPayload)
}
// DeepCopyUpdateDid is a decorator for deep copy of type MsgUpdateDidPayload.
type DeepCopyUpdateDid struct {
TDeepCopy
}
func (d *DeepCopyUpdateDid) DeepCopy(src didtypes.MsgUpdateDidDocPayload) didtypes.MsgUpdateDidDocPayload {
return deepCopy(src).(didtypes.MsgUpdateDidDocPayload)
}
type DeepCopyDIDDocument struct {
TDeepCopy
}
func (d *DeepCopyDIDDocument) DeepCopy(src didcli.DIDDocument) didcli.DIDDocument {
return deepCopy(src).(didcli.DIDDocument)
}
// DeepCopyCreateResource is a decorator for deep copy of type MsgCreateResource.
type DeepCopyCreateResource struct {
TDeepCopy
}
func (d *DeepCopyCreateResource) DeepCopy(src resourcetypes.MsgCreateResource) resourcetypes.MsgCreateResource {
return deepCopy(src).(resourcetypes.MsgCreateResource)
}
func deepCopy(src interface{}) interface{} {
var reflection interface{}
var dst reflect.Value
switch actualSrc := (src).(type) {
case didtypes.MsgCreateDidDocPayload:
// Create a reflection slice of the same length as the source slice
reflection = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(actualSrc)), 1, 1).Interface()
// Extract destination value as definition
dst = reflect.ValueOf(reflection)
// Define source value as slice
slc := []didtypes.MsgCreateDidDocPayload{actualSrc}
// Copy the source value into the destination
reflect.Copy(dst, reflect.ValueOf(slc))
// Return the destination value from the reflection slice
return dst.Index(0).Interface().(didtypes.MsgCreateDidDocPayload)
case didtypes.MsgUpdateDidDocPayload:
// Create a reflection slice of the same length as the source slice
reflection = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(actualSrc)), 1, 1).Interface()
// Extract destination value as definition
dst = reflect.ValueOf(reflection)
// Define source value as slice
slc := []didtypes.MsgUpdateDidDocPayload{actualSrc}
// Copy the source value into the destination
reflect.Copy(dst, reflect.ValueOf(slc))
// Return the destination value from the reflection slice
return dst.Index(0).Interface().(didtypes.MsgUpdateDidDocPayload)
case resourcetypes.MsgCreateResource:
// Create a reflection slice of the same length as the source slice
reflection = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(actualSrc)), 1, 1).Interface()
// Extract destination value as definition
dst = reflect.ValueOf(reflection)
// Define source value as slice
slc := []resourcetypes.MsgCreateResource{actualSrc}
// Copy the source value into the destination
reflect.Copy(dst, reflect.ValueOf(slc))
// Return the destination value from the reflection slice
return dst.Index(0).Interface().(resourcetypes.MsgCreateResource)
case didcli.DIDDocument:
// Create a reflection slice of the same length as the source slice
reflection = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(actualSrc)), 1, 1).Interface()
// Extract destination value as definition
dst = reflect.ValueOf(reflection)
// Define source value as slice
slc := []didcli.DIDDocument{actualSrc}
// Copy the source value into the destination
reflect.Copy(dst, reflect.ValueOf(slc))
// Return the destination value from the reflection slice
return dst.Index(0).Interface().(didcli.DIDDocument)
default:
panic("Unsupported type")
}
}