Skip to content
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

exchanges/margin: Fix marshalling issue #1812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gloriousCode
Copy link
Collaborator

PR Description

Addresses issue #1810

There is a bug from a lack of MarshalJSON implementation. When unmarshalling and marshalling an order.Submit for example, it will fail from being unable to parse a string into a margin.Type

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and
also consider improving test coverage whilst working on a certain feature or package.

  • go test ./... -race
  • golangci-lint run
  • TestUnmarshalOrder
  • TestMarshalJSON

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Github Actions with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK. Was wondering about the delimiter and maybe we should add in one in a marshaller for Submit if missing between, but out of scope.

@thrasher- thrasher- changed the title Bug: Margin: Fix marshalling issue exchanges/margin: Fix marshalling issue Feb 24, 2025
@thrasher- thrasher- requested review from gbjk and thrasher- February 24, 2025 05:15
@thrasher- thrasher- added the review me This pull request is ready for review label Feb 24, 2025
@shazbert shazbert added the szrc shazbert review complete label Feb 26, 2025
Copy link
Collaborator

@gbjk gbjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me, nice work 👍

I've raised nits, but none of them stop this being merged as-is.

@@ -23,6 +23,11 @@ func (t *Type) UnmarshalJSON(d []byte) error {
return err
}

// MarshalJSON conforms type to the Marshaler interface
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
// MarshalJSON conforms type to the Marshaler interface
// MarshalJSON conforms type to the json.Marshaler interface

I'd also argue for implements over conforms, but that's getting picky.

Comment on lines +64 to +69
t.Run(tc.want, func(t *testing.T) {
t.Parallel()
resp, err := json.Marshal(tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, string(resp))
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opinionated Nit:
As someone who runs tests verbose often, I really don't like having t.Run for trivial cases like this, over just adding tc.want to the end of the 2 checks, because of the increased output if we're always doing this.
I feel like we want t.Run for tests which are going to take a lot of time, or where we might want to -run them individually.

The reason I raise this (again) is because if we keep adding it for ubiqutous methods like TestMarshal or TestString then we'll get a lot of this:

=== RUN   TestMarshalJSON
=== PAUSE TestMarshalJSON
=== CONT  TestMarshalJSON
=== RUN   TestMarshalJSON/"isolated"
=== PAUSE TestMarshalJSON/"isolated"
=== RUN   TestMarshalJSON/"multi"
=== PAUSE TestMarshalJSON/"multi"
=== RUN   TestMarshalJSON/"cash"
=== PAUSE TestMarshalJSON/"cash"
=== RUN   TestMarshalJSON/"spot_isolated"
=== PAUSE TestMarshalJSON/"spot_isolated"
=== RUN   TestMarshalJSON/"unknown"
=== PAUSE TestMarshalJSON/"unknown"
=== RUN   TestMarshalJSON/""
=== PAUSE TestMarshalJSON/""
=== CONT  TestMarshalJSON/"isolated"
=== CONT  TestMarshalJSON/"cash"
=== CONT  TestMarshalJSON/"multi"
=== CONT  TestMarshalJSON/"spot_isolated"
=== CONT  TestMarshalJSON/""
=== CONT  TestMarshalJSON/"unknown"
--- PASS: TestMarshalJSON (0.00s)
    --- PASS: TestMarshalJSON/"isolated" (0.00s)
    --- PASS: TestMarshalJSON/"cash" (0.00s)
    --- PASS: TestMarshalJSON/"multi" (0.00s)
    --- PASS: TestMarshalJSON/"spot_isolated" (0.00s)
    --- PASS: TestMarshalJSON/"" (0.00s)
    --- PASS: TestMarshalJSON/"unknown" (0.00s)
PASS
ok      github.com/thrasher-corp/gocryptotrader/exchanges/margin        0.519s

And it'll overwhelm scrollback and make grepping the test output harder.

Price: 1000,
}
jOrderSubmit, err := json.Marshal(orderSubmit)
require.NoError(t, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.NoError(t, err)
require.NoError(t, err, "json.Marshal must not error")

Comment on lines +2216 to +2222
// Margin-type regression test
orderSubmit.MarginType = margin.Multi
jOrderSubmit, err = json.Marshal(orderSubmit)
require.NoError(t, err)
err = json.Unmarshal(jOrderSubmit, &os2)
require.NoError(t, err)
require.Equal(t, orderSubmit, os2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits:

  • Think the regression can be moved into the reason we do the test
  • But actually, I don't think we should have this regression test here (in isolation of any other field tests); We didn't fix anything in order.Submit at all, so the regression wouldn't be here.
  • Also: Am I missing why we couldn't have just set MarginType in the original orderSumbit and let L2215 handle this?
Suggested change
// Margin-type regression test
orderSubmit.MarginType = margin.Multi
jOrderSubmit, err = json.Marshal(orderSubmit)
require.NoError(t, err)
err = json.Unmarshal(jOrderSubmit, &os2)
require.NoError(t, err)
require.Equal(t, orderSubmit, os2)
orderSubmit.MarginType = margin.Multi
jOrderSubmit, err = json.Marshal(orderSubmit)
require.NoError(t, err, "json.Marshal must not error")
err = json.Unmarshal(jOrderSubmit, &os2)
require.NoError(t, err)
require.Equal(t, orderSubmit, os2, "order.Submit must unmarshal Margin.Type correctly")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug review me This pull request is ready for review szrc shazbert review complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

json.Marshal order.Submit json.Unmarshal marginType error
4 participants