Skip to content

Conversation

@timostamm
Copy link
Member

Idiomatic use for initializing message fields is to give a partial object:

const user = new User({
  createTime: {nanos: 123},
});

Giving a message instance should work the same:

const user = new User({
  createTime: new Timestamp({nanos: 123}),
});

But it does not. Thanks to @paul-sachs for finding this.

@timostamm timostamm mentioned this pull request May 3, 2022
9 tasks
t[localName] = val;
} else {
t[localName] = val instanceof mt ? mt : new mt(val);
t[localName] = val instanceof mt ? val : new mt(val);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code runs when you call new MyMessage(data). We're walking through the metadata (message fields) here, and pick values from the data. If the value for a message field is already an instance of the expected message type, we can use it as is. But instead of doing that, we do something silly and use the message type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, yeah having it set to mt IS very weird. Definitely explains some other weird behaviour i was seeing.

@timostamm timostamm requested a review from paul-sachs May 3, 2022 17:13
Copy link
Member

@paul-sachs paul-sachs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that you need my approval but this certainly looks like it would solve the issue I saw.

@timostamm timostamm merged commit d8b009d into main May 3, 2022
@timostamm timostamm deleted the tstamm/fix-constructor-message-instance-field branch May 3, 2022 18:01
@timostamm timostamm mentioned this pull request May 4, 2022
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

Successfully merging this pull request may close these issues.

3 participants