hertz icon indicating copy to clipboard operation
hertz copied to clipboard

如何在package.yaml里获取proto文件的内容

Open firsthubgit opened this issue 5 months ago • 7 comments

类似可以直接访问HttpMehtod的,我想在package.yaml里访问proto文件里定义的struct,访问struct定义的字段名称,类型、注释

firsthubgit avatar Aug 15 '25 05:08 firsthubgit

只想访问定义的请求的入参的和出参的结构体,该怎么实现啊

firsthubgit avatar Aug 19 '25 07:08 firsthubgit

不确定是否正确理解了你的需求,可以参考下这个文档。其中有一个MethodInfo ,可以获取拿到RequestType, Comment等。 https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/more-feature/template/#%E9%BB%98%E8%AE%A4-package-%E6%A8%A1%E6%9D%BF

ppzqh avatar Aug 22 '25 02:08 ppzqh

不确定是否正确理解了你的需求,可以参考下这个文档。其中有一个MethodInfo ,可以获取拿到RequestType, Comment等。 https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/more-feature/template/#%E9%BB%98%E8%AE%A4-package-%E6%A8%A1%E6%9D%BF

看过了,不满足的,他能拿到入参$MethodInfo.RequestTypeName,我想要拿这个入参struct的所有字段的tag和类型

firsthubgit avatar Aug 22 '25 02:08 firsthubgit

不确定是否正确理解了你的需求,可以参考下这个文档。其中有一个MethodInfo ,可以获取拿到RequestType, Comment等。 https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/more-feature/template/#%E9%BB%98%E8%AE%A4-package-%E6%A8%A1%E6%9D%BF

所以不是很懂,生成pb.go文件的时候,那里面都可以有strcut的详细信息,为什么使用在package.yaml里就拿不到?pb.go是通过什么方式获取到的?

firsthubgit avatar Aug 22 '25 02:08 firsthubgit

不太行。hertz 模板提供的渲染参数(对应你这里MethodInfo说的是HttpMethod类型),他里面只记录了 struct 的名字,没有记录进一步的结构体和细节,所以拿不到

type HttpMethod struct {
	Name               string
	HTTPMethod         string
	Comment            string
	RequestTypeName    string
	RequestTypePackage string
	RequestTypeRawName string
	ReturnTypeName     string
	ReturnTypePackage  string
	ReturnTypeRawName  string
	Path               string
	Serializer         string
	OutputDir          string
	RefPackage         string // handler import dir
	RefPackageAlias    string // handler import alias
	ModelPackage       map[string]string
	GenHandler         bool // Whether to generate one handler, when an idl interface corresponds to multiple http method
	// Annotations     map[string]string
	Models map[string]*model.Model
}

HeyJavaBean avatar Aug 25 '25 06:08 HeyJavaBean

@HeyJavaBean 对的,所以想知道有没有别的办法,生成pb.go文件的时候,他是怎么做到生成模板代码的

firsthubgit avatar Aug 25 '25 07:08 firsthubgit

pb.go 的桩结构体,其实是 hertztool 调用 protoc 生成的,hertztool 并不感知 struct 的字段细节这些 ast,所以模板的渲染字段就只有文档里列的那些,没有具体的结构体内容,所以现在做不到

HeyJavaBean avatar Aug 25 '25 08:08 HeyJavaBean