-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Handle json.RawMessage #31
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
Conversation
Without special handling, json.RawMessage is rendered as []uint8 which isn't very easy to decode. Since json.RawMessage always holds JSON text, this prints it instead so you can see the value instead.
For example, without this patch:
With the patch:
|
repr.go
Outdated
@@ -193,6 +195,11 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent | |||
} | |||
t := v.Type() | |||
|
|||
if t == rawJSONType { | |||
fmt.Fprintf(p.w, "%s", v.Bytes()) |
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.
I don't mind the idea of representing json.RawMessage differently, but I don't think this is what we want. This will make it impossible to copy+paste the output of repr and have it usable, which is one of the goals of this package.
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.
We probably want something like:
fmt.Fprintf(p.w, "json.RawMessage(%q)", v.Bytes())
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.
Ah yes, good point. I forgot about the copy-paste part. I'll fix it up later today.
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.
I opted for json.RawMessage(
%s)
instead, as it gives easier output.
If you have a string with json.RawMessage(%q)
you get json.RawMessage(\"\\\"string\\\"\")
since the quotes need to be escaped. With the backtick wrapped %s
formatter you avoid that.
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.
So, in my example, you now get:
Value: json.RawMessage(`"plain literal"`),
Whereas with %q
you'd get:
Value: json.RawMessage("\"plain literal\""),
@@ -193,6 +195,11 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent | |||
} | |||
t := v.Type() | |||
|
|||
if t == rawJSONType { | |||
fmt.Fprintf(p.w, "json.RawMessage(`%s`)", v.Bytes()) |
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.
Unfortunately this is not going to suffice - if the string contains a backtick, this will output invalid Go code.
Without special handling, json.RawMessage is rendered as []uint8 which isn't very easy to decode. Since json.RawMessage always holds JSON text, this prints it instead so you can see the value instead.