-
Notifications
You must be signed in to change notification settings - Fork 42
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
Fix JSON Parsing #28
Fix JSON Parsing #28
Conversation
Are we mixing errors and values with no type indication? |
Ish. cmdkit.Error is the kind of error a command returns when it fails, so it is one form of result a command can give. That's why it's treated like a regular value. |
Yes but we have no type information. I could just as easily decide to return a value with the same fields as an error. Is there any way to wrap this in a type? I.e., wrap it in an object that contains a |
I agree that having type hints would be great. That's why I added a |
That doesn't help. Try: > ipfs dag get zdpuAxiFRrxtmMmFRZx145LufQsKTw8is1ZgfTQX4vgzgb8DB |
I get Regarding the CI complaining: That will be fixed with cmds0.5 which is well on the way. |
That is, there are valid IPLD objects that will cause the API to return an error. However, I understand that it's a bit late to fix this in API v0. It's just frustrating as this shouldn't have happened. |
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.
As a fix, this looks good to me.
order []reflect.Type | ||
type MaybeError struct { | ||
Value interface{} // needs to be a pointer | ||
Error cmdkit.Error |
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.
can we not assume that it's one or the other? (i.e., if Error is nil, it's a value)?
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.
hmm, Error
is a struct so I can't check for nil
. I could check if the message is empty but that seems like a bad hack. Checking whether Value
is nil won't work because it's supposed to be set to a non-nil value by the user. So inferring whether we have an error or not is not possible in a clean way without isError
.
I think the danger of incosistencies (i.e. isError
is false but we in fact do have an error) is very small, so I'd like to stick with it.
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 see. Sounds good.
vIsNil, isNilOk := isNil(v) | ||
vIsZero, isZeroOk := isZero(v, t) | ||
|
||
nilish := (isNilOk && vIsNil) || (isZeroOk && vIsZero) |
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.
It looks like we do actually need this logic.
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 get it. Why? nevermind.
@Stebalien reported a panic here that is caused by the cmds layer to not correctly parse a value that results in a zero value.
The problem is that I tried to make a type that takes a set of
interface{}
values, tries to decode the JSON into each of them and returns the first one that doesn't throw an error or result in a zero value. If this doesn't work for any of the supplied types, instead unmarshal into amap[string]interface{}
.This approach obviously fails if we want to parse a value that actually results in a zero value. This is what happens in the issue mentioned above.
So instead I made a type that expects either a cmdkit.Error or a value, and unmarshals into the value if unmarshaling into an error fails.
I used this version of go-ipfs-cmds to build go-ipfs and the panic does not occur anymore.
If the PR is accepted I'll squash and gx publish.
Regarding the tests, CI will only work after at cmds0.5 is done, which I'm working on when I'm not busy fixing panics ;)