Skip to content

Instantly share code, notes, and snippets.

@cmdrkeene
Created January 22, 2015 16:59
Show Gist options
  • Save cmdrkeene/45863376fe7f23fdec35 to your computer and use it in GitHub Desktop.
Save cmdrkeene/45863376fe7f23fdec35 to your computer and use it in GitHub Desktop.
Don't use primitives for things like email
// 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