-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathclone.go
63 lines (54 loc) · 1.88 KB
/
clone.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
package helpers
import (
"fmt"
"reflect"
"github.com/jinzhu/copier"
"github.com/superfly/flyctl/internal/sentry"
)
// Clone clones *public fields* in a structure.
// Private fields may or may not be copied, and private pointers may or may not point into the original object.
// - this will panic if the structure is not serializable
// - See CloneFallible
func Clone[T any](v T) T {
ret, err := CloneFallible(v)
// Reference for what errors can be returned:
// https://github.com/jinzhu/copier/blob/20cee7e229707f8e3fd10f8ed21f3e6c08ca9463/errors.go
if err != nil {
typename := fmt.Sprintf("%T", v)
sentry.CaptureException(fmt.Errorf("failed to clone '%s': %w", typename, err))
panic(fmt.Sprintf("failed to deep-copy '%s'. this is a bug!\nerror: %v", typename, err))
}
return ret
}
// deepCopy is a little helper so that the implementation can be easily swapped out
// If from is type T, into should be *T and non-nil
func deepCopy(from any, into any) error {
return copier.CopyWithOption(into, from, copier.Option{IgnoreEmpty: true, DeepCopy: true})
}
// CloneFallible clones *public fields* in a structure.
// Private fields may or may not be copied, and private pointers may or may not point into the original object.
// - returns an error if the structure is not serializable
// - See Clone
func CloneFallible[T any](v T) (T, error) {
reflectedValue := reflect.ValueOf(v)
if reflectedValue.Kind() == reflect.Ptr {
var nilT T
if reflectedValue.IsNil() {
return nilT, nil
}
cloned := reflect.New(reflect.Indirect(reflectedValue).Type())
err := deepCopy(v, cloned.Interface())
if err != nil {
return nilT, err
}
ret, ok := cloned.Interface().(T)
if !ok {
return nilT, fmt.Errorf("could not convert pointer back to generic T, got type %v", cloned.Type())
}
return ret, nil
} else {
var cloned T
err := deepCopy(v, &cloned)
return cloned, err
}
}