utils

package
v0.1.8-1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentType = "application/json"
	Charset     = "utf-8"
)
View Source
const ZULU_FORM = "2006-01-02T15:04:05Z"

Variables

View Source
var APIErrBadRequest = func(msg string) *APIError {
	msg = fmt.Sprintf("Poorly formatted JSON. %v", msg)
	return &APIError{msg, 400, "BAD REQUEST"}
}
View Source
var APIErrConflict = func(resource string, field string, value string) *APIError {
	msg := fmt.Sprintf("%v object with %v: %v already exists", resource, field, value)
	return &APIError{msg, 409, "CONFLICT"}
}
View Source
var APIErrDatabase = func(msg string) *APIError {
	msg = fmt.Sprintf("Database Error: %v", msg)
	return &APIError{msg, 500, "INTERNAL SERVER ERROR"}
}
View Source
var APIErrEmptyRequiredField = func(resource string, msg string) *APIError {
	return &APIError{fmt.Sprintf("%v object contains empty fields. %v", resource, msg), 422, "UNPROCESSABLE ENTITY"}
}
View Source
var APIErrInvalidFieldContent = func(field string, reason string) *APIError {
	msg := fmt.Sprintf("Field: %v contains invalid data. %v", field, reason)
	return &APIError{msg, 422, "UNPROCESSABLE ENTITY"}
}
View Source
var APIErrNotFound = func(resource string) *APIError {
	msg := fmt.Sprintf("%v was not found", resource)
	return &APIError{msg, 404, "NOT FOUND"}
}
View Source
var APIErrUnauthorized = func(msg string) *APIError {
	return &APIError{msg, 401, "UNAUTHORIZED"}
}
View Source
var APIErrUnsupportedContent = func(place, content string, supported string) *APIError {
	msg := fmt.Sprintf("%v: %v is not yet supported.%v", place, content, supported)
	return &APIError{msg, 422, "UNPROCESSABLE ENTITY"}
}
View Source
var APIErrUnsupportedContentNonVerbose = func(place, content string) *APIError {
	msg := fmt.Sprintf("%v: %v is not yet supported", place, content)
	return &APIError{msg, 422, "UNPROCESSABLE ENTITY"}
}
View Source
var APIGenericInternalError = func(msg string) error {
	return &APIError{"Internal Error: " + msg, 500, "INTERNAL SERVER ERROR"}
}
View Source
var GenericEmptyRequiredField = func(fieldName string) error {
	return errors.New(fmt.Sprintf("empty value for field: %v", fieldName))
}
View Source
var StructGenericEmptyRequiredField = func(strct string, reason string) error {
	return errors.New(fmt.Sprintf("%v object contains empty fields. %v", strct, reason))
}

also contains struct information

Functions

func CopyFields

func CopyFields(from interface{}, to interface{}) error

CopyFields finds same named field between two structs and copies the values from one to an other

func GetFieldValueByName

func GetFieldValueByName(instance interface{}, fieldName string) (interface{}, error)

GetFieldValueByName retrieves the value of a specified field from the provided instance

func IsCapitalized

func IsCapitalized(str string) bool

IsCapitalized returns whether or not not a string is capitalized

func RespondError

func RespondError(w http.ResponseWriter, err error)

func RespondOk

func RespondOk(w http.ResponseWriter, code int, data interface{})

func SetFieldValueByName

func SetFieldValueByName(instance interface{}, fieldName string, value interface{}) error

SetFieldValueByName assigns a value to the specified field of the given interface

func StructToMap

func StructToMap(instance interface{}) map[string]interface{}

StructToMap converts a non nil struct to a map of map[string]interface{}

func ValidateRequired

func ValidateRequired(instance interface{}) error

ValidateRequired accepts an instance of any type and checks whether or not all fields are filled

func ZuluTimeNow

func ZuluTimeNow() string

ZuluTimeNow returns the current UTC time in zulu format

Types

type APIError

type APIError struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
	Status  string `json:"status"`
}

func (*APIError) Error

func (e *APIError) Error() string

type ErrResp

type ErrResp struct {
	APIErr *APIError `json:"error"`
}

Jump to

Keyboard shortcuts

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