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

[DEC-2159] - add generic json printer to help format customtypes in protos #702

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions protocol/lib/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lib

import (
"encoding/json"
"fmt"
)

// MaybeGetStructJsonString returns the json representation of a struct, or a formatted string using
// %+v if the json conversion encounters an error.
func MaybeGetJsonString(i interface{}) string {
jsonData, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("%+v", i)
}

return string(jsonData)
}
Comment on lines +1 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

The function MaybeGetJsonString is well implemented. It tries to convert an interface to a JSON string and if it fails, it returns a formatted string. It also handles nil inputs correctly. However, it's important to note that the error from json.Marshal is silently ignored. While this might be acceptable in some cases, it could lead to unexpected behavior if the input is not serializable and the caller is not aware of this. Consider logging the error or returning it to the caller to handle.

- func MaybeGetJsonString(i interface{}) string {
+ func MaybeGetJsonString(i interface{}) (string, error) {
	jsonData, err := json.Marshal(i)
	if err != nil {
		return fmt.Sprintf("%+v", i), err
	}
	return string(jsonData), nil
}
Committable suggestion (Beta)
Suggested change
package lib
import (
"encoding/json"
"fmt"
)
// MaybeGetStructJsonString returns the json representation of a struct, or a formatted string using
// %+v if the json conversion encounters an error.
func MaybeGetJsonString(i interface{}) string {
jsonData, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("%+v", i)
}
return string(jsonData)
}
func MaybeGetJsonString(i interface{}) (string, error) {
jsonData, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("%+v", i), err
}
return string(jsonData), nil
}

11 changes: 5 additions & 6 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
errorsmod "cosmossdk.io/errors"

gometrics "github.com/armon/go-metrics"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -270,8 +269,8 @@ func (k Keeper) OffsetSubaccountPerpetualPosition(
"checkTx", ctx.IsCheckTx(),
"perpetualId", perpetualId,
"deltaQuantums", deltaQuantums,
"liquidatedSubaccount", log.NewLazySprintf("%+v", liquidatedSubaccount),
"offsettingSubaccount", log.NewLazySprintf("%+v", offsettingSubaccount),
"liquidatedSubaccount", liquidatedSubaccount,
"offsettingSubaccount", offsettingSubaccount,
)
numSubaccountsWithNonOverlappingBankruptcyPrices++
}
Expand Down Expand Up @@ -346,9 +345,9 @@ func (k Keeper) ProcessDeleveraging(
offsettingPositionQuantums.CmpAbs(deltaQuantums) == -1 {
return errorsmod.Wrapf(
types.ErrInvalidPerpetualPositionSizeDelta,
"ProcessDeleveraging: liquidated = (%+v), offsetting = (%+v), perpetual id = (%d), deltaQuantums = (%+v)",
liquidatedSubaccount,
offsettingSubaccount,
"ProcessDeleveraging: liquidated = (%s), offsetting = (%s), perpetual id = (%d), deltaQuantums = (%+v)",
lib.MaybeGetJsonString(liquidatedSubaccount),
lib.MaybeGetJsonString(offsettingSubaccount),
perpetualId,
deltaQuantums,
)
Expand Down