根据 .json 和 .gotmpl 文件生成代码。

huangapple go评论83阅读模式
英文:

Generating code based on .json and .gotmpl file

问题

我正在问这个简单的问题,因为经过一段时间的研究,我仍然没有找到答案。

我正在尝试使用这个仓库https://github.com/StefanAbl/go-freeipa,并使用我使用的最新freeipa版本(即4.10和API 2.280)更新生成的代码。在"developping.md"说明中,他们谈到了一个gen工具来生成freeipa代码。

代码使用.json文件和一个.gotmpl文件来生成代码(根据我所了解的)。

我完全不知道他们在说什么。我从来没有用过Go编程(但由于语言的易用性,我可以在某些地方修复问题),所以我对知名的包不熟悉。我试图在网上寻求帮助,特别是使用这个工具https://github.com/clipperhouse/gen或者go generate,但似乎都不对。clipperhouse/gen期望代码中有+gen标签(在仓库中不存在),go generate也需要标签。

请注意,我还尝试运行go run main.go,但是我收到了以下错误:

# command-line-arguments
gen/main.go:53:21: undefined: Schema
gen/main.go:58:13: undefined: SchemaDump
gen/main.go:64:18: undefined: Command
gen/main.go:96:21: undefined: Class
gen/main.go:190:20: undefined: ErrDesc
gen/main.go:195:12: undefined: ErrDesc
gen/main.go:202:27: undefined: Schema
gen/main.go:202:42: undefined: ErrDesc
gen/main.go:204:16: undefined: toGoType
gen/main.go:215:11: undefined: Schema
gen/main.go:215:11: too many errors

检查代码后,确实这些(类型?)没有声明,这解释了错误。而且我对这门语言的了解还不够,无法自己修复这个问题 根据 .json 和 .gotmpl 文件生成代码。 或者是否需要修复,因为我应该使用这个gen工具。

如果有人知道我应该如何根据freeipa/gen中的文件生成freeeipa/generated.go文件,那就太棒了 根据 .json 和 .gotmpl 文件生成代码。

PS:出于你的好奇心,我为什么要这样做:

我正在尝试使用最新的freeipa版本更新go-freeipa包。我正在尝试在我的K8S集群上设置Freeipa-issuer https://github.com/guilhem/freeipa-issuer,但返回一个Failed to sign certificate request: Fail to request certificate: json: cannot unmarshal string into Go struct field CertRequestResult.result.value of type int错误。我希望这个错误是因为go-freeipa客户端使用的是一个过时的freeipa版本(4.9,api 2.170,而不是4.10和api 2.280)来生成代码。

英文:

I am asking this simple question as, after researching for quite some time, I still haven't found an answer yet.

I am trying to use this repository https://github.com/StefanAbl/go-freeipa, and updating the generated code with the latest freeipa version I use (which is 4.10 with API 2.280). In the "developping.md" instructions, they talk about a gen tool to generate the freeipa code.

The code is using .json files and a .gotmpl file to generate the code (from what I could grasp).

I have NO idea what they are talking about. I have absolutely never coded in Go (but can fix things here and there due to the language ease of use) so I am not familiar with weel-known packages. I tried to find help online, especially with this tool https://github.com/clipperhouse/gen or the go generate, but it doesn't seems right. The clipperhouse/gen expects +gen tags in the code (which doesnt exists in the repo) and go generate does work with tags as well.

Note that I also tried to do a go run main.go but I receive those errors:

# command-line-arguments
gen/main.go:53:21: undefined: Schema
gen/main.go:58:13: undefined: SchemaDump
gen/main.go:64:18: undefined: Command
gen/main.go:96:21: undefined: Class
gen/main.go:190:20: undefined: ErrDesc
gen/main.go:195:12: undefined: ErrDesc
gen/main.go:202:27: undefined: Schema
gen/main.go:202:42: undefined: ErrDesc
gen/main.go:204:16: undefined: toGoType
gen/main.go:215:11: undefined: Schema
gen/main.go:215:11: too many errors

After checking the code, indeed those (types?) are not declared which explains the errors. And I am not versed in the language enough to be able to fix this myself 根据 .json 和 .gotmpl 文件生成代码。 Or even if it needs fixing due to the fact that I should use this gen tool instead.

If anyone has an idea on How I should generate the freeeipa/generated.go file, based on the files in freeipa/gen, that would be awesome 根据 .json 和 .gotmpl 文件生成代码。

PS : For your curiosity on why I am doing this:

I am trying to update the go-freeipa package with the latest freeipa version. The Freeipa-issuer https://github.com/guilhem/freeipa-issuer I am trying to setup on my K8S cluster is returning a
Failed to sign certificate request: Fail to request certificate: json: cannot unmarshal string into Go struct field CertRequestResult.result.value of type int error> I hope this error comes from the fact that go-freeipa client is using an oudated freeipa version (4.9, api 2.170 instead of 4.10 and api 2.280) to generate the code.

答案1

得分: 2

实际上,我很愚蠢 根据 .json 和 .gotmpl 文件生成代码。

深入研究代码并与Go文档进行比较后,我意识到执行go run main.go只会运行main.go文件,而忽略包中的其他文件。

我天真地以为它们被导入并在调用main.go文件时被使用。

一个简单的“修复”(实际上不是修复,只是正确地启动程序)是使用go run .而不是go run main.go 根据 .json 和 .gotmpl 文件生成代码。

英文:

Actually I am stupid 根据 .json 和 .gotmpl 文件生成代码。

Diving a bit more into the code and comapring it the go documentation, I realised that doing go run main.go only runs the main.go file, ignoring other files in the package.

I naively thought they were imported and therefore used when calling the main.go file.

A simple fix (which is not a fix but just correctly starting the program) is to do a go run . instead of go run main.go 根据 .json 和 .gotmpl 文件生成代码。

huangapple
  • 本文由 发表于 2023年6月4日 17:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76399669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定