8000 oneof case is lost after using toObject · Issue #675 · grpc/grpc-web · GitHub
[go: up one dir, main page]

Skip to content
oneof case is lost after using toObject #675
@B-Reif

Description

@B-Reif

Suppose we have the following message:

message Example {
  oneof kind {
    int64 int = 1;
    double double = 2;
    string string = 3;
  }
}

The client's class definition has a method getKindCase (delegates to google-protobuf):

proto.pb.Metadata.prototype.getKindCase = function() {
  return (jspb.Message.computeOneofCase(this, proto.pb.Example.oneofGroups_[0]));
};

The client's generated toObject function looks something like this:

proto.pb.Example.toObject = function(includeInstance, msg) {
  var f, obj = {
    pb_int: jspb.Message.getFieldWithDefault(msg, 1, 0),
    pb_double: jspb.Message.getFloatingPointFieldWithDefault(msg, 2, 0.0),
    string: jspb.Message.getFieldWithDefault(msg, 3, ""),
  };

  if (includeInstance) {
    obj.$jspbMessageInstance = msg;
  }
  return obj;
};

After converting our Example to an object, we lose the ability to determine which case it belongs to. To use the resulting Example.AsObject, we need to either

  • capture the case before converting to object and track it separately,
  • mutate the resulting object (and, in TS, type it separately), or
  • sift through empty case values later (possibly incorrect when the correct case is empty).

My current solution is to make my own type and pass the two around together:

type KindedExample = {
  example: Example.AsObject;
  kindCase: Example.KindCase;
}

This is somewhat inconvenient. It would be preferable to have the case available on the generated object.

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

      0