utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2018 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HookTypeCollection specify the `collection` type hook.
	HookTypeCollection = "collection"

	// HookTypeEntity specify the `entity` type hook.
	HookTypeEntity = "entity"

	// HookActionCreate specify the `create` action hook.
	HookActionCreate = "create"

	// HookActionUpdate specify the `update` action hook.
	HookActionUpdate = "update"

	// HookActionDelete specify the `delete` action hook.
	HookActionDelete = "delete"
)
View Source
const FILE_TYPE_AUDIO = "audio"
View Source
const FILE_TYPE_DOC = "doc"
View Source
const FILE_TYPE_IMAGE = "image"
View Source
const FILE_TYPE_OTHER = "other"
View Source
const FILE_TYPE_VIDEO = "video"
View Source
const SearchSpecialChar = "~"

SearchSpecialChar stores the special character used to simulate LIKE query search NB! need to be valid url and non escapable regex string character(s)

Variables

This section is empty.

Functions

func GetExtAndFileTypeByMimeType

func GetExtAndFileTypeByMimeType(data []byte) (string, string)

GetExtAndFileTypeByMimeType returns data extension and file type by its mime type.

func GetMimeTypesByExt

func GetMimeTypesByExt(extensions ...string) []string

GetMimeTypesByExt returns all mime types related to extension(s).

func GetPaginationSettings

func GetPaginationSettings(c *routing.Context, total int) (int, int)

GetPaginationSettings extracts and returns common pagination settings from a request.

func GetSearchConditions

func GetSearchConditions(c *routing.Context, validFields []string) bson.M

GetSearchConditions builds and returns bson collection find condition from a request.

func GetSortFields

func GetSortFields(c *routing.Context, validFields []string) []string

GetSortFields formats and returns sort fields from a request.

func InterfaceToObjectIds

func InterfaceToObjectIds(val interface{}) []bson.ObjectId

InterfaceToObjectIds extracts and converts slice of hex strings or ObjectIds to slice of ObjectIds.

func InterfaceToStrings

func InterfaceToStrings(val interface{}) []string

InterfaceToStrings extracts and converts slice of interfaces to slice of strings.

func MD5

func MD5(str string) string

MD5 hashes using md5 algorithm

func Random

func Random(length int) string

Random generates and returns random string.

func RenderTemplateFiles

func RenderTemplateFiles(data interface{}, files ...string) (string, error)

RenderTemplateFiles renders html templates and returns the result as a string.

func RenderTemplateStrings

func RenderTemplateStrings(data interface{}, content ...string) (string, error)

RenderTemplateStrings resolves inline html template strings.

func SendEmail

func SendEmail(from, to, subject, body string) error

SendEmails sends simple email with html body.

func SendHook

func SendHook(url string, hookType string, hookAction string, hookData interface{}) error

SendHook creates new hook instance and sends a POST request to the specified url.

func SendJsonPostData

func SendJsonPostData(url string, data []byte) error

SendJsonPostData sends json post request data.

func Sentenize

func Sentenize(str string) string

Sentenize converts and normalizes string into a sentence.

func SetPaginationHeaders

func SetPaginationHeaders(c *routing.Context, limit int, total int, currentPage int)

SetPaginationHeaders sets common pagination related headers.

func StringInSlice

func StringInSlice(str string, list []string) bool

StringInSlice checks whether a string exist in array/slice.

func UcFirst

func UcFirst(str string) string

UcFirst converts the first character of a string into uppercase.

func ValidateMimeType

func ValidateMimeType(data []byte, validTypes []string) bool

ValidateMimeType validates data mime type.

func ValidateSize

func ValidateSize(data []byte, maxValidSize float64) bool

ValidateSize validates data size.

Types

type ApiError

type ApiError struct {
	Status  int         `json:"status"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

ApiError defines the properties for a basic api error response.

func NewApiError

func NewApiError(status int, message string, data interface{}) *ApiError

NewApiError creates and returns new normalized ApiError instance.

func NewBadRequestError

func NewBadRequestError(message string, data interface{}) *ApiError

NewBadRequestError creates and returns 400 ApiError.

func NewNotFoundError

func NewNotFoundError(message string) *ApiError

NewNotFoundError creates and returns 404 ApiError.

func (*ApiError) Error

func (e *ApiError) Error() string

Error returns the error message.

func (*ApiError) StatusCode

func (e *ApiError) StatusCode() int

StatusCode returns the HTTP status code.

type DataError

type DataError struct {
	Data interface{} `json:"data"`
}

DataError defines the properties for a basic data error structure.

func NewDataError

func NewDataError(data interface{}) *DataError

NewDataError initilizes and returns a new DataError.

func (*DataError) Error

func (e *DataError) Error() string

Error returns the data error message.

func (*DataError) MarshalJSON

func (e *DataError) MarshalJSON() ([]byte, error)

MarshalJSON handles json serialization

type Hook

type Hook struct {
	Type   string      `json:"type" bson:"type"`
	Action string      `json:"action" bson:"action"`
	Data   interface{} `json:"data" bson:"data"`
}

Hook defines the general hooks format structure.

func NewHook

func NewHook(hookType string, hookAction string, hookData interface{}) *Hook

NewHook creates and returns new Hook instance.

Jump to

Keyboard shortcuts

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