Files
pocket-id-pocket-id-2/backend/internal/dto/dto_mapper.go

28 lines
634 B
Go
Raw Normal View History

2024-08-23 17:04:19 +02:00
package dto
import (
"fmt"
"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
func MapStructList[S any, D any](source []S, destination *[]D) (err error) {
*destination = make([]D, len(source))
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
func MapStruct(source any, destination any) error {
return copier.CopyWithOption(destination, source, copier.Option{
DeepCopy: true,
})
2024-08-23 17:04:19 +02:00
}