Skip to content

Commit

Permalink
Fixing IsEmail cyclomatic complexity. Fixes #78 (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Jun 22, 2023
1 parent db8ce0e commit c7edf9e
Showing 1 changed file with 37 additions and 34 deletions.
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)
}

0 comments on commit c7edf9e

Please sign in to comment.