auth/internal/domain/email.go

43 lines
822 B
Go
Raw Normal View History

2021-12-26 19:04:01 +00:00
package domain
2022-01-08 20:07:14 +00:00
import (
"strings"
"testing"
)
2022-01-29 17:50:45 +00:00
// Email represent email identifier.
2022-01-08 20:07:14 +00:00
type Email struct {
user string
host string
}
var ErrEmailInvalid error = NewError(ErrorCodeInvalidRequest, "cannot parse email", "")
2022-01-08 20:07:14 +00:00
2022-01-29 17:50:45 +00:00
// ParseEmail parse strings to email identifier.
2022-01-29 14:43:00 +00:00
func ParseEmail(src string) (*Email, error) {
2022-01-08 20:07:14 +00:00
parts := strings.Split(strings.TrimPrefix(src, "mailto:"), "@")
if len(parts) != 2 { //nolint: gomnd
return nil, ErrEmailInvalid
}
return &Email{
user: parts[0],
host: parts[1],
}, nil
}
2022-01-29 17:50:45 +00:00
// TestEmail returns valid random generated email identifier.
2022-01-08 20:07:14 +00:00
func TestEmail(tb testing.TB) *Email {
tb.Helper()
return &Email{
user: "user",
host: "example.com",
}
}
2022-01-29 17:50:45 +00:00
// String returns string representation of email identifier.
2022-01-08 20:07:14 +00:00
func (e Email) String() string {
return e.user + "@" + e.host
}