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

Add support for non-nullable properties #120

Closed
Ky6uk opened this issue Oct 12, 2018 · 4 comments
Closed

Add support for non-nullable properties #120

Ky6uk opened this issue Oct 12, 2018 · 4 comments
Labels

Comments

@Ky6uk
Copy link

Ky6uk commented Oct 12, 2018

gogoproto.nullable annotation allow us to control property availability

For example the code

message Example {
    TestMessage test = 1 [(gogoproto.nullable) = false];
}

will generate TS

export class Example extends jspb.Message {
  getTest(): TestMessage | undefined;

  // ...
}

when getTest() actually will never return undefined. And it will require additional checks to undefined in codebase when strictNullChecks is enabled.

So can we add a custom annotation which will generate TS code without | undefined part? Or please guide me how I can control this behaviour.

@easyCZ
Copy link
Contributor

easyCZ commented Oct 12, 2018

gogoproto.nullable is a proto extension. Your usage in this context is domain specific, ie. only the receiver implementing gogoproto will know what this means. However, ts-protoc-gen does not understand this extension.

Furthermore, the fundamental proto message can still be null, it is just your particular domain that is claiming it never will be.

It might be possible to fork this repo and implement the extension. At the moment, we don't have sufficient support for plugins

@stefanorinetti
Copy link

As far as I am aware every message property in proto3 is required.

message A {
  B test = 1;
}

message B {
  string val = 1;
}

Generates the following (I'm pasting only part of it)

export class A extends jspb.Message {
  hasValue(): boolean;
  clearValue(): void;
  getValue(): B | undefined;
  setValue(value?: B): void;

  // ...
}
export namespace A {
  export type AsObject = {
    value?: B.AsObject,
  }
}

What's the deal with having | undefined in the class properties?

The same goes for the .AsObject properties.

@Ky6uk
Copy link
Author

Ky6uk commented Oct 30, 2018

@stefanorinetti All fields inside a message are optional actually. You can omit any of them and default value will be set. https://developers.google.com/protocol-buffers/docs/proto3#default

But getValue() also could be return default value I think.

@stale
Copy link

stale bot commented Jan 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 28, 2019
@stale stale bot closed this as completed Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants