Documentation
¶
Index ¶
- Variables
- func IsErrBadEffect(err error) bool
- func IsErrEmpty(err error) bool
- func IsErrInvalid(err error) bool
- func IsErrNoEntry(err error) bool
- func ShouldAffect(name string, res sql.Result, expected int64) error
- type Cacheable
- type ErrBadEffect
- type ErrEmpty
- type ErrInvalid
- type ErrNoEntry
- type User
Constants ¶
This section is empty.
Variables ¶
var ( // ValidUserName is regex to check if a user's name is valid. ValidUserName = regexp.MustCompile("^(?:[a-zA-Z,.'-]+ ?)+$") // ValidUserEmail is regex to check if a user's email is valid. ValidUserEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") // ValidUserPassword is regex to check if a user's password is valid. ValidUserPassword = regexp.MustCompile("^.{8,}$") )
Functions ¶
func IsErrBadEffect ¶
IsErrBadEffect returns true if the error is an ErrBadEffect.
func IsErrEmpty ¶
IsErrEmpty returns true if the error is an ErrEmpty.
func IsErrInvalid ¶
IsErrInvalid returns true if the error is an ErrInvalid.
func IsErrNoEntry ¶
IsErrNoEntry returns true if the error is an ErrNoEntry.
Types ¶
type Cacheable ¶
type Cacheable interface {
Refresh(interface{}) error
}
Cacheable is any type containing a method to refresh the contents of the instance given an identifier.
type ErrBadEffect ¶
type ErrBadEffect struct { Name string // a name to identify the caller Affected int64 // the number of rows that were affected Expected int64 // the number of rows expected to be affected }
ErrBadEffect is returned when some SQL affects an unexpected number of rows.
func (*ErrBadEffect) Error ¶
func (err *ErrBadEffect) Error() string
Error implements the error interface for ErrBadEffect.
type ErrEmpty ¶
type ErrEmpty struct {
Name string // the name of the table
}
ErrEmpty is returned when a table is empty.
type ErrInvalid ¶
ErrInvalid is returned when a field of some model is invalid.
func (*ErrInvalid) Error ¶
func (err *ErrInvalid) Error() string
Error implements the error interface for ErrInvalid.
type ErrNoEntry ¶
type ErrNoEntry struct { Type string // the type of the entry Identifier interface{} // identifier used to find the entry, usually unique }
ErrNoEntry is returned when a requested entry does not exist.
func (*ErrNoEntry) Error ¶
func (err *ErrNoEntry) Error() string
Error implements the error interface for ErrNoEntry.
type User ¶
type User struct { ID uint64 Created time.Time Modified time.Time Name string Email string Password []byte }
User identifies an account.
func AuthenticateUser ¶
AuthenticateUser takes an email and a plaintext password and returns the matching user. If no matching user exists, an ErrNoEntry is returned.
func GetUser ¶
GetUser fetches a User from the database by email or by ID. If no such user exists or something other than a string or integer is passed to GetUser, an error is returned.
func ListUser ¶
ListUser returns an array of all Users in the database. If the user table is empty an ErrEmpty is returned. If anything else goes wrong it is returned.
func NewUser ¶
NewUser takes a name, email, and plaintext password and returns a new User. If an error occurs while hashing the password, it is returned. If validation of the provided fields fails, an ErrInvalid is returned.
func UserFromContext ¶
UserFromContext returns the User value stored in a context, if any.
func (*User) Authenticate ¶
Authenticate takes a plaintext password and compares it with the hashed password stored. Returns nil on succcess or an error on failure.
func (*User) Delete ¶
Delete removes the user from the database. If the ID field is 0 an ErrNoEntry is returned. If any other errors occurs it is returned.
func (*User) NewContext ¶
NewContext returns a new context.Context that carries this user instance.
func (*User) Refresh ¶
Refresh updates the user object to be equivalent to the corresponding database entry. The provided identifier must be allowed by GetUser. If an error occurs it is returned.
func (*User) Save ¶
Save propagates any changes back to the database. If the ID field is 0, a new entry is created. Otherwise, Save attempts to update an existing entry. If anything goes wrong an error is returned. If the user's name or email is invalid, an ErrInvalid is returned.
func (*User) SetPassword ¶
SetPassword takes a plaintext password and hashes it before storing it in the password field. If the plaintext password does not meet the requirements an ErrInvalid is returned. If an error occurs while hashing the password, it is returned.