Skip to content

Return null for missing optional field? #333

@zz85

Description

@zz85

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.

table File {
        size: uint;
}

root_type File;

and the generated JS code.

/**
 * @returns {number}
 */
File.prototype.size = function() {
  var offset = this.bb.__offset(this.bb_pos, 6);
  return offset ? this.bb.readUint32(this.bb_pos + offset) : 0;
};

imho, i would prefer it to be the following (simply replace 0 with null when no offset is found)

File.prototype.size = function() {
  var offset = this.bb.__offset(this.bb_pos, 6);
  return offset ? this.bb.readUint32(this.bb_pos + offset) : null;
};

cc @evanw (nice work on the js bindings btw!)

update: I observe a similar behaviour with arrays too. a missing field for [children] would return null in .children() but 0 in .childLength().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions