douhuang3833 2016-09-05 10:32
浏览 56
已采纳

检查POST参数的更好方法?

I'm currently working on setting up an API using Golang. I'm making a signup route and I need to check that everything is filled in my SignupModel. So for now I'm using a very long if condition.. I'm sure there must be more efficient/elegant solutions, but I somehow cannot find them.

Here is a code snippet:

// My SignupModel
type SignupModel struct {
    ID        string `json:"id"`
    Username  string `json:"username"`
    FbToken   string `json:"fbtoken"`
    Email     string `json:"email"`
    Firstname string `json:"firstname"`
    Lastname  string `json:"lastname"`
    Picture   string `json:"picture"`
}

// This is the condition I'm using inside my route (I know it's terrible)
if signup.ID != "" && signup.Username != "" && signup.FbToken == "" && signup.Email == "" && signup.Firstname == "" && signup.Lastname == "" && signup.Picture == "" {        
    json.NewEncoder(w).Encode(signup)
} else {
    statusResponse := StatusResponse{Status: "Something went wrong", StatusCode: 401}
    json.NewEncoder(w).Encode(statusResponse)
}

Thanks in advance for the help.

  • 写回答

1条回答 默认 最新

  • dongya6381 2016-09-05 10:44
    关注

    You could create a general helper function which uses reflection (reflect package) to check all fields of a struct:

    func check(s interface{}) error {
        v := reflect.ValueOf(s)
        for i := v.NumField() - 1; i >= 0; i-- {
            if v.Field(i).Interface() == "" {
                return fmt.Errorf("Field %s is empty!", v.Type().Field(i).Name)
            }
        }
        return nil
    }
    

    Using it:

    fmt.Println(check(SignupModel{"a", "b", "c", "d", "e", "f", "g"}))
    fmt.Println(check(SignupModel{}))
    

    Output (try it on the Go Playground):

    <nil>
    Field Picture is empty!
    

    Improvements:

    The above check() function works for all types, not just for SignupModel, but currently –as it is– only handles fields of string type. You can improve it if you want to support other field types.

    Here's an improved version which handles fields of other types too, and it also handles if the passed value is a pointer to a struct (simply dereferences it). If a non-struct value is passed, it returns an error:

    func check(s interface{}) error {
        v := reflect.Indirect(reflect.ValueOf(s))
        if v.Kind() != reflect.Struct {
            return fmt.Errorf("Not struct!")
        }
        v2 := reflect.Zero(v.Type())
        for i := v.NumField() - 1; i >= 0; i-- {
            if v.Field(i).Interface() == v2.Field(i).Interface() {
                return fmt.Errorf("Field %s is empty!", v.Type().Field(i).Name)
            }
        }
        return nil
    }
    

    Testing it:

    fmt.Println(check(&struct{ I int }{1}))
    fmt.Println(check(struct{ I int }{}))
    fmt.Println(check(struct { I int; S string }{1, "a"}))
    fmt.Println(check(&struct { I int; S string }{}))
    fmt.Println(check("I'm a string"))
    

    Output (try it on the Go Playground):

    <nil>
    Field I is empty!
    <nil>
    Field S is empty!
    Not struct!
    

    Final notes:

    The above solution uses reflection which is always slower than direct code (the one you're trying to shorten). But since we're talking about HTTP POST requests, the performance difference is negligible (as an HTTP POST request could take hundreds of milliseconds while this check() function takes half a microsecond when called with a value of your SignupModel type – benchmarked).

    If you'd want to mix the speed of your code and the flexibility of this, one option would be to create a generator (go generate, read blog post: Generating code) which would generate unique checkXX() helper functions, one for each distinct struct type, operating without reflection (using direct field value comparisons).

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

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀