I am trying to write a rest endpoint to update a resource but struggling with parsing JSON and then unmarshaling it to a struct.
Let's say this is my struct:
type Car struct {
Make string `json:"make"`
Model string `json:"model"`
Mileage int `json:"mileage"`
Dealer string `json:"Dealer"`
}
I know that I could make the types pointers to avoid this issue by the way, but then that breaks my JSON responses where nil turns into null
Anyway, let's say an update call is made but the json from the client is
{
"dealer": "Jim's Cars"
}
If I unmarshal this incoming JSON against car Car, then car.Mileage will be 0.
This is an issue when it comes time to do an update on the database side because I don't want to update the mileage to 0.
Is there a way to do ensure that if the user didn't send mileage that car.Mileage is not 0?