Documentation ¶
Overview ¶
Package service provides utilities used for implementing services.
Index ¶
- Constants
- Variables
- func AppResponder(w http.ResponseWriter, r *http.Request, v interface{})
- func CheckInt64JsonHasStringTag(obj interface{})
- func ConvertDBTimeToJSONTime(data interface{}) string
- func ConvertMapFromDBToJSON(dbMap map[string]interface{}) map[string]interface{}
- func ConvertSliceOfMapsFromDBToJSON(dbMaps []map[string]interface{}) []map[string]interface{}
- func CreationSuccess(data interface{}) render.Renderer
- func DeletionSuccess(data interface{}) render.Renderer
- func MustNotBeError(err error)
- func NotFound(w http.ResponseWriter, r *http.Request)
- func ParsePagingParameters(r *http.Request, expectedFromFields map[string]FieldType) (map[string]interface{}, error)
- func ParticipantIDFromContext(ctx context.Context) int64
- func ParticipantMiddleware(srv GetStorer) func(next http.Handler) http.Handler
- func ResolveURLQueryGetBoolField(httpReq *http.Request, name string) (bool, error)
- func ResolveURLQueryGetBoolFieldWithDefault(httpReq *http.Request, name string, defaultValue bool) (bool, error)
- func ResolveURLQueryGetInt64Field(httpReq *http.Request, name string) (int64, error)
- func ResolveURLQueryGetInt64SliceField(req *http.Request, paramName string) ([]int64, error)
- func ResolveURLQueryGetStringField(httpReq *http.Request, name string) (string, error)
- func ResolveURLQueryGetStringSliceField(req *http.Request, paramName string) ([]string, error)
- func ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters(r *http.Request, fieldName string, knownValuesMap map[string]bool) ([]string, error)
- func ResolveURLQueryGetTimeField(httpReq *http.Request, name string) (time.Time, error)
- func ResolveURLQueryPathInt64Field(httpReq *http.Request, name string) (int64, error)
- func ResolveURLQueryPathInt64SliceField(req *http.Request, paramName string) ([]int64, error)
- func ResolveURLQueryPathInt64SliceFieldWithLimit(r *http.Request, paramName string, limit int) ([]int64, error)
- func URLQueryPathHasField(httpReq *http.Request, name string) bool
- func UnchangedSuccess(httpStatus int) render.Renderer
- func UpdateSuccess(data interface{}) render.Renderer
- type APIError
- func ApplySortingAndPaging(r *http.Request, query *database.DB, parameters *SortingAndPagingParameters) (*database.DB, APIError)
- func ErrConflict(err error) APIError
- func ErrForbidden(err error) APIError
- func ErrInvalidRequest(err error) APIError
- func ErrNotFound(err error) APIError
- func ErrUnexpected(err error) APIError
- func ErrUnprocessableEntity(err error) APIError
- func GetParticipantIDFromRequest(httpReq *http.Request, user *database.User, store *database.DataStore) (int64, APIError)
- type AppHandler
- type Base
- type ErrorResponse
- type FieldSortingParams
- type FieldType
- type GetStorer
- type QueryLimiter
- type Response
- type SortingAndPagingFields
- type SortingAndPagingParameters
- type SortingAndPagingTieBreakers
Constants ¶
const FromFirstRow = iota
FromFirstRow is a special value of SortingAndPagingParameters.StartFromRowSubQuery needed to bypass pagination completely and ignore 'from.*' parameters of the HTTP request.
Variables ¶
var InsufficientAccessRightsError = ErrForbidden(errors.New("insufficient access rights"))
InsufficientAccessRightsError is an APIError to be returned when the has no access rights to perform an action.
var NoError = APIError{0, nil}
NoError is an APIError to be returned when there is no error.
Functions ¶
func AppResponder ¶ added in v1.12.0
func AppResponder(w http.ResponseWriter, r *http.Request, v interface{})
AppResponder serializes the output of services. Whenever in test env, it also checks that all int64/uint64 are serialized as strings, otherwise, it breaks javascript that tries to convert them as floats.
func CheckInt64JsonHasStringTag ¶ added in v1.12.0
func CheckInt64JsonHasStringTag(obj interface{})
CheckInt64JsonHasStringTag checks that all fields of obj, recursively, which are int64/uint64, and are exported as JSON, also have the "string" tag. Otherwise, it breaks javascript that tries to convert them as floats. This function is only intended to be used in test env, and panics if the checks fail.
func ConvertDBTimeToJSONTime ¶
func ConvertDBTimeToJSONTime(data interface{}) string
ConvertDBTimeToJSONTime converts the DB datetime representation to RFC3339.
func ConvertMapFromDBToJSON ¶
ConvertMapFromDBToJSON given a map that represents DB result data, converts it a map for rendering JSON so that: 1) all map keys with "__" are considered as paths in JSON (converts "User__ID":... to "user":{"id": ...}) 2) all map keys are converted to snake case 3) prefixes are stripped, values are converted to needed types accordingly.
func ConvertSliceOfMapsFromDBToJSON ¶
ConvertSliceOfMapsFromDBToJSON given a slice of maps that represents DB result data, converts it to a slice of maps for rendering JSON so that: 1) all maps keys with "__" are considered as paths in JSON (converts "User__ID":... to "user":{"id": ...}) 2) all maps keys are converted to snake case 3) prefixes are stripped, values are converted to needed types accordingly.
func CreationSuccess ¶
CreationSuccess generated a success response for a POST creation.
func DeletionSuccess ¶
DeletionSuccess generated a success response for a DELETE deletion.
func NotFound ¶
func NotFound(w http.ResponseWriter, r *http.Request)
NotFound is a basic HTTP handler which generates a 404 error.
func ParsePagingParameters ¶
func ParsePagingParameters(r *http.Request, expectedFromFields map[string]FieldType) (map[string]interface{}, error)
ParsePagingParameters returns a map with values provided for paging in a request URL (none or all should be present).
func ParticipantIDFromContext ¶
ParticipantIDFromContext retrieves a participant id set by the middleware from a context.
func ParticipantMiddleware ¶
ParticipantMiddleware is a middleware retrieving a participant from the request content. The participant id is the `as_team_id` parameter value if it is given or the user's `group_id` otherwise. If `as_team_id` is given, it should be an id of a team and the user should be a member of this team, otherwise the 'forbidden' error is returned.
func ResolveURLQueryGetBoolField ¶
ResolveURLQueryGetBoolField extracts a get-parameter of type bool (0 or 1) from the query, fails if the value is empty.
func ResolveURLQueryGetBoolFieldWithDefault ¶ added in v1.9.3
func ResolveURLQueryGetBoolFieldWithDefault(httpReq *http.Request, name string, defaultValue bool) (bool, error)
ResolveURLQueryGetBoolFieldWithDefault extracts a get-parameter of type bool (0 or 1) from the query. If it is not provided, `defaultValue` is returned.
func ResolveURLQueryGetInt64Field ¶
ResolveURLQueryGetInt64Field extracts a get-parameter of type int64 from the query.
func ResolveURLQueryGetInt64SliceField ¶
ResolveURLQueryGetInt64SliceField extracts from the query parameter of the request a list of integer separated by commas (',') returns `nil` for no IDs.
func ResolveURLQueryGetStringField ¶
ResolveURLQueryGetStringField extracts a get-parameter of type string from the query, fails if the value is empty.
func ResolveURLQueryGetStringSliceField ¶
ResolveURLQueryGetStringSliceField extracts from the query parameter of the request a list of strings separated by commas (',') returns `nil` the parameter is missing.
func ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters ¶
func ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters( r *http.Request, fieldName string, knownValuesMap map[string]bool, ) ([]string, error)
ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters extracts a list of values out from '<fieldName>_include'/'<fieldName>_exclude' request parameters:
- If none of '<fieldName>_include'/'<fieldName>_exclude' is present, all the known values are returned.
- If '<fieldName>_include' is present, then it becomes the result list.
- If '<fieldName>_exclude' is present, then we exclude all its values from the result list.
All values from both the request parameters are checked against the list of known values.
func ResolveURLQueryGetTimeField ¶
ResolveURLQueryGetTimeField extracts a get-parameter of type time.Time (rfc3339) from the query.
func ResolveURLQueryPathInt64Field ¶
ResolveURLQueryPathInt64Field extracts a path element of type int64 from the query.
func ResolveURLQueryPathInt64SliceField ¶
ResolveURLQueryPathInt64SliceField extracts a list of integers separated by commas (',') from the query path of the request.
func ResolveURLQueryPathInt64SliceFieldWithLimit ¶
func ResolveURLQueryPathInt64SliceFieldWithLimit(r *http.Request, paramName string, limit int) ([]int64, error)
ResolveURLQueryPathInt64SliceFieldWithLimit extracts a list of integers separated by commas (',') from the query path of the request applying the given limit.
func URLQueryPathHasField ¶ added in v1.9.0
URLQueryPathHasField checks whether a field is present in the query.
func UnchangedSuccess ¶
UnchangedSuccess generated a success response for a POST/PUT/DELETE action if no data have been modified.
func UpdateSuccess ¶
UpdateSuccess generated a success response for a PUT updating.
Types ¶
type APIError ¶
APIError represents an error as returned by this application. It works in tandem with AppHandler for easy handling of errors.
func ApplySortingAndPaging ¶
func ApplySortingAndPaging(r *http.Request, query *database.DB, parameters *SortingAndPagingParameters) (*database.DB, APIError)
ApplySortingAndPaging applies ordering and paging according to the given parameters taking into the account the URL parameters 'from.*' if needed.
When `parameters.IgnoreSortParameter` is true, the 'sort' request parameter is ignored.
When `parameters.StartFromRowSubQuery` is set, the 'from.*' request parameters are ignored.
func ErrConflict ¶
ErrConflict is for errors caused by wrong resource state not allowing to perform the operation It results in a 409 Conflict.
func ErrForbidden ¶
ErrForbidden is for errors caused by a lack of permissions globally or on a requested object It results in a 403 Forbidden.
func ErrInvalidRequest ¶
ErrInvalidRequest is for errors caused by invalid request input It results in a 400 Invalid request response.
func ErrNotFound ¶
ErrNotFound is for errors caused by absence of a requested object It results in a 404 Not Found.
func ErrUnexpected ¶
ErrUnexpected is for internal errors (not supposed to fail) not directly caused by the user input It results in a 500 Internal Server Error response.
func ErrUnprocessableEntity ¶
ErrUnprocessableEntity is for errors caused by our inability to perform data modifications for some reason It results in a 422 Unprocessable Entity.
func GetParticipantIDFromRequest ¶
func GetParticipantIDFromRequest(httpReq *http.Request, user *database.User, store *database.DataStore) (int64, APIError)
GetParticipantIDFromRequest returns `as_team_id` parameter value if it is given or the user's `group_id` otherwise. If `as_team_id` is given, it should be an id of a team and the user should be a member of this team, otherwise the 'forbidden' error is returned.
type AppHandler ¶
type AppHandler func(http.ResponseWriter, *http.Request) APIError
AppHandler is a type that implements http.Handler and makes handling errors easier. When its method returns an error, it prints it to the logs and shows a JSON formatted error to the user.
func (AppHandler) ServeHTTP ¶
func (fn AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Base ¶
type Base struct { ServerConfig *viper.Viper AuthConfig *viper.Viper DomainConfig []domain.ConfigItem TokenConfig *token.Config // contains filtered or unexported fields }
Base is the common service context data.
func (*Base) GetStore ¶ added in v1.3.0
GetStore returns a data store with the given request's context.
func (*Base) ResolveWatchedGroupID ¶
func (srv *Base) ResolveWatchedGroupID(httpReq *http.Request) (watchedGroupID int64, ok bool, apiError APIError)
ResolveWatchedGroupID returns the watched_group_id parameter (if given) and checks if the current user has rights to watch for its members.
func (*Base) SetGlobalStore ¶ added in v1.3.0
SetGlobalStore sets the global store shared by all the request (should be called only once on start).
type ErrorResponse ¶
type ErrorResponse struct { Response ErrorText string `json:"error_text,omitempty"` // application-level error message, for debugging Errors interface{} `json:"errors,omitempty"` // form errors }
ErrorResponse is an extension of the response for returning errors.
type FieldSortingParams ¶
type FieldSortingParams struct { // ColumnName is a DB column name (should contain a table name as a prefix, e.g. "groups.id") ColumnName string // Nullable means that the field can be null Nullable bool }
FieldSortingParams represents sorting parameters for one field.
type GetStorer ¶ added in v1.3.0
GetStorer is an interface allowing to get a data store bound to the context of the given request.
type QueryLimiter ¶
type QueryLimiter struct {
// contains filtered or unexported fields
}
QueryLimiter applies the limit parameter from an HTTP request to a given DB query (see QueryLimiter.Apply()).
func NewQueryLimiter ¶
func NewQueryLimiter() *QueryLimiter
NewQueryLimiter creates a QueryLimiter with default settings:
- default limit is 500,
- maximum allowed limit is 1000.
func (*QueryLimiter) Apply ¶
Apply limits the number of records of the given DB query according to the `limit` request parameter.
func (*QueryLimiter) SetDefaultLimit ¶
func (ql *QueryLimiter) SetDefaultLimit(defaultLimit int64) *QueryLimiter
SetDefaultLimit sets the default limit used when the 'limit' request parameter is missing.
func (*QueryLimiter) SetMaxAllowedLimit ¶
func (ql *QueryLimiter) SetMaxAllowedLimit(maxAllowedLimit int64) *QueryLimiter
SetMaxAllowedLimit sets the maximum allowed limit.
type Response ¶
type Response struct { HTTPStatusCode int `json:"-"` Success bool `json:"success"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` }
Response is used for generating non-data responses, i.e. on error or on POST/PUT/PATCH/DELETE request.
type SortingAndPagingFields ¶
type SortingAndPagingFields map[string]*FieldSortingParams
SortingAndPagingFields is a type of SortingAndPagingParameters.Fields (field_name -> params).
type SortingAndPagingParameters ¶
type SortingAndPagingParameters struct { Fields SortingAndPagingFields DefaultRules string TieBreakers SortingAndPagingTieBreakers // If IgnoreSortParameter is true, the 'sort' parameter of the HTTP request is ignored IgnoreSortParameter bool // If StartFromRowSubQuery is not nil, 'from.*' parameters of the HTTP request are not analyzed, // the sub-query should provide tie-breaker fields instead. // // If StartFromRowSubQuery is FromFirstRow, pagination gets skipped. StartFromRowSubQuery interface{} }
SortingAndPagingParameters represents sorting and paging parameters to apply.
type SortingAndPagingTieBreakers ¶
SortingAndPagingTieBreakers is a type of SortingAndPagingParameters.TieBreakers (field_name -> type).