Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bag in type conversion #152

Open
agoalofalife opened this issue Nov 8, 2017 · 1 comment
Open

Bag in type conversion #152

agoalofalife opened this issue Nov 8, 2017 · 1 comment
Assignees
Labels

Comments

@agoalofalife
Copy link
Collaborator

switch possibleSlice.(type) {

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
}
@agoalofalife agoalofalife self-assigned this Nov 9, 2017
@codenoid
Copy link

how

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants