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

Proposal: Support custom string, binary, message serialization in prost-deriv #1224

Open
bickfordb opened this issue Jan 7, 2025 · 0 comments

Comments

@bickfordb
Copy link

It would be convenient to reuse existing structs (like those defined for SeaORM models) to be used by Prost (and subsequently Tonic) for Grpc web apps.

For example, given the following protocol buffer code, it would be convenient to have the subsequent Rust code have a 1-1 mapping.

Without this functionality or similar, one currently has to define/generate intermediate structs to marshal between gRPC/ProtoBuf and ORM structs even though they represent exactly the same information.

Example Protocol Buffer

message Timestamp {
   int64 seconds = 1;
   int32 nanos = 2; 
}

message Date {
    int32 year = 1;
    uint32 month = 2;
    uint32 day = 3;
}

message Account {
  bytes id = 1; // UUID encoded as 4 bytes
  string first_name = 2; // like "Joe"
  string last_name = 3; // like "Biden"
  Timestamp created_at = 4; // account registration timestamp encode as message
  Date birthday = 5; // like Nov 20, 1942 encoded as a message
  string favorite_movie_id = 6; // some other uuid encoded as a string like "7033d448-c14e-4967-af0c-9b3826d6edb8"
  string some_other_datetime = 7; // datetime encoded as a iso8601 string like "2022-09-27 18:00:00.000."
}

Example Rust code

use uuid::Uuid;
use chrono::{NaiveDate, DateTime};

#derive(Message, ...)
struct Account { 
      #[prost(UUIDBytesCodec, tag=1)]
      id: Uuid,
      first_name: String,
      last_name: String,
      #[prost(DateTimeMessageCodec, tag=4)]
      created_at: DateTime,
      #[prost(DateMessageCodec, tag=5)]
      birthday: NaiveDate,
      #[prost(UUIDStringCodec, tag=6)]
      favorite_movie_id: Uuid,
      #[prost(ISO8601DateTime, tag=7)]
      some_other_datetime: DateTime
}
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