Skip to content
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

Deserializing feed: missing optional fields return 0 value in node.js #53

Open
nselikoff opened this issue Jul 31, 2019 · 2 comments
Open

Comments

@nselikoff
Copy link

Given a feed that does NOT have an optional field (e.g. vehicle speed), the value returned when getting this value is 0. So there's no way to differentiate "not included" from an actual value of zero.

const feed = realtime.transit_realtime.FeedMessage.decode(protobuf);
feed.entity.forEach((entity) => {
  if (entity.vehicle) {
    const speed = entity.vehicle.position.speed; // speed is 0
  }
});

This may be similar to #52

@barbeau
Copy link
Member

barbeau commented Aug 1, 2019

@nselikoff Thanks for the report! In Java bindings there is a hasSpeed() method that you can call to determine if the field has been set, but I'm not as familiar with the Node.js bindings. I'm assuming there isn't an equivalent?

@nselikoff
Copy link
Author

@barbeau good question - just looked and there is not a similar method. However, I found a workaround using realtime.transit_realtime.Position.toObject:

const feed = realtime.transit_realtime.FeedMessage.decode(protobuf);
feed.entity.forEach((entity) => {
  if (entity.vehicle) {
    // using `toObject` only adds properties that exist in the protobuf
    const position = realtime.transit_realtime.Position.toObject(entity.vehicle.position);
    // so speed will be undefined if not included in the protobuf
    const speed = position.speed; 
    // maybe you want it null
    // const speed = position.speed || null;
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants