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

Conversation

jakob-dydx
Copy link
Contributor

No description provided.

@linear
Copy link

linear bot commented Oct 25, 2023

DEC-2159

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 25, 2023

Walkthrough

The changes primarily involve the introduction of a new function MaybeGetJsonString in the lib package, which attempts to convert an interface to a JSON string. If unsuccessful, it returns a formatted string. The changes also include the replacement of log.NewLazySprintf with MaybeGetJsonString in the protocol/x/clob/keeper package to handle JSON conversion.

Changes

File Summary
protocol/lib/json.go Introduced a new function MaybeGetJsonString that attempts to convert an interface to a JSON string, and returns a formatted string if the conversion fails.
protocol/x/clob/keeper/deleveraging.go Replaced log.NewLazySprintf with lib.MaybeGetJsonString for JSON conversion of liquidatedSubaccount and offsettingSubaccount variables. Updated error messages to use the new function.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

@jakob-dydx jakob-dydx force-pushed the jakob-dydx/json-printer branch from d2afcf1 to 7746c8d Compare October 25, 2023 02:25
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 65f556f and 7746c8d.
Files selected for processing (2)
  • protocol/lib/json.go (1 hunks)
  • protocol/x/clob/keeper/deleveraging.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/x/clob/keeper/deleveraging.go

protocol/lib/json.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7746c8d and e221f80.
Files selected for processing (1)
  • protocol/lib/json.go (1 hunks)

Comment on lines +1 to +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)
}
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
}

@jakob-dydx jakob-dydx merged commit e47a416 into main Oct 25, 2023
@jakob-dydx jakob-dydx deleted the jakob-dydx/json-printer branch October 25, 2023 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants