Skip to content

Prost-build generates i32 for enum fields instead of using the enum type directly in message #1201

Description

@immno

I have a .proto file:

enum AssetAttrAggregationSourceType {
    AttrMeasureDacoo = 0;
    AttrTask = 1;
    AttrMeasurePipeline = 3;
}

message AssetAttrAggregationCreateEvent {
    int64 id = 1;
    int64 sourceId = 2;
    AssetAttrAggregationSourceType sourceType = 3;
    optional int64 associationId = 4;
}

/// build.rs
/// prost = "0.13"
/// prost-types = "0.13"
/// prost-build = "0.13"
fn main() {
    let mut config = prost_build::Config::new();
    config
        .out_dir("src/queue/msg")
        .compile_protos(&["cluster_event.proto"], &["proto/"])
        .unwrap();
}

After using prost-build to generate the code, it looks like this:

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum AssetAttrAggregationSourceType {
    AttrMeasureDacoo = 0,
    AttrTask = 1,
    AttrMeasurePipeline = 3,
}

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AssetAttrAggregationCreateEvent {
    #[prost(int64, tag = "1")]
    pub id: i64,
    #[prost(int64, tag = "2")]
    pub source_id: i64,
    #[prost(enumeration = "AssetAttrAggregationSourceType", tag = "3")]
    pub source_type: i32,
    #[prost(int64, optional, tag = "4")]
    pub association_id: ::core::option::Option<i64>
}

Why can't we generate AssetAttrAggregationCreateEvent like the following (Oneof is fine, but it's inconvenient to use now, and we need to convert From every time):

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AssetAttrAggregationCreateEvent {
    #[prost(int64, tag = "1")]
    pub id: i64,
    #[prost(int64, tag = "2")]
    pub source_id: i64,
    #[prost(enumeration = "AssetAttrAggregationSourceType", tag = "3")]
    pub source_type: AssetAttrAggregationSourceType,
    #[prost(int64, optional, tag = "4")]
    pub association_id: ::core::option::Option<i64>
}

Activity

  1. caspermeijn commented on Dec 6, 2024

    @caspermeijn
    Member

    I agree that would be more convenient. However, that is a breaking change and it requires people to work on it.

    I am closing as a duplicate of: #276

    If you are interested in working on a solution, you can try to contribute to this PR: #1079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions