Skip to content

Commit

Permalink
Subs: Conditional Marshalling of pair
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 8, 2023
1 parent c5c2027 commit e82e898
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions exchanges/kline/kline.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func (i *Interval) UnmarshalText(text []byte) error {
}

// MarshalText implements the TextMarshaler interface for Intervals
func (i *Interval) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
func (i Interval) MarshalText() ([]byte, error) {
return []byte(i.Short()), nil
}

// addPadding inserts padding time aligned when exchanges do not supply all data
Expand Down
2 changes: 1 addition & 1 deletion exchanges/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Subscription struct {
// MarshalJSON generates a JSON representation of a Subscription, specifically for config writing
// The only reason it exists is to avoid having to make Pair a pointer, since that would be generally painful
// If Pair becomes a pointer, this method is redundant and should be removed
func (s *Subscription) MarshalJSON() ([]byte, error) {
func (s Subscription) MarshalJSON() ([]byte, error) {
// None of the usual type embedding tricks seem to work for not emitting an nil Pair
// The embedded type's Pair always fills the empty value
type MaybePair struct {
Expand Down
24 changes: 12 additions & 12 deletions exchanges/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ import (
func TestEnsureKeyed(t *testing.T) {
t.Parallel()
c := Subscription{
Channel: "candles",
Asset: asset.Spot,
Currency: currency.NewPair(currency.BTC, currency.USDT),
Channel: "candles",
Asset: asset.Spot,
Pair: currency.NewPair(currency.BTC, currency.USDT),
}
k1, ok := c.EnsureKeyed().(DefaultKey)
if assert.True(t, ok, "EnsureKeyed should return a DefaultKey") {
assert.Exactly(t, k1, c.Key, "EnsureKeyed should set the same key")
assert.Equal(t, k1.Channel, c.Channel, "DefaultKey channel should be correct")
assert.Equal(t, k1.Asset, c.Asset, "DefaultKey asset should be correct")
assert.Equal(t, k1.Currency, c.Currency, "DefaultKey currency should be correct")
assert.Equal(t, k1.Pair, c.Pair, "DefaultKey currency should be correct")
}
type platypus string
c = Subscription{
Key: platypus("Gerald"),
Channel: "orderbook",
Asset: asset.Margin,
Currency: currency.NewPair(currency.ETH, currency.USDC),
Key: platypus("Gerald"),
Channel: "orderbook",
Asset: asset.Margin,
Pair: currency.NewPair(currency.ETH, currency.USDC),
}
k2, ok := c.EnsureKeyed().(platypus)
if assert.True(t, ok, "EnsureKeyed should return a platypus") {
Expand All @@ -42,19 +42,19 @@ func TestEnsureKeyed(t *testing.T) {
// TestMarshalling logic test
func TestMarshaling(t *testing.T) {
t.Parallel()
j, err := json.Marshal(&Subscription{Channel: CandlesChannel})
j, err := json.Marshal(Subscription{Channel: CandlesChannel})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"candles"}`, string(j), "Marshalling should be clean and concise")

j, err = json.Marshal(&Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4})
j, err = json.Marshal(Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"orderbook","interval":"5m","levels":4}`, string(j), "Marshalling should be clean and concise")

j, err = json.Marshal(&Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4, Pair: currency.NewPair(currency.BTC, currency.USDT)})
j, err = json.Marshal(Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4, Pair: currency.NewPair(currency.BTC, currency.USDT)})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"orderbook","interval":"5m","levels":4,"pair":"BTCUSDT"}`, string(j), "Marshalling should be clean and concise")

j, err = json.Marshal(&Subscription{Channel: MyTradesChannel, Authenticated: true})
j, err = json.Marshal(Subscription{Channel: MyTradesChannel, Authenticated: true})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"myTrades","authenticated":true}`, string(j), "Marshalling should be clean and concise")
}

0 comments on commit e82e898

Please sign in to comment.