Open zcyc opened 4 months ago
目前是这样实现的,不知道有没有更优雅的方式
func (s *sUser) GetById(ctx context.Context, uid uint64) (*pbentity.User, error) {
var (
user *entity.User
userPB *pbentity.User
)
// 从数据库中获取用户信息
err := dao.User.Ctx(ctx).Where(do.User{
Id: uid,
}).Scan(&user)
if user == nil {
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, g.I18n().T(ctx, "{#user-not-found}"))
}
// 转换 pb 对象
if err := gconv.Struct(user, &userPB); err != nil {
return nil, gerror.NewCodef(gcode.CodeInternalError, g.I18n().T(ctx, "{#system-busy}"))
}
// 处理 pb 时间
userPB.CreateAt = timestamppb.New(user.CreateAt.Time)
userPB.UpdateAt = timestamppb.New(user.UpdateAt.Time)
return userPB, err
}
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
This is currently possible, but I don’t know if there is a more elegant way.
func (s *sUser) GetById(ctx context.Context, uid uint64) (*pbentity.User, error) { var ( user *entity.User userPB *pbentity.User )
// Get user information from the database err := dao.User.Ctx(ctx).Where(do.User{ Id: uid, }).Scan(&user) if user == nil { return nil, gerror.NewCodef(gcode.CodeInvalidParameter, g.I18n().T(ctx, "{#user-not-found}")) }
// Convert pb object if err := gconv.Struct(user, &userPB); err != nil { return nil, gerror.NewCodef(gcode.CodeInternalError, g.I18n().T(ctx, "{#system-busy}")) }
// Process pb time userPB.CreateAt = timestamppb.New(user.CreateAt.Time) userPB.UpdateAt = timestamppb.New(user.UpdateAt.Time)
return userPB, err }
@zcyc 可以试试给字段实现一个json.Unmarshaler接口
Go version
go version go1.22.3 darwin/amd64
GoFrame version
v2.7.0
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
I run gf-demo-grpc and use PostgresSQL for database. DDL is:
When i request the GetById method, scan the zero value for google.protobuf.Timestamp of pbentity.User
What did you see happen?
What did you expect to see?