Note: the provided example is just to highlight the problem, but it's not the good example for it, I'll provide more convenient example (and more likely to happen) at the bottom of this issue description.
Description
When copying from source of type int to destination of type int but smaller size like int8, the result is not as expected.
The value of source is set to destination but because of the limited size of the destination, the value of source may overlap (if larger than the maximum size of destination), so that the value of destination become unexpected.
package main
import (
"fmt"
"github.com/jinzhu/copier"
)
type Exam struct {
Name string
Date string
Subject string
School string
NbOfStudents int8
}
func main() {
data := struct {
Name string
Date string
Subject string
School string
NbOfStudents int
}{
Name: "<NAME>",
Date: "2020-01-01",
Subject: "Maths",
School: "University of Lille",
NbOfStudents: 1250,
}
var e Exam
err := copier.Copy(&e, data)
if err != nil {
panic(err)
}
fmt.Printf("exam: %+v\n", e)
fmt.Printf("nb of students: %d\n", e.NbOfStudents) // output: -30
}
Reproducible Example
Note: the provided example is just to highlight the problem, but it's not the good example for it, I'll provide more convenient example (and more likely to happen) at the bottom of this issue description.
Description
When copying from source of type
int
to destination of typeint
but smaller size likeint8
, the result is not as expected. The value of source is set to destination but because of the limited size of the destination, the value of source may overlap (if larger than the maximum size of destination), so that the value of destination become unexpected.