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

Provide a way to wrap create message #1040

Open
wenerme opened this issue Dec 24, 2024 · 0 comments
Open

Provide a way to wrap create message #1040

wenerme opened this issue Dec 24, 2024 · 0 comments

Comments

@wenerme
Copy link

wenerme commented Dec 24, 2024

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:

export function createMessage<Desc extends DescMessage>(
  schema: Desc,
  init?: MessageInitShape<Desc>,
): MessageShape<Desc> {
  if (isMessage(init, schema)) {
    return init;
  }
  // Support for Date type
  if (init instanceof Date) {
    if (schema.typeName === 'google.protobuf.Timestamp') {
      return timestampFromDate(init) as any;
    }
  }
  const message = createZeroMessage(schema) as MessageShape<Desc>;
  if (init !== undefined) {
    initMessage(schema, message, init);
  }
  return message;
}

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:

export function createMessage2<Desc extends DescMessage>(
  schema: Desc,
  init?: MessageInitShape<Desc>,
): MessageShape<Desc> {
  return create(schema, init, {
    // provide a custom create function
    create: (schema, init) => {
      if (init instanceof Date) {
        if (schema.typeName === 'google.protobuf.Timestamp') {
          return timestampFromDate(init) as any;
        }
      }
      return create(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.

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

No branches or pull requests

1 participant