helpers

package
v0.1.31-0...-b1e5c7a Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2018 License: ISC Imports: 7 Imported by: 0

Documentation

Overview

Package helpers provides a set of utility functions used across gonawin app.

Index

Constants

View Source
const (
	// generic
	ErrorCodeNotSupported      = "Not Supported"
	ErrorCodeInternal          = "Internal error"
	ErrorCodeNotFound          = "Not Found"
	ErrorCodeNameCannotBeEmpty = "Name field cannot be empty"

	// sessions
	ErrorCodeSessionsAccessTokenNotValid      = "Access token is not valid"
	ErrorCodeSessionsForbiden                 = "You are not authorized to log in to gonawin"
	ErrorCodeSessionsUnableToSignin           = "Error occurred during signin process"
	ErrorCodeSessionsCannotGetTempCredentials = "Error getting temporary credentials"
	ErrorCodeSessionsCannotSetSecretValue     = "Error setting 'secret' value"
	ErrorCodeSessionsCannotGetSecretValue     = "Error getting 'secret' value"
	ErrorCodeSessionsCannotGetRequestToken    = "Error getting request token"
	ErrorCodeSessionsCannotGetUserInfo        = "Error getting user info from Twitter"
	ErrorCodeSessionsCannotGetGoogleLoginURL  = "Error getting Google accounts login URL"

	// users
	ErrorCodeUserNotFound                      = "User not found"
	ErrorCodeUserNotFoundCannotUpdate          = "User not found, unable to update"
	ErrorCodeUserNotFoundCannotDelete          = "User not found, unable to delete"
	ErrorCodeUserNotFoundCannotInvite          = "User not found, unable to send invitation"
	ErrorCodeUserCannotUpdate                  = "Could not update user"
	ErrorCodeUsersCannotUpdate                 = "Could not update users"
	ErrorCodeUsersCannotPublishScore           = "Could not pusblish score activities"
	ErrorCodeUserIsTeamAdminCannotDelete       = "User cannot be deleted because he is team admin"
	ErrorCodeUserIsTournamentAdminCannotDelete = "User cannot be deleted because he is tournament admin"
	// teams
	ErrorCodeTeamAlreadyExists        = "Sorry, that team already exists"
	ErrorCodeTeamCannotCreate         = "Could not create the team"
	ErrorCodeTeamNotFound             = "Team not found"
	ErrorCodeTeamNotFoundCannotUpdate = "Team not found, unable to update"
	ErrorCodeTeamNotFoundCannotDelete = "Team not found, unable to delete"
	ErrorCodeTeamNotFoundCannotInvite = "Team not found, unable to send invitation"
	ErrorCodeTeamUpdateForbiden       = "Team can only be updated by the team administrator"
	ErrorCodeTeamDeleteForbiden       = "Team can only be deleted by the team administrator"
	ErrorCodeTeamCannotUpdate         = "Could not update team"
	ErrorCodeTeamCannotInvite         = "Could not send invitation"
	ErrorCodeTeamRequestNotFound      = "Request not found"
	ErrorCodeTeamMemberNotFound       = "Member not found"
	ErrorCodeTeamAdminCannotLeave     = "Team administrator cannot leave the team"
	ErrorCodeTeamPrivateJoinForbiden  = "Private Team cannot be joined without consent. Please request an invitation"
	ErrorCodeTeamRequestAlreadySent   = "Sorry, you already requested an invitation"
	//tournaments
	ErrorCodeTournamentAlreadyExists          = "Sorry, that tournament already exists"
	ErrorCodeTournamentCannotCreate           = "Could not create the team"
	ErrorCodeTournamentNotFound               = "Tournament not found"
	ErrorCodeTournamentNotFoundCannotUpdate   = "Tournament not found, unable to update"
	ErrorCodeTournamentNotFoundCannotDelete   = "Tournament not found, unable to delete"
	ErrorCodeTournamentUpdateForbiden         = "Tournament can only be updated by the team administrator"
	ErrorCodeTournamentDeleteForbiden         = "Tournament can only be deleted by the team administrator"
	ErrorCodeTournamentCannotUpdate           = "Could not update tournament"
	ErrorCodeTournamentCannotSearch           = "Something went wrong, we are unable to perform search query"
	ErrorCodeMatchCannotUpdate                = "Something went wrong, unable to update match"
	ErrorCodeMatchesCannotUpdate              = "Something went wrong, unable to update matches"
	ErrorCodeMatchNotFoundCannotUpdate        = "Match not found, unable to update match"
	ErrorCodeMatchNotFound                    = "Match not found"
	ErrorCodeMatchNotFoundCannotSetPrediction = "Match not found, unable to set prediction"
	ErrorCodeCannotSetPrediction              = "Something went wrong, unable to set prediction"
	ErrorCodeNotAllowedToSetPrediction        = "You have to join the tournament to be able to set a predict for this match"
	ErrorCodeTeamsCannotUpdate                = "Could not update teams"

	// invite
	ErrorCodeInviteNoEmailAddr     = "No email address has been entered"
	ErrorCodeInviteEmailsInvalid   = "Emails list is not properly formatted"
	ErrorCodeInviteEmailCannotSend = "Sorry, we were unable to send the Email"
)

errors

Variables

View Source
var PasswordRegexp = regexp.MustCompile(`^.{3,20}$`)

PasswordRegexp is the regexp for password.

View Source
var UsernameRegExp = regexp.MustCompile(`^[\sa-zA-Z0-9_-]{3,20}$`)

UsernameRegExp is the regexp for username.

Functions

func AreEmailsValid

func AreEmailsValid(emails []string) bool

AreEmailsValid checks if an array of emails is valid.

func ArgMaxInt64

func ArgMaxInt64(arr []int64) (index int, max int64)

ArgMaxInt64 returns the index and the max value.

func Contains

func Contains(s []int64, value int64) (bool, int)

Contains indicates if a value exists in a given slice. If the value exists, its position in the slice is returned otherwise -1.

func CopyToPointerStructure

func CopyToPointerStructure(tSrc interface{}, tDest interface{})

CopyToPointerStructure copies structure fields to structure pointer fields. from two structures source and destination. structures defined as follows: both structures have the same fields destination structure field types are pointers of the source types example:

type TA struct {
	Field1      string
	Field2      string
	IsSomething bool
}
type TB struct {
	Field1      *string `json:",omitempty"`
	Field2      *string `json:",omitempty"`
	IsSomething *bool   `json:",omitempty"`
}

use source structure to build destination structure

func CountTerm

func CountTerm(words []string, w string) int64

CountTerm counts the occurrence of a word into a slice of words.

func Error404

func Error404(w http.ResponseWriter)

Error404 writes a 404 not found in ResponseWriter.

func InitPointerStructure

func InitPointerStructure(pSrc interface{}, pDest interface{}, fieldsToKeep arrayOfStrings)

InitPointerStructure initializes structure with pointer fields from two structures, source and destination. structures defined as follows: both structures have the same fields destination structure field types are pointers of the source types example:

type TA struct {
	Field1      string
	Field2      string
	IsSomething bool
}
type TB struct {
	Field1      *string `json:",omitempty"`
	Field2      *string `json:",omitempty"`
	IsSomething *bool   `json:",omitempty"`
}

use source structure to build destination structure and at the same time set to nil all fields of Destination structure not present in array fieldsToKeep we suppose that pDest is a structure of pointer types and fieldsToKeep is an array of the fields you wish to keep.

func Insert

func Insert(s []int64, value int64, i int64) []int64

Insert inserts new value into a slice at a given position source: https://code.google.com/p/go-wiki/wiki/SliceTricks

func Intersect

func Intersect(a string, b string) string

Intersect computes the instersection of two strings. From two strings with format "str1 str2" and "str2 str3" in this example the result is "str2".

func IsEmailValid

func IsEmailValid(email string) bool

IsEmailValid returns true if string email has the form a@b.c

func IsPasswordValid

func IsPasswordValid(password string) bool

IsPasswordValid returns true for all passwords that are between 3 and 20 characters.

func IsStringValid

func IsStringValid(s string) bool

IsStringValid returns true if string is not empty.

func IsUsernameValid

func IsUsernameValid(username string) bool

IsUsernameValid returns true is username is a string between 3 and 20 characters. note: added \s just temporary until user name is splited with name and last name username should not have whitespaces

func KeepFields

func KeepFields(t interface{}, fieldsToKeep arrayOfStrings)

KeepFields sets to nil all fields of t structure not present in array. We suppose that t is a structure of pointer types and fieldsToKeep is an array of the fields you wish to keep.

func MergeIds

func MergeIds(teamIds []byte, id int64) string

MergeIds merges ids in slice of byte with id if it is not already there if id is already in the slice return empty string.

func RemovefromIds

func RemovefromIds(teamIds []byte, id int64) (string, error)

RemovefromIds removes id from slice of byte with ids.

func SetOfStrings

func SetOfStrings(s string) []string

SetOfStrings returns a set of string for a given strings.

func SliceContains

func SliceContains(slice []string, s string) bool

SliceContains checks if a given string exists in a slice of strings.

func StringContains

func StringContains(strToSplit string, s string) bool

StringContains checks if a given string exists in a string.

func TeamImageURL

func TeamImageURL(name string, id int64) string

TeamImageURL returns the team image URL for a given name and id.

func TournamentImageURL

func TournamentImageURL(name string, id int64) string

TournamentImageURL returns the tournament image URL for a given name and id.

func TransformFromArrayOfPointers

func TransformFromArrayOfPointers(pArraySrc interface{}, pArrayDest interface{}, fieldsToKeep arrayOfStrings)

TransformFromArrayOfPointers works like InitPointerStructure but for arrays. source array has values.

func TransformFromArrayOfValues

func TransformFromArrayOfValues(pArraySrc interface{}, pArrayDest interface{}, fieldsToKeep arrayOfStrings)

TransformFromArrayOfValues works like InitPointerStructure but for arrays. source array has pointers.

func TrimLower

func TrimLower(s string) string

TrimLower returns a lower case slice of the string s, with all leading and trailing white space removed, as defined by Unicode.

func UserImageURL

func UserImageURL(name string, id int64) string

UserImageURL returns the user image URL for a given name and id.

Types

type BadRequest

type BadRequest struct {
	Err error
}

BadRequest is handled by setting the status code in the reply to StatusBadRequest.

func (*BadRequest) Error

func (e *BadRequest) Error() string

Implementation of error, returns string error on Err structure.

type Forbidden

type Forbidden struct {
	Err error
}

Forbidden is handled by setting the status code in the reply to StatusForbidden.

func (*Forbidden) Error

func (e *Forbidden) Error() string

Implementation of error, returns string error on Err structure.

type InternalServerError

type InternalServerError struct {
	Err error
}

InternalServerError is handled by setting the status code in the reply to StatusInternalServerError.

func (*InternalServerError) Error

func (e *InternalServerError) Error() string

Implementation of error, returns string error on Err structure.

type NotFound

type NotFound struct {
	Err error
}

NotFound is handled by setting the status code in the reply to StatusNotFound.

func (*NotFound) Error

func (e *NotFound) Error() string

Implementation of error, returns string error on Err structure.

type Unauthorized

type Unauthorized struct {
	Err error
}

Unauthorized is handled by setting the status code in the reply to StatusUnauthorized.

func (*Unauthorized) Error

func (e *Unauthorized) Error() string

Implementation of error, returns string error on Err structure.

Directories

Path Synopsis
Package auth provides the functions needed to get a user information.
Package auth provides the functions needed to get a user information.
Package handlers provides a set of functions to manipulate http.HandlerFunc at a route level.
Package handlers provides a set of functions to manipulate http.HandlerFunc at a route level.
Package log provide a set of function to log in gonawin app.
Package log provide a set of function to log in gonawin app.
Package memcache provides a set of functions to use memcache in gonawin.
Package memcache provides a set of functions to use memcache in gonawin.
Package templates provides a set of functions to encode data in a http.ResponseWriter.
Package templates provides a set of functions to encode data in a http.ResponseWriter.

Jump to

Keyboard shortcuts

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