-
Notifications
You must be signed in to change notification settings - Fork 716
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
Clone or copy of existing client #773
Comments
Potentially I have fallen victim to a blind spot in using pointers: I tend to always use pointers when dealing with struct types. Thus, I also always pass around Wouldn't simply passing |
Yeah, so this is pretty simple: func copyRestyClient(c *resty.Client) *resty.Client {
// dereference the pointer and copy the value
cc := *c
return &cc
} If you fancy, some tests show that this is valid: func Test_copyRestyClient(t *testing.T) {
t.Run("returns valid copy", func(t *testing.T) {
original := resty.New()
copied := copyRestyClient(original)
require.NotSame(t, original, copied)
t.Run("copy modifications do not affect original", func(t *testing.T) {
copied.JSONUnmarshal = func(data []byte, v interface{}) error {
return errors.New("this is some error")
}
require.NotSame(t, original.JSONUnmarshal, copied.JSONUnmarshal)
})
})
} What I'd like to know from a maintainer is if I am overlooking any specifics or potential bugs, if I were to go for this approach. If not and this is a valid, bug-free solution, is there interest in adding a comfort function to resty which does exactly this, something like |
This is problematic because doing so will result in
Execute go vet ./... on the piece of code you are running. Having a clone function would make sense, like |
Pinging @jeevatkm to get his opinion on if there is a recommended way of doing this. |
I will push a PR which adresses this issue. I am totally open to not merging it since the implementation is pretty crude and I don't know what the usual maintainers prefer :) |
I have a use case where my application should have a global HTTP client with sane defaults for tracing, logging and error handling and then some submodules which must apply highly specific settings for only their use case.
For that I would like to copy / clone an existing client which thus inherits all of the clients settings. In other libraries / languages this is also sometimes possible by having a child-parent relationship between clients.
What would be the best way to approach this with resty? Is there a function or common solutions for this use case?
Edit: If this is not a present feature but of interest to people, let's discuss how that could be implemented.
The text was updated successfully, but these errors were encountered: