检查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.

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).