How to identify ::std::option::Option<::protobuf::EnumOrUnknown<T>> in the fn field of protobuf_codegen::CustomizeCallback?

I want to use CustomizeCallback to insert some annotations to the output of protobuf_codegen. Specifically is serde annotations.

Now I get a problem that EnumOrUnknown is not satisfied for the trait Serialize, so I need to add annotation like this #[serde(serialize_with = "crate::unknow_or_enum_serialize")].

but I can't find a way to identify ::std::option::Option<::protobuf::EnumOrUnknown<T>> in CustomizeCallback.

can you help me?

impl CustomizeCallback for MyCustomize {
    fn field(&self, field: &FieldDescriptor) -> Customize {
        let c = Customize::default();
        //  here, how to identify?
    }
}

@kingwingfly my friend

is this ok?

    fn field(&self, field: &FieldDescriptor) -> Customize {
        let c = Customize::default();

        match field.proto().type_() {
            Type::TYPE_ENUM if !field.is_required() => {
                c.before("#[serde(serialize_with = \"crate::unknow_serialize\")]")
            }
            _ => c,
        }
    }

I think it may work as expected. I always use protobuf_json_mapping, and I am not so familiar with how to serialize a protobuf message. My opinion is to refactor your proto file and use oneof to achieve your goal, so that you can use protobuf_json_mapping crate.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.