2022-05-02 20:07:15 +00:00
|
|
|
package utils
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Copy[T any](obj T) (*T, error) {
|
|
|
|
marshal, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal object")
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
|
|
|
|
out := new(T)
|
|
|
|
if err := json.Unmarshal(marshal, out); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal object")
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|