models

package
v0.0.0-...-17ed6e2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func IsErrBadEffect(err error) bool

IsErrBadEffect returns true if the error is an ErrBadEffect.

func IsErrEmpty

func IsErrEmpty(err error) bool

IsErrEmpty returns true if the error is an ErrEmpty.

func IsErrInvalid

func IsErrInvalid(err error) bool

IsErrInvalid returns true if the error is an ErrInvalid.

func IsErrNoEntry

func IsErrNoEntry(err error) bool

IsErrNoEntry returns true if the error is an ErrNoEntry.

func ShouldAffect

func ShouldAffect(name string, res sql.Result, expected int64) error

ShouldAffect takes an sql.Result and returns an error if the number of rows affected is different from what was expected.

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.

func Cache

func Cache(item Cacheable, identifier interface{}) (Cacheable, error)

Cache takes an empty instance of a cacheable items and an identifier for the wanted instance, returning a cached instance or attempting to fetch the item if it is not cached. If an error occurs it is returned.

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.

func (*ErrEmpty) Error

func (err *ErrEmpty) Error() string

Error implements the error interface for ErrEmpty.

type ErrInvalid

type ErrInvalid struct {
	Model string
	Which string
	Value string
}

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

func AuthenticateUser(email, password string) (*User, error)

AuthenticateUser takes an email and a plaintext password and returns the matching user. If no matching user exists, an ErrNoEntry is returned.

func GetUser

func GetUser(emailOrID interface{}) (*User, error)

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

func ListUser() ([]User, error)

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

func NewUser(name, email, password string) (*User, error)

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

func UserFromContext(ctx context.Context) (*User, bool)

UserFromContext returns the User value stored in a context, if any.

func (*User) Authenticate

func (user *User) Authenticate(password string) error

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

func (user *User) Delete() error

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

func (user *User) NewContext(parent context.Context) context.Context

NewContext returns a new context.Context that carries this user instance.

func (*User) Refresh

func (user *User) Refresh(identifier interface{}) error

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

func (user *User) Save() error

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

func (user *User) SetPassword(password string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL