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

Fixing IsEmail cyclomatic complexity. Fixes #78 #88

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,44 @@ func IsEmail(email string) Result {
return ResultNotEmail
}

user := email[:atIndex]
return isValidEmailUser(email[:atIndex])
}

// makeEmail makes a checker function for the email checker.
func makeEmail(_ string) CheckFunc {
return checkEmail
}

// checkEmail checks if the given string is an email address.
func checkEmail(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsEmail(value.String())
}

// isValidEmailDomain checks if the email domain is a IPv4 or IPv6 address, or a FQDN.
func isValidEmailDomain(domain string) Result {
if len(domain) > 255 {
return ResultNotEmail
}

if domain[0] == '[' {
if strings.HasPrefix(domain, ipV6Prefix) {
// postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]
return IsIPV6(domain[len(ipV6Prefix) : len(domain)-1])
}

// postmaster@[123.123.123.123]
return IsIPV4(domain[1 : len(domain)-1])
}

return IsFqdn(domain)
}

// isValidEmailUser checks if the email user is valid.
func isValidEmailUser(user string) Result {
// Cannot be empty user
if user == "" || len(user) > 64 {
return ResultNotEmail
Expand Down Expand Up @@ -79,36 +115,3 @@ func IsEmail(email string) Result {

return ResultValid
}

// makeEmail makes a checker function for the email checker.
func makeEmail(_ string) CheckFunc {
return checkEmail
}

// checkEmail checks if the given string is an email address.
func checkEmail(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsEmail(value.String())
}

// isValidEmailDomain checks if the email domain is a IPv4 or IPv6 address, or a FQDN.
func isValidEmailDomain(domain string) Result {
if len(domain) > 255 {
return ResultNotEmail
}

if domain[0] == '[' {
if strings.HasPrefix(domain, ipV6Prefix) {
// postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]
return IsIPV6(domain[len(ipV6Prefix) : len(domain)-1])
}

// postmaster@[123.123.123.123]
return IsIPV4(domain[1 : len(domain)-1])
}

return IsFqdn(domain)
}