2022-05-02 20:07:15 +00:00
|
|
|
package utils
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-12-16 14:19:53 +00:00
|
|
|
"fmt"
|
2022-06-06 23:55:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Copy[T any](obj T) (*T, error) {
|
|
|
|
marshal, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal object: %w", err)
|
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 {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal object: %w", err)
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|