-
Notifications
You must be signed in to change notification settings - Fork 124
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
Conversation
WalkthroughThe changes primarily involve the introduction of a new function Changes
TipsChat with CodeRabbit Bot (
|
d2afcf1
to
7746c8d
Compare
There was a problem hiding this 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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) | ||
} |
There was a problem hiding this comment.
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)
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 | |
} |
No description provided.