mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Websocket: Restructure files and types (#1859)
* Websocket: Rename stream package * Websocket: Rename Websocket to Manager * Websocket: Replace explicit errs with common.NilGuard * Websocket: Move websocket_types.go to types.go * Websocket: Minor field comment and alignment in types * Webosocket: Rename WebsocketConnection to Connection * Alphapoint: Make gorilla ws import explicit Just to avoid confusion with our own packages. * Websocket: Move stream_match to match * Websocket: Move websocket_connection to connection * Websocket: Move websocket.go to manager.go * Websocket: Break out all subscription methods into subscriptions.go * Websocket: Move connection type into its file * Websocket: Remove PositionUpdated Type is not used anywhere * Kraken: Use local constant for pong Was the only use of websocket.Pong and doesn't really feel right to represent kraken's api resp in one of our packages * Websocket: Move connection sub-types to connection package * Websocket: Move manager types into manager * Websocket: Move ConnectionWrapper into manager * Websocket: Move websocket_test to manager_test * Websocket: Privatise connectionWrapper * Websocket: Remaining types into types.go These really belong somewhere else mostly, but this will do for now * Websocket: Tidy up connection method vars * Gofumpt: Moving package imports around * Websocket: Rename errDuplicateConnectionSetup * Websocket: Fix duplicate import of gws * Websocket: Fix gofumpt -extra * Websocket: Standardise import of gws across other pkgs * Kraken: Remove unused sub conf consts These were replaced by the generic Levels and Depth fields on all subs * Websocket: Privitise ConnectioWrapper fields * Websocket: inline single use var WebsocketNotAuthenticatedUsingRest * Websocket: Move documentation to template * Bithumb: Assertify TestWsHandleData
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unicode"
|
||||
"unsafe"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/common/file"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
@@ -80,6 +81,22 @@ var (
|
||||
errHTTPClientInvalid = errors.New("custom http client cannot be nil")
|
||||
)
|
||||
|
||||
// NilGuard returns an ErrNilPointer with the type of the first nil argument
|
||||
func NilGuard(ptrs ...any) (errs error) {
|
||||
for _, p := range ptrs {
|
||||
/* Internally interfaces contain a type and a value address
|
||||
Obviously can't compare to nil, since the types won't match, so we look into the interface
|
||||
eface is the internal representation of any; e(mpty-inter)face
|
||||
See: https://cs.opensource.google/go/go/+/refs/tags/go1.24.1:src/runtime/runtime2.go;l=184-187
|
||||
We optimize here by converting to [2]uintptr and just checking the address, instead of casting to a local eface type
|
||||
*/
|
||||
if (*[2]uintptr)(unsafe.Pointer(&p))[1] == 0 {
|
||||
errs = AppendError(errs, fmt.Errorf("%w: %T", ErrNilPointer, p))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
// MatchesEmailPattern ensures that the string is an email address by regexp check
|
||||
func MatchesEmailPattern(value string) bool {
|
||||
if len(value) < 3 || len(value) > 254 {
|
||||
|
||||
@@ -722,3 +722,26 @@ func BenchmarkCounter(b *testing.B) {
|
||||
c.IncrementAndGet()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilGuard(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := NilGuard((*int)(nil))
|
||||
assert.ErrorIs(t, err, ErrNilPointer)
|
||||
assert.ErrorContains(t, err, "*int")
|
||||
|
||||
s := "normal input"
|
||||
err = NilGuard(&s, 2, &[]int{4, 5, 6}, []int{1, 2, 3}, new(A))
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = NilGuard(&s, nil, (*int)(nil))
|
||||
assert.ErrorIs(t, err, ErrNilPointer)
|
||||
assert.ErrorContains(t, err, "*int")
|
||||
var mErr *multiError
|
||||
require.ErrorAs(t, err, &mErr, "err must be a multiError")
|
||||
assert.Len(t, mErr.Unwrap(), 2, "Should get 2 errors back")
|
||||
|
||||
assert.ErrorIs(t, NilGuard(nil), ErrNilPointer, "Unusual input of an untyped nil should still error correctly")
|
||||
|
||||
err = NilGuard()
|
||||
require.NoError(t, err, "NilGuard with no arguments should not panic")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user