google / closure-templates

A client- and server-side templating system that helps you dynamically build reusable HTML and UI elements

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

{switch} with a protobuf oneof field?

pcj opened this issue · comments

A proto message definition with a oneof field:

message Foo {
   oneof content {
       Bar bar = 1;
       Baz baz = 2;
   }
}

Generates a method:

/**
 * @enum {number}
 */
proto.mypackage.Foo.ContentCase = {
  CONTENT_NOT_SET: 0,
  BAR: 1,
  BAZ: 2,
};

/**
 * @return {mypackage.Foo.ContentCase}
 */
proto.mypackage.prototype.getContentCase = function() {
  return /** @type {proto.mypackage.Foo.ContentCase} */(jspb.Message.computeOneofCase(this, proto.mypackage.Foo.oneofGroups_[0]));
};

So I was expecting I might be able to:

{template .Foo}
    {@param foo: mypackage.Foo}
  {switch $foo.contentCase}
    {case mypackage.Foo.BAR}
       ...
    {default}
      ...
  {/switch}
{/template}

But that did not seem to work. Any pointers? Are oneofs supported?

error: Undefined field 'contentCase' for proto type mypackage.Foo.
  {switch $foo.contentCase}
            ^

Same outcome for {switch $foo.content}...

There is no way to access the case field currently. you can access the oneof fields though, so the best option is to test each field for nullness using a set of if conditions.

We do want to add this feature but have not had the resources to do so.

OK thanks. I didn't think of the workaround, that should work fine.