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.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么