caicloud / nirvana

Golang Restful API Framework for Productivity
https://caicloud.github.io/nirvana/
Apache License 2.0
520 stars 105 forks source link

fix(utils): 确保 params 跟 results 的个数与函数的参数及返回值的个数相同 #414

Closed knight42 closed 3 years ago

knight42 commented 3 years ago

What this PR does / why we need it:

目前如果 parameters 的个数比函数的参数个数多,或者 results 的个数比函数的返回值的个数多的话,那么在执行 nirvana apinirvana client 的时候 nirvana 就会 panic:

panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/caicloud/nirvana/utils/api.NewDefinition(0xc00d6038f0, 0xc00022b8f8, 0x157a5e4, 0x3, 0xc005401f00, 0x0, 0x0)
        /Users/knight/go/pkg/mod/github.com/caicloud/nirvana@v0.3.0-rc.3/utils/api/definitions.go:147 +0xe25
github.com/caicloud/nirvana/utils/api.NewDefinitions(0xc00d6038f0, 0xc000164b40, 0x1, 0x1, 0x157a5e4, 0x3, 0xc005401b00, 0x1, 0x1, 0x0, ...)
        /Users/knight/go/pkg/mod/github.com/caicloud/nirvana@v0.3.0-rc.3/utils/api/definitions.go:175 +0x148
github.com/caicloud/nirvana/utils/api.NewPathDefinitions(0xc00d6038f0, 0xc00d603950, 0x157a5e4, 0x3, 0x0, 0x0, 0x0)
        /Users/knight/go/pkg/mod/github.com/caicloud/nirvana@v0.3.0-rc.3/utils/api/definitions.go:188 +0x10b
github.com/caicloud/nirvana/utils/api.(*Container).Generate(0xc00d61e040, 0x157a5e4, 0x3, 0xc00022bd38, 0x1, 0x1)
        /Users/knight/go/pkg/mod/github.com/caicloud/nirvana@v0.3.0-rc.3/utils/api/api.go:144 +0x179
main.main()
        /Users/knight/go/src/github.com/caicloud/redacted/nirvana-generated196043317/main.go:30 +0x610
exit status 2
FATAL 1211-13:45:34.841+08 api.go:54 | exit status 1

这个 PR 通过先校验两边的个数是否相等来避免 panic,同时输出一个比较用户友好的错误信息。

Which issue(s) this PR is related to (optional, link to 3rd issue(s)):

Fixes #

Reference to #

Special notes for your reviewer:

/cc @iawia002

Release note:

NONE
iawia002 commented 3 years ago

/lgtm /cherrypick release-0.3

caicloud-bot commented 3 years ago

@iawia002: once the present PR merges, I will cherry-pick it on top of release-0.3 in a new PR and assign it to you.

In response to [this](https://github.com/caicloud/nirvana/pull/414#issuecomment-742999836): >/lgtm >/cherrypick release-0.3 Instructions for interacting with me using PR comments are available [here](https://github.com/caicloud/engineering/blob/master/docs/caicloud_bot.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
caicloud-bot commented 3 years ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: iawia002

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)~~ [iawia002] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
caicloud-bot commented 3 years ago

@iawia002: new pull request created: #415

In response to [this](https://github.com/caicloud/nirvana/pull/414#issuecomment-742999836): >/lgtm >/cherrypick release-0.3 Instructions for interacting with me using PR comments are available [here](https://github.com/caicloud/engineering/blob/master/docs/caicloud_bot.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.