Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non existent fields on source struct is still over ridden in destination struct #204

Open
Roccoriu opened this issue Dec 11, 2023 · 1 comment
Assignees

Comments

@Roccoriu
Copy link

Roccoriu commented Dec 11, 2023

Reproducible Example

Description

I use gorm for managing my db schema in my application. I need to copy fields from a dto to the model. The id fields are not present in the dto and should not be overriden since it only exists in the destination struct. Why are they set to a zero value? It makes absolutely no sense and should not happen


type HostnameConfig struct {
	database.Model

	OrderConfigurationID *uint `json:"-"`

	OrderConfiguration OrderConfiguration `json:"-"`
	NetworkConfigs     []NetworkConfig    `json:"networkConfigs" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignkey:HostnameConfigID"`
}

type OrderHostnameDTO struct {
	OrderConfigurationID *uint              `json:"orderConfigurationID"`
	NetworkConfigs       []NetworkConfigDTO `json:"networkConfigs"`
}


log.Printf("%v", exists[0].ID)
if err := copier.Copy(&exists, &hostnameConfigDTO); err != nil {
	return nil, err
}

log.Printf("%v", exists[0].ID)

image

This does not behave as expected. The ID field of exists gets set to after it's overridden, even if the DTO does not contain any ID fields what so ever. I tried the CopyWithOptions method, none of the options changed the described behaviour what so ever.

@Roccoriu
Copy link
Author

I've noticed that it breaks if you make use of struct embedding. Is something that can be implemented in the future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants