HouzuoGuo / tiedot

A rudimentary implementation of a basic document (NoSQL) database in Go
BSD 2-Clause "Simplified" License
2.72k stars 257 forks source link

Bag in type conversion #152

Open agoalofalife opened 6 years ago

agoalofalife commented 6 years ago

https://github.com/HouzuoGuo/tiedot/blob/21707c0a3e12f008577d5384772fbd7ce9bb059d/httpapi/jwt.go#L244

if the first argument to pass type type MapClaims map[string]interface{} example how here:

    if !sliceContainsStr(tokenClaims[JWT_ENDPOINTS_ATTR], url) {
            http.Error(w, "", http.StatusUnauthorized)
            return

It switch possibleSlice.(type) { will return interface{} accordingly, the function will never return true

func sliceContainsStr(possibleSlice interface{}, str string) bool {
    switch possibleSlice.(type) {
    case []string:
        for _, elem := range possibleSlice.([]string) {
            if elem == str {
                return true
            }
        }
    }
    return false
}

I suggest so:

func sliceContainsStr(possibleSlice interface{}, str string) bool {
    if possibleSlice, exist := possibleSlice.([]string); exist {
        for _, elem := range possibleSlice {
            if elem == str {
                return true
            }
        }
    }
    return false
}
codenoid commented 4 years ago

how