【发布时间】:2019-02-01 13:07:41
【问题描述】:
我想制作一个验证 api,以验证一组关于特定规则集的 json 请求。为此,我只想使用一个端点并调用与特定 json 结构相对应的函数。我知道 go 中没有方法重载,所以我有点难过。
...
type requestBodyA struct {
SomeField string `json:"someField"`
SomeOtherField string `json:"someOtherField"`
}
type requestBodyB struct {
SomeDifferentField string `json:"someDifferentField"`
SomeOtherDifferentField string `json:"someOtherDifferentField"`
}
type ValidationService interface {
ValidateRequest(ctx context.Context, s string) (err error)
}
type basicValidationService struct{}
...
那么,为了验证大量不同的 json 请求,为每个 json 请求创建结构会更好吗?或者我应该动态创建这些?如果我只有一个端点,如何知道发送了什么样的请求?
【问题讨论】:
-
“如果我只有一个端点,我怎么知道发送了什么样的请求”除非您设计一种进入 API 的方法,否则无论您使用什么语言,您都无法做到。这只是一个不可能的 API 设计。必须有某种方法来检测客户端正在尝试做什么。
-
是的,知道了。感谢您的意见。