将gorm.Model字段集成到protobuf定义中

Ale*_*ias 5 postgresql go grpc go-gorm proto3

我试图弄清楚如何将gorm.Model字段(deleted_at,create_at,id等)集成到我的proto3定义中。但是,我不能为proto3提供日期时间类型。我试图寻找有关如何将gorm字段序列化为字符串的文档(因为proto3处理字符串),但是我什么都没找到。

有没有人能够在其原型定义中成功使用gorm模型字段?我正在使用go-micro的插件来生成* pb.go文件。

这是我当前无法使用的消息定义。似乎空字符串存储在delete_at数据库中,因为当delete_at查询为null时,postgres数据库什么也不返回。

message DatabaseConfig {
    string address = 1;
    int32 port = 2;
    string databaseName = 3;
    string username = 4;
    string password = 5;
    string databaseType = 6;
    string quertStatement = 7;
    int32 id = 8;
    string createdAt = 9;
    string updatedAt = 10;
    string deletedAt = 11;
}
Run Code Online (Sandbox Code Playgroud)

更新:我已经将proto def更新为以下内容,但gord仍无法使用Id,CreatedAt,UpdatedAt和DeletedAt字段正确

syntax = "proto3";

package go.micro.srv.importer;

import "google/protobuf/timestamp.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";

service ImporterService {
    rpc CreateDatabaseConfig(DatabaseConfig) returns (Response) {}
    rpc RetrieveDatabaseConfig(GetRequest) returns (Response) {}
    rpc UpdateDatabaseConfig(DatabaseConfig) returns (Response) {}
    rpc DeleteDatabaseConfig(DatabaseConfig) returns (Response) {}
}


message GetRequest {}


message DatabaseConfig {
    string address = 1;
    int32 port = 2;
    string databaseName = 3;
    string username = 4;
    string password = 5;
    string databaseType = 6;
    string quertStatement = 7;
    int32 id = 8;
    google.protobuf.Timestamp createdAt = 9 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp updatedAt = 10 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp deletedAt = 11 [(gogoproto.stdtime) = true];
}


message Response {
    bool created = 1;
    DatabaseConfig database_config = 2;

    repeated DatabaseConfig databaseConfigs = 3;
}
Run Code Online (Sandbox Code Playgroud)

小智 2

protoc-gen-gorm项目不适合我。看起来 proto2 和 proto3 发生了一些混合,最终我无法让它工作。

我的解决方案是在从 protobuf 生成 go 文件后创建一个脚本来进行后处理。

如果这是我的原型profile/profile.proto

message Profile {
  uint64 id = 1;
  string name = 2;
  bool active = 3;
  // ...
}
Run Code Online (Sandbox Code Playgroud)

profile/profile.pb.go使用标准命令创建的protoc

// ...
type Profile struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Id     uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
    Name   string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
    Active bool   `protobuf:"varint,3,opt,name=active,proto3" json:"active,omitempty"`
}
// ...
Run Code Online (Sandbox Code Playgroud)

我使用这个脚本gorm.sh

#!/bin/bash

g () {
  sed "s/json:\"$1,omitempty\"/json:\"$1,omitempty\" gorm:\"type:$2\"/"
}

cat $1 \
| g "id" "primary_key" \
| g "name" "varchar(100)" \
> $1.tmp && mv $1{.tmp,}
Run Code Online (Sandbox Code Playgroud)

我在生成 go 文件后调用它./gorm.sh profile/profile.pb.go,结果profile/profile.pb.go是:

// ...
type Profile struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Id     uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" gorm:"type:primary_key"`
    Name   string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty" gorm:"type:varchar(100)"`
    Active bool   `protobuf:"varint,3,opt,name=active,proto3" json:"active,omitempty"`
}
// ...
Run Code Online (Sandbox Code Playgroud)