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

Support google.protobuf.any ? #2041

Open
derons8868 opened this issue Dec 10, 2024 · 0 comments
Open

Support google.protobuf.any ? #2041

derons8868 opened this issue Dec 10, 2024 · 0 comments

Comments

@derons8868
Copy link

protobuf.js version: <7.4.0>

How can I put any message in the "child" field just similar to google.protobuf.any ?

import {
  Message, Type, Field, OneOf,
} from 'protobufjs/light'; // respectively "./node_modules/protobufjs/light.js"

export class ChildString extends Message<ChildString> {
  @Field.d(1, 'string')
  public someString: string = '';
}
export class ChildNumber extends Message<ChildNumber> {
  @Field.d(1, 'int32')
  public someNumber?: number;
}

export class ParentMessage extends Message<ParentMessage> {
  @Field.d(1, 'string')
  public name?: string;

  @Field.d(2, Message) // Any of ChildString or ChildNumber etc...
  public child?: Message;
}

const childMessage = new ChildString();
childMessage.someString = 'Hello World';

const parentMessage = new ParentMessage();
parentMessage.name = 'John';
parentMessage.child = childMessage;

let buffer = ParentMessage.encode(parentMessage).finish();
let decodedMessage = ParentMessage.decode(buffer);
console.info('output:', decodedMessage);

const childMessage2 = new ChildNumber();
childMessage2.someNumber = 123;
parentMessage.child = childMessage2;

buffer = ParentMessage.encode(parentMessage).finish();
decodedMessage = ParentMessage.decode(buffer);
console.info('output:', decodedMessage);
>

Result

output: ParentMessage { name: 'John', child: Message {} }

output: ParentMessage { name: 'John', child: Message {} }

Both output of "child" field is empty.

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

1 participant