flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

How to judge a field ( primitive type ) is already set value in flatbuffer ?

Open ghost opened this issue 11 years ago • 9 comments

From the website https://google.github.io/flatbuffers/md__schemas.html , I got the message about If you don't assign a value to a field. it defaults to 0 or NULL. But If I assign a value of 0 to a field (numberOfStudent) . And it's not the default value, they are equal though.So ,How can I judge this filed and then has already assigned ?

Compared to protobuf, I can use boolean hasNumberOfStudent() method to judge that.But In FlatBuffer ? What can I do ? Thank you.

ghost avatar Oct 28 '15 07:10 ghost

No, the field's default value defaults to 0, and any field instance defaults to the default value :) Confusing, I know.

If you set a field to 0 whose default value is not 0, then the 0 value will be stored inside the FlatBuffer. I'm not sure what you mean by "already assigned".

We have a plan to add a has() method, yes, but it's not there yet. For now you'd have to compare against the default value.

ghost avatar Oct 28 '15 18:10 ghost

@gwvo , you say , I am not sure "already assigned". Ok, I mean if I have a field of numOfStudent in the table Class. Just like below schema. I set this filed to 0 and default value is 0 too even if I don't set this value to 0. How can I judge this filed has already set ? This filed has already assigned by myself. Not the default value. Although they are equal. So, I should use hasNumOfStudent() method to judge. But FlatBuffer don't support it. If the field is not primitive type, so I can judge whether it is NULL to judge that. To primitive type, I am confusing. For 0, it doesn't mean that it does not exist. But NULL can show this filed is not exist. Do you understand what I mean yet?

table Class {
      numOfStudent : short = 0;
}

ghost avatar Oct 29 '15 00:10 ghost

You generally can't, there's no difference in FlatBuffers for a table in which ClassBuilder::addNumOfStudent has been called, and one which it hasn't. The idea is that you shouldn't need to. The value is 0, regardless of wether it is stored or set. Even if we add a hasNumOfStudent() method, it will return false if the value is 0.

ghost avatar Nov 03 '15 01:11 ghost

@gwvo OK, It seems look I just judge that by whether it is 0 or not. but Thanks anyway.

ghost avatar Nov 03 '15 02:11 ghost

Hi @gwvo , I think the question here is that is there a way to tell whether an optional field exists? Checking the default value won't help, because the user still doesn't know if the field hasn't been set, or it's just set to the default value. Thanks.

lirui-apache avatar Nov 05 '15 09:11 lirui-apache

@lirui-intel : yes, that's what my previous comment answers. There's no way to tell the difference between a field that has not been set and a field that has been set and has the default value, because they are both not stored in the wire format.

ghost avatar Nov 06 '15 18:11 ghost

I see. Thanks @gwvo .

lirui-apache avatar Nov 12 '15 04:11 lirui-apache

This functionality is now in: https://github.com/google/flatbuffers/commit/1fa803d187c244e8de0bb40f0d3c91f22408bde0

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.

ghost avatar Nov 30 '15 22:11 ghost

OK, thanks a lot.

ghost avatar Dec 18 '15 15:12 ghost