-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Return null for missing optional field? #333
Comments
I agree that it's nice to be able to know if a field is present or not. However, that doesn't appear to be how the flatbuffers spec is designed, so I think the generated JavaScript code is working as intended. I'm looking at http://google.github.io/flatbuffers/md__schemas.html and the closest thing I can find is "non-scalar (string/vector/table) fields default to NULL when not present" although I can't find anything about scalar values defaulting to 0 when not present. Maybe @gwvo can clarify? I imagine cross-language consistency would be the desired behavior here. In our project (C++ code), we solved this problem by using invalid sentinel values as defaults for scalar fields: 255 for bools and enums, INT_MIN for ints, and FLT_MIN for floats. |
Yes, this is how all languages work, and so should JS. Returning null for a scalar would be bad, since the field not being present means it is equal to the default, not that it wasn't set. For example, if your field is defined a |
Hmm, that's an interesting behaviour. In which case, I'm interested to know if there could be a null default for integers and whether it make any sense for have an api which returns whether the field is default? Otherwise, I think Evan's approach seems reasonable for most cases, or an additional field could be defined to indicate the presence of a field. |
The easiest way to get a null default for an integer field is to wrap it in a struct:
this will get you null if scalar isn't present. It also doesn't take up any more space on the wire than a regular int. |
That mighht change the way the code is called, but thanks for the suggestion! |
This functionality is now in: This allows you to call e.g. mymonster.CheckField(Monster::VT_HEALTH) to see if a field was stored at all. Note the caveat I mentioned still applies: you won't be able to tell the difference between a field that wasn't written vs a field that happens to have the default value this way, unless you use force_defaults. This is intrinsic to how FlatBuffers works. |
Table::CheckField is not visible to clients of the table object. |
Use |
Ah, excellent. I missed that. |
Yes, never mind. I see that in the code comments. |
I'm trying out the javascript generated flatbuffer code when I observe that optional string fields return null if the field doesn't exist while missing field for uint returns 0. I would think that it's useful to know when a field is null(missing) instead of returning 0 (which is a valid number).
Is this the intended behaviour? A quick check on the generated python and java classes shows that they return null for missing string fields but 0 on missing integers too.
To give a reduced use case, here's the idl.
and the generated JS code.
imho, i would prefer it to be the following (simply replace 0 with null when no offset is found)
cc @evanw (nice work on the js bindings btw!)
update: I observe a similar behaviour with arrays too. a missing field for
[children]
would returnnull
in.children()
but 0 in.childLength()
.The text was updated successfully, but these errors were encountered: