You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had to override the entire create function to support custom types (e.g., Date initialization for google.protobuf.Timestamp), resulting in the following custom implementation:
exportfunctioncreateMessage<DescextendsDescMessage>(schema: Desc,init?: MessageInitShape<Desc>,): MessageShape<Desc>{if(isMessage(init,schema)){returninit;}// Support for Date typeif(initinstanceofDate){if(schema.typeName==='google.protobuf.Timestamp'){returntimestampFromDate(init)asany;}}constmessage=createZeroMessage(schema)asMessageShape<Desc>;if(init!==undefined){initMessage(schema,message,init);}returnmessage;}
While this works, it requires duplicating and modifying the original create function, which is neither clean nor maintainable.
Proposed Solution
I propose extending the create function to accept an additional option for custom creation logic. This would allow users to define their custom type conversions without overriding the entire function.
Here’s an example implementation of the proposed solution:
exportfunctioncreateMessage2<DescextendsDescMessage>(schema: Desc,init?: MessageInitShape<Desc>,): MessageShape<Desc>{returncreate(schema,init,{// provide a custom create functioncreate: (schema,init)=>{if(initinstanceofDate){if(schema.typeName==='google.protobuf.Timestamp'){returntimestampFromDate(init)asany;}}returncreate(schema,init);},});}
This change would allow create to pass the custom logic to the internal toMessage function. While it may not resolve type-checking errors for MessageInit entirely, it would provide a flexible workaround for custom type conversions with minimal effort.
Benefits
Reduces the need for duplicating the create function.
Provides a cleaner and more maintainable solution for handling custom type conversions.
Maintains backward compatibility by introducing an optional parameter.
Would it be possible to consider implementing this enhancement? It would greatly improve the extensibility and usability of the create function.
Disclaimer: I am a non-native English speaker, and this issue description has been improved with the assistance of AI. Please let me know if anything needs further clarification.
The text was updated successfully, but these errors were encountered:
Due to limitations in the current implementation, as discussed in the following issues:
I had to override the entire create function to support custom types (e.g., Date initialization for google.protobuf.Timestamp), resulting in the following custom implementation:
While this works, it requires duplicating and modifying the original create function, which is neither clean nor maintainable.
Proposed Solution
I propose extending the create function to accept an additional option for custom creation logic. This would allow users to define their custom type conversions without overriding the entire function.
Here’s an example implementation of the proposed solution:
This change would allow create to pass the custom logic to the internal toMessage function. While it may not resolve type-checking errors for MessageInit entirely, it would provide a flexible workaround for custom type conversions with minimal effort.
Benefits
Would it be possible to consider implementing this enhancement? It would greatly improve the extensibility and usability of the create function.
Disclaimer: I am a non-native English speaker, and this issue description has been improved with the assistance of AI. Please let me know if anything needs further clarification.
The text was updated successfully, but these errors were encountered: