Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/gdb: scan zero value for google.protobuf.Timestamp #3571

Open
zcyc opened this issue May 10, 2024 · 3 comments
Open

database/gdb: scan zero value for google.protobuf.Timestamp #3571

zcyc opened this issue May 10, 2024 · 3 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@zcyc
Copy link
Contributor

zcyc commented May 10, 2024

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:

create table "user"
(
    id serial primary key,
    passport  varchar(45) not null unique,
    password  varchar(45) not null,
    nickname  varchar(45) not null,
    create_at timestamp,
    update_at timestamp,
    delete_at timestamp
);

When i request the GetById method, scan the zero value for google.protobuf.Timestamp of pbentity.User

What did you see happen?

{
	"User": {
		"Id": 1,
		"Passport": "正团小斯",
		"Password": "口际严消只但",
		"Nickname": "进影一得华来行",
		"CreateAt": {
			"seconds": "0",
			"nanos": 0
		},
		"UpdateAt": {
			"seconds": "0",
			"nanos": 0
		},
		"DeleteAt": null
	}
}

What did you expect to see?

{
	"User": {
		"Id": 1,
		"Passport": "正团小斯",
		"Password": "口际严消只但",
		"Nickname": "进影一得华来行",
		"CreateAt": {
			"seconds": "1715155546",
			"nanos": 0
		},
		"UpdateAt": {
			"seconds": "1715155546",
			"nanos": 0
		},
		"DeleteAt": null
	}
}
@zcyc zcyc added the bug It is confirmed a bug, but don't worry, we'll handle it. label May 10, 2024
@zcyc
Copy link
Contributor Author

zcyc commented May 11, 2024

目前是这样实现的,不知道有没有更优雅的方式

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
}

@Issues-translate-bot
Copy link

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
}

@wln32
Copy link
Member

wln32 commented May 16, 2024

@zcyc 可以试试给字段实现一个json.Unmarshaler接口

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

3 participants