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

Optional Field Updates using null.v4 #29

Open
naftulikay opened this issue Aug 28, 2021 · 0 comments
Open

Optional Field Updates using null.v4 #29

naftulikay opened this issue Aug 28, 2021 · 0 comments

Comments

@naftulikay
Copy link

So far, I'm benefiting a lot from this package, so thank you for the excellent work so far.

I mentioned this in #26 but this seems to merit its own issue.

In my REST API, I accept PATCH requests, which have all optional fields, so that you can update only what you'd like to:

type UpdateNewsRequest struct {
    Title null.String
    Description null.String
}

type News struct {
    ID uint
    Title string
    Description string
}

I'm converting it like so:

httpModel := UpdateNewsRequest {
    Title: null.StringFrom("new title")
}

dbModel := DatabaseModel {
    // fields...
   Title: "old title"
   Description: "leave me alone"
}

if err := deepcopier.Copy(&httpModel).To(&dbModel); err != nil {
    t.Fatalf("Could not deep copy: %s", err)
}

assert.Equal(t, httpModel.Title.ValueOrZero(), dbModel.Title)
assert.Equal(t, "leave me alone", dbModel.Description)

This currently fails at the first assertion, because deepcopier did not copy UpdateNewsRequest.Title, presumably because it is a null.String and deepcopier does not understand it.

I'm wondering how much work it would be to integrate this optional update feature, I'll have to look at the codebase and see if I can implement this. Alternatively, if there is a way using the context feature to accomplish this, that would be wonderful, but I don't exactly understand what the context feature does from trying it out.

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

1 participant