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

Skip object check for types with a defined custom wrapper. #1271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alec-mccormick
Copy link

@alec-mccormick alec-mccormick commented Jul 31, 2019

This PR would give the ability to pass a primitive value for a non primitive type as long as a custom wrapper is defined for it.

Use Case

We would like to be able to define fields that can be multiple different primitive types, however the library currently will throw an error if a message field resolves to a type and the value is not an object. If a custom wrapper is defined for that type, then it should be assumed that the wrapper will handle serialization.

Message:

message StringOrDouble {
  oneof kind {
    string stringValue = 1;
    double doubleValue = 2;
  }  
}

message MyMessage {
  StringOrDouble value = 1;
}

Protobuf Wrapper:

{
  '.StringOrDouble': {
    fromObject(value) {
      return (typeof value === 'string') 
        ? this.create({ stringValue: value }) 
        : this.create({ doubleValue: value });
    },
    toObject(message) {
      return (typeof message.stringValue === 'string') 
        ? message.stringValue 
        : message.doubleValue;
    }
  }
}

Copy link

@pgherveou pgherveou left a comment

Choose a reason for hiding this comment

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

I am not an owner, but tested this patch works perfectly for defining StringValue wrapper for examples
see #677 (comment)

@pgherveou
Copy link

@alexander-fenster, @JustinBeckwith, do you think you guys or another contributors could review and merge this?

@pgherveou
Copy link

@nicolasnoble any chance you could look at this PR? It is a quick fix and solves some interesting issues. see my comment here see #677 (comment)

GregHattJr pushed a commit to GregHattJr/protobuf.js that referenced this pull request Nov 18, 2019
GregHattJr pushed a commit to GregHattJr/protobuf.js that referenced this pull request Nov 18, 2019
@dardanos
Copy link

Please merge this 🙏

pgherveou pushed a commit to pgherveou/protobuf.js that referenced this pull request Mar 28, 2020
@betmix-matt
Copy link

@dcodeIO or @alexander-fenster any chance this could get some love?

It's a dependency of work arounds like:
#677

@Sandeep-Hirani
Copy link

@dcodeIO @alexander-fenster @bcoe Review please

@stephenh
Copy link

I know pings are annoying @dcodeIO / @alexander-fenster / @gitjuba but FWIW I'm the maintainer of ts-proto, and have pre-reviewed / :+1: this super-small patch.

We'd like to see this merged b/c it allows us to support modeling StringValue/etc. in TypeScript messages as just string | undefined.

Currently we already support this for our own encoder methods, but a number of our users use NestJS, which goes through grpcjs to @grpcjs/proto-loader, and proto-loader really wants to go through protobufjs's converter.js-generated encoders.

Usually we can use protobufjs's wrappers config to interject our own fromObject / toObject to map our "idiomatic TS"-isms back to what protobufjs supports.

Except for non-object primitives, due to this one extra-cautious typeof ... !== object check that this PR removes, only if the type has a wrapper configured, i.e. an indication that the user knows what they are doing, and hence the type ... !== object check imo should be safe to remove, and this PR safe to merge.

Lmk if you have questions or concerns. Thanks!

@gigi
Copy link

gigi commented Nov 27, 2023

Hi guys! Is there any chance that this can be merged?

@timosaikkonen timosaikkonen mentioned this pull request Dec 13, 2023
@Marko298
Copy link

Marko298 commented Jan 4, 2024

We really need this change

@zamarawka
Copy link

Any way to get it merged?

@hyunjinjeong
Copy link

hyunjinjeong commented Apr 8, 2024

This simple PR that will help many people hasn't been merged or even reviewed for 5 years now 😢

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.

None yet