copy.go 457 B

1234567891011121314151617181920212223242526
  1. package common
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/copier"
  5. )
  6. func Copy[T any](src *T, deepCopy bool) (*T, error) {
  7. if src == nil {
  8. return nil, fmt.Errorf("copy source cannot be nil")
  9. }
  10. var dst T
  11. if deepCopy {
  12. err := copier.CopyWithOption(&dst, src, copier.Option{DeepCopy: true, IgnoreEmpty: true})
  13. if err != nil {
  14. return nil, err
  15. }
  16. } else {
  17. err := copier.Copy(&dst, src)
  18. if err != nil {
  19. return nil, err
  20. }
  21. }
  22. return &dst, nil
  23. }