Skip to content

Commit

Permalink
Fix SetRequestCallbacks/SetResponseCallbacks (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
averche committed Oct 19, 2023
1 parent fd98162 commit ae2cb1b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
7 changes: 4 additions & 3 deletions client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vault

import (
"fmt"
"net/http"
"testing"
"time"

Expand All @@ -27,6 +28,12 @@ func Test_Client_Clone(t *testing.T) {

require.NoError(t, client.SetToken("test-token"))
require.NoError(t, client.SetNamespace("test-namespace"))
require.NoError(t, client.SetRequestCallbacks(func(req *http.Request) {
t.Log(req)
}))
require.NoError(t, client.SetResponseCallbacks(func(req *http.Request, resp *http.Response) {
t.Log(req, resp)
}))

clone := client.Clone()

Expand Down
7 changes: 4 additions & 3 deletions generate/templates/client.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"net/url"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -180,8 +181,8 @@ func (c *Client) cloneClientRequestModifiers() requestModifiers {

var clone requestModifiers

copy(clone.requestCallbacks, c.clientRequestModifiers.requestCallbacks)
copy(clone.responseCallbacks, c.clientRequestModifiers.responseCallbacks)
clone.requestCallbacks = slices.Clone(c.clientRequestModifiers.requestCallbacks)
clone.responseCallbacks = slices.Clone(c.clientRequestModifiers.responseCallbacks)

clone.headers = requestHeaders{
userAgent: c.clientRequestModifiers.headers.userAgent,
Expand All @@ -192,7 +193,7 @@ func (c *Client) cloneClientRequestModifiers() requestModifiers {
customHeaders: c.clientRequestModifiers.headers.customHeaders.Clone(),
}

copy(clone.headers.mfaCredentials, c.clientRequestModifiers.headers.mfaCredentials)
clone.headers.mfaCredentials = slices.Clone(c.clientRequestModifiers.headers.mfaCredentials)

return clone
}
Expand Down
4 changes: 2 additions & 2 deletions replication_consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -228,8 +229,7 @@ func (c *replicationStateCache) clone() replicationStateCache {
/* */ c.statesLock.RLock()
defer c.statesLock.RUnlock()

var cloned []string
copy(cloned, c.states)
cloned := slices.Clone(c.states)

return replicationStateCache{
statesLock: sync.RWMutex{},
Expand Down
5 changes: 3 additions & 2 deletions request_modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"maps"
"net/http"
"net/url"
"slices"
"strings"
"time"
"unicode"
Expand Down Expand Up @@ -172,7 +173,7 @@ func (c *Client) ClearCustomHeaders() {
// SetRequestCallbacks sets callbacks which will be invoked before each request.
func (c *Client) SetRequestCallbacks(callbacks ...RequestCallback) error {
c.clientRequestModifiersLock.Lock()
copy(c.clientRequestModifiers.requestCallbacks, callbacks)
c.clientRequestModifiers.requestCallbacks = slices.Clone(callbacks)
c.clientRequestModifiersLock.Unlock()

return nil
Expand All @@ -189,7 +190,7 @@ func (c *Client) ClearRequestCallbacks() {
// successful response.
func (c *Client) SetResponseCallbacks(callbacks ...ResponseCallback) error {
c.clientRequestModifiersLock.Lock()
copy(c.clientRequestModifiers.responseCallbacks, callbacks)
c.clientRequestModifiers.responseCallbacks = slices.Clone(callbacks)
c.clientRequestModifiersLock.Unlock()

return nil
Expand Down

0 comments on commit ae2cb1b

Please sign in to comment.