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

hz 使用 (protobuf) #631

Closed
maojinhua opened this issue Apr 24, 2023 · 6 comments
Closed

hz 使用 (protobuf) #631

maojinhua opened this issue Apr 24, 2023 · 6 comments

Comments

@maojinhua
Copy link

请问如何生成必须传输的请求字段呢?我看通过protobuf 生成的 字段都是 omitempty,有必传的字段要怎么写 呢?

比如 我下面的 id 和 name 字段要求必传,要怎么写 UpdateRepositoryReq 呢?
message UpdateRepositoryReq {
int64 Id = 1[(api.path) = "id"];
string Name = 2[(api.body) = "name"];
}

type UpdateRepositoryReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields

Id    int64  `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty" path:"id"`
Name  string `protobuf:"bytes,2,opt,name=Name,proto3" form:"name" json:"name,omitempty"`

}

@maojinhua
Copy link
Author

这样子写吗?
message UpdateRepositoryReq {
int64 Id = 1[(api.path) = "id,required"];
string Name = 2[(api.body) = "name,required"];
}

@rogerogers
Copy link
Contributor

https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/command/

如果所有字段都是required的话hz --unset_omitempty
部分字段的话要自己判断

@maojinhua
Copy link
Author

maojinhua commented Apr 24, 2023

https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/command/

如果所有字段都是required的话hz --unset_omitempty 部分字段的话要自己判断

你好 自己要怎么判断呢?是自己在业务层里自己判断吗? 比如说 if name==""{return err}

@rogerogers
Copy link
Contributor

https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/binding-and-validate/
建议先看下文档的相关部分。

@maojinhua
Copy link
Author

hz --unset_omitempty

嗯好的谢谢

@maojinhua
Copy link
Author

https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/binding-and-validate/ 建议先看下文档的相关部分。

你好 我试了,这样子写就可以
message UpdateRepositoryReq {
int64 Id = 1[(api.path) = "id,required"];
string Name = 2[(api.body) = "name,required"];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants