2024-08-23 17:04:19 +02:00
|
|
|
package dto
|
|
|
|
|
|
|
|
|
|
import (
|
2025-06-29 06:01:10 -07:00
|
|
|
"fmt"
|
2025-02-05 18:08:01 +01:00
|
|
|
|
2025-06-29 06:01:10 -07:00
|
|
|
"github.com/jinzhu/copier"
|
2024-08-23 17:04:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MapStructList maps a list of source structs to a list of destination structs
|
2025-06-29 06:01:10 -07:00
|
|
|
func MapStructList[S any, D any](source []S, destination *[]D) (err error) {
|
|
|
|
|
*destination = make([]D, len(source))
|
2024-08-24 12:44:02 +02:00
|
|
|
|
2025-06-29 06:01:10 -07:00
|
|
|
for i, item := range source {
|
|
|
|
|
err = MapStruct(item, &((*destination)[i]))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to map field %d: %w", i, err)
|
2024-08-23 17:04:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MapStruct maps a source struct to a destination struct
|
2025-06-29 06:01:10 -07:00
|
|
|
func MapStruct(source any, destination any) error {
|
|
|
|
|
return copier.CopyWithOption(destination, source, copier.Option{
|
|
|
|
|
DeepCopy: true,
|
|
|
|
|
})
|
2024-08-23 17:04:19 +02:00
|
|
|
}
|