-
Notifications
You must be signed in to change notification settings - Fork 55
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
Converting json to simple types #487
Comments
We can do this now with:
except we may want some more conversions (e.g. between numbers and strings), but we probably want those with We also have that the type of E is lax, so one approach is to make
then this turns into
So in the above case, the code would be simply:
Note that if we were dealing with strongly typed data rather than just json, you could simply write:
so it’s not such a stretch to make lax typing do this. Also note that |
As per the above comment, I assume that the following is also supported. As per the definition, this syntax works for any
If that is the case, the following should also be supported:
One concern here is that the expression |
I would say |
@sameerajayasoma Your example before you edited was a good one, which I want to work:
|
We should probably make |
According to @jclark's comment #487 (comment), any expression whose inferred type-descriptor is By making
|
Revised proposal following discussions today. Add new langlib function to lang.value:
Then given an expression
then this turns into
So the original case can be written like this: if orderPayload is json {
string orderId = check orderPayload.Order.ID;
} On the other hand, this
wouldn't work (it would get a compile-time error). And it shouldn't work because it's too much for |
We are OK with this, but prefer |
Is this a valid scenario? if orderPayload is json {
string|int orderId = check orderPayload.Order.ID;
} |
@chiranSachintha Yes: |
Consider the example in #350.
Because of lax static typing, the type of
orderPayload.Order.ID
isjson|error
.Apart from the problem mentioned in #350, there's another problem with using
toString
here: iforderPayload.Order.ID
is a map or an array, we want to get an error, not try to convert the map or array to a string. If it's a number, string, boolean or()
, it's not unreasonable to convert to a string usingtoString
; an error would also be OK in these case.There's currently no easy way to do this properly. Using a cast
<string>
has other problems. Not only will it hide the error, but it will also panic if it's not a string, rather than returning an error.This problem applies not just to conversion to string but conversion to any simple type.
For numbers, this relates to #8.
A consistent way to do this would be for
lang.string
to provide:But this feels too inconvenient for such a common use case.
The text was updated successfully, but these errors were encountered: