drmgg4411 2019-02-01 13:07
浏览 62
已采纳

如何为一个端点创建多种验证方法?

I want to make a validation api in order to validate a set of json requests regarding specific set of rules. To do that I want to use just one endpoint and call functions that correspond to the specific json struct. I know that there is no method overloading in go so I am kind of stumped.

...

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{}

...

So in order to validate lots of different json requests, is it better to create structs for each and every json request? Or should I create these dynamically? How can I know what kind of request is sent if I only have one endpoint?

  • 写回答

1条回答 默认 最新

  • dongshuo2752 2019-02-01 13:43
    关注

    If you have a single endpoint/rpc that has to accept different JSON types, you'll need to tell it how to distinguish between them, somehow. One option is to have something like:

    type request struct {
      bodyA *requestBodyA
      bodyB *requestBodyB
    }
    

    Then, populate these fields in a container JSON object appropriately. The json module will only populate bodyA if a bodyA key is present, otherwise leaving it a nil, and so on.

    Here's a more complete example:

    type RequestBodyFoo struct {
        Name    string
        Balance float64
    }
    
    type RequestBodyBar struct {
        Id  int
        Ref int
    }
    
    type Request struct {
        Foo *RequestBodyFoo
        Bar *RequestBodyBar
    }
    
    func (r *Request) Show() {
        if r.Foo != nil {
            fmt.Println("Request has Foo:", *r.Foo)
        }
        if r.Bar != nil {
            fmt.Println("Request has Bar:", *r.Bar)
        }
    }
    
    func main() {
        bb := []byte(`
        {
            "Foo": {"Name": "joe", "balance": 4591.25}
        }
        `)
    
        var req Request
        if err := json.Unmarshal(bb, &req); err != nil {
            panic(err)
        }
        req.Show()
    
        var req2 Request
        bb = []byte(`
        {
            "Bar": {"Id": 128992, "Ref": 801472}
        }
        `)
        if err := json.Unmarshal(bb, &req2); err != nil {
            panic(err)
        }
        req2.Show()
    }
    

    Another option is to do it more dynamically with maps, but it's likely that the method above will be sufficient.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站