Answer the question
In order to leave comments, you need to log in
Can this be considered "shit code"?
Hello. I want to unlearn shitty coding even in the early stages of learning Go. It seems to me that I'm quietly transferring the smell of my code from php to go along with the project :D
Here is the validation function.
If ps.ByName("data") is empty, it will return 404 (at the router level. Not visible in the function)
Updated the code following the advice below from Sergey Protko
It seems to have overcome duplication
Fixed "magic numbers"
and used two rules
- One abstraction level per 1 function
- Read code from top to bottom. Demotion rule
package auth
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/julienschmidt/httprouter"
"net/http"
"regexp"
"strings"
)
var DB *sql.DB
func Validate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
result := strings.Split(ps.ByName("data"), "|][|")
if result[0] == "login" {
if validateLogin(result[1]) == true {
fmt.Fprint(w, "valid|][|логин можно использовать при регистрации")
} else {
fmt.Fprint(w, "invalid|][|логин нельзя использовать при регистрации")
}
} else if result[0] == "email" {
if validateEmail(result[1]) == true {
fmt.Fprint(w, "valid|][|email можно использовать при регистрации")
} else {
fmt.Fprint(w, "invalid|][|email нельзя использовать при регистрации")
}
} else {
fmt.Fprint(w, "err|][|Данный тип не поддерживается. Возможны login и email")
}
}
func validateLogin(login string) bool {
var rxLogin = regexp.MustCompile(`^(?:[0-9a-zA-Zа-яА-Яa-zA-ZáéíóúàâêôãõüçÁÉÍÓÚÀÂÊÔÃÕÜÇ\ \_\-\*\+\=]){1,16}$`)
var result bool = rxLogin.MatchString(login)
if result == true {
return isLoginAvailable(login)
} else {
return false
}
}
func validateEmail(email string) bool {
var rxEmail = regexp.MustCompile("^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$")
var result bool = rxEmail.MatchString(email)
if result == true {
return isEmailAvailable(email)
} else {
return false
}
}
func isLoginAvailable(login string) bool {
rows, err := DB.Query("SELECT id FROM users WHERE login=?", login)
if err != nil {
return false
}
defer rows.Close()
var i int = 0
for rows.Next() {
i++
}
if i == 0 {
return true
} else {
return false
}
}
func isEmailAvailable(email string) bool {
rows, err := DB.Query("SELECT id FROM users WHERE email=?", email)
if err != nil {
return false
}
defer rows.Close()
var i int = 0
for rows.Next() {
i++
}
if i == 0 {
return true
} else {
return false
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question