-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Automatically wrapping & unwrapping wrappers.proto types? #677
Comments
There is no special handling for Wrappers, Any, Duration, Timestamp etc. currently. What could be done, though, is to set up custom classes for these types that are capable of handling these cases. Haven't thought this through, yet, unfortunately. |
I was thinking along those lines as well, I was having a bit of difficulty working out how to get the custom classes injected in when using codegen, but I'll have another crack |
There is a commented out example here: 0c6e639#diff-2c36c683dff5798fefed561dbec17803R54
|
…e API easier to use manually, see #677
Has it's own module now. Adding wrappers to the module translates to reflection. Static code integration is still todo, but possible. |
If anyone is interested in tackling other common types, feel free to send a PR! Otherwise, if there is a need for a specific type, but a PR is out of reach, feel free to leave a comment. |
I'm having trouble writing wrappers to support google/protobuf/wrappers types. Suppose I have a message like the following:
And I try to call
The desired result is that the string in this object gets turned into a StringValue message. However, this method results in the following error:
I tried to override this behavior both by providing a wrapper for |
This error comes from https://github.com/dcodeIO/protobuf.js/blob/master/src/converter.js#L36 which gets thrown before @dcodeIO Is there any way around this? Couldn't a preprocess step be called before this step? Or rename fromObject/toObject to serialize/deserialize and assume the argument could be anything. |
Any word on a solution for well-known types support in static code generation? I'm using protobufjs to read/write JSON that is generated by C# Protobuf code, which produces canonical JSON for well-known types throughout. I am forced to write brittle hardcoded preprocessing before every fromObject and toJSON calls to wrap/unwrap the values. |
Im also interested in static code generation for wrapper types. Currently, my solution is to manually overwrite
Then I can overwrite the statically generated
This isn't ideal because I could have many message types, each of which I need to overwrite. Would it make sense for this (or similar) logic to be baked into converter.js? As it stands (at least in Go), jsonpb special cases wrapper types which makes protobufjs incompatible with our server code. |
+1 - it would be great to add a mode where WKTs are generated per the JSON proto3 spec. |
This PR #1271 seems to resolve the issue mentioned by @kurayama (#677 (comment)) and make it possible to write wrapper like this one const valueTypeWrapper: Protobuf.IWrapper = {
fromObject(object) {
return this.fromObject({ value: object })
},
toObject(message, options) {
const object = this.toObject(message, options)
return object && object.value
}
}
Protobuf.wrappers['.google.protobuf.DoubleValue'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.StringValue'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.Int64Value'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.BoolValue'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.UInt64Value'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.UInt32Value'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.FloatValue'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.BytesValue'] = valueTypeWrapper
Protobuf.wrappers['.google.protobuf.Int32Value'] = valueTypeWrapper Would be great if an owner could review & merge it |
I'm wondering where you are defining those wrappers / applying them to your PB generated JS files? |
Any chance of getting PR #1271 or something similar merged? 🙏 This is generally also a use case for us. |
Is there any update on this? Are there any examples on how the wrapper module could be used for this purpose? |
Is there any way to get the typescript generated types from |
faced with this issue inside of nest.js -> no way to fix it |
protobuf.js version: 6.6.3
Hey, first off this library is really great - love the typescript integration! Thanks for building it.
I'm trying to work out how to retain 'null' values across the wire. We have some instances where a value can be null and it means something different to empty string or 0 value. Google has the wrappers https://github.com/google/protobuf/blob/master/src/google/protobuf/wrappers.proto that let you do this, and protobuf.js handles retaining the null well, however it doesn't automatically wrap and unwrap the values
Lets say I have
I'd like to be able to pass in:
and have them both encode & decode to the same thing. However it seems if I want to set the code string, I have to do:
I was hoping fromObject and toObject would handle this, but they don't - is there any way I can hook in some customisation to do this, or would this be a good feature to add to the framework?
The text was updated successfully, but these errors were encountered: