Created
January 22, 2015 16:59
-
-
Save cmdrkeene/45863376fe7f23fdec35 to your computer and use it in GitHub Desktop.
Don't use primitives for things like email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An electronic mail address | |
type email struct { | |
s string | |
} | |
var invalidEmail = errors.New("invalid email") | |
func newEmail(s string) (email, error) { | |
s = strings.Trim(s, " ") | |
if len(s) < 3 { | |
return email{}, invalidEmail | |
} | |
if !strings.Contains(s, "@") { | |
return email{}, invalidEmail | |
} | |
return email{s: s}, nil | |
} | |
func (e *email) Scan(src interface{}) error { | |
b, ok := src.([]byte) | |
if !ok { | |
err := errors.New( | |
fmt.Sprintf("can't scan email from %#v", src), | |
) | |
glog.Error(err) | |
return err | |
} | |
email, err := newEmail(string(b)) | |
if err != nil { | |
return err | |
} | |
e.s = email.s | |
return nil | |
} | |
func (e email) Value() (driver.Value, error) { | |
return driver.Value(e.s), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment