"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Integrate gorm.Model into Protocol Buffer Definitions with DateTime Support?

How to Integrate gorm.Model into Protocol Buffer Definitions with DateTime Support?

Published on 2024-11-02
Browse:872

How to Integrate gorm.Model into Protocol Buffer Definitions with DateTime Support?

Integrating gorm.Model into Protocol Buffer Definitions

When integrating gorm's gorm.Model fields into protobuf definitions, challenges arise due to the lack of datetime support in proto3. This article explores solutions to this problem.

ProtoBuf Field Type Mapping

CreatedAt, UpdatedAt: Use google.protobuf.Timestamp with the gogoproto.stdtime option.

DeletedAt: Handle it as an opaque timestamp field with a conversion function.

Code Generation Limitations

The protoc-gen-gorm project faced challenges with proto2 and proto3 compatibility.

Post-processing Script Solution

To integrate gorm.Model fields, a post-processing script like gorm.sh can be used after go file generation. This script can update the generated code to include GORM-specific annotations.

Script Execution and Results

Execute the script with ./gorm.sh file.pb.go on the generated pb.go file. The script will add annotations for id, name, etc., allowing GORM to properly interact with the model fields.

Conclusion

Integrating gorm.Model requires additional handling of datetime fields and customized code generation or post-processing. By following these steps, developers can successfully integrate GORM fields into their proto definitions.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3