From 3c73d973f6f5253a998ad261080ff459d68236a1 Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Tue, 26 Nov 2024 11:19:13 +1100 Subject: [PATCH] order: Add `Side` type MarshalJSON method (#1726) * Add side MarshalJSON * glorious: nits * more removal * Update exchanges/order/order_test.go Co-authored-by: Adrian Gallagher --------- Co-authored-by: Ryan O'Hara-Reid Co-authored-by: Adrian Gallagher --- exchanges/order/order_test.go | 10 ++++++++++ exchanges/order/orders.go | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/exchanges/order/order_test.go b/exchanges/order/order_test.go index 2f2b8190..b01d62ae 100644 --- a/exchanges/order/order_test.go +++ b/exchanges/order/order_test.go @@ -2123,6 +2123,16 @@ func TestSideUnmarshal(t *testing.T) { assert.ErrorAs(t, s.UnmarshalJSON([]byte(`14`)), &jErr, "non-string valid json is rejected") } +func TestSideMarshalJSON(t *testing.T) { + t.Parallel() + b, err := Buy.MarshalJSON() + assert.NoError(t, err) + assert.Equal(t, `"BUY"`, string(b)) + b, err = UnknownSide.MarshalJSON() + assert.NoError(t, err) + assert.Equal(t, `"UNKNOWN"`, string(b)) +} + func TestGetTradeAmount(t *testing.T) { t.Parallel() var s *Submit diff --git a/exchanges/order/orders.go b/exchanges/order/orders.go index 5f92b802..ad09ba5d 100644 --- a/exchanges/order/orders.go +++ b/exchanges/order/orders.go @@ -1115,6 +1115,11 @@ func (s *Side) UnmarshalJSON(data []byte) (err error) { return } +// MarshalJSON returns the JSON-encoded order side +func (s Side) MarshalJSON() ([]byte, error) { + return []byte(`"` + s.String() + `"`), nil +} + // StringToOrderType for converting case insensitive order type // and returning a real Type func StringToOrderType(oType string) (Type, error) {