Documentation
¶
Overview ¶
Package dqk provides helper functions to manage: Caching with memcached, dynamic filter for queries, standard responses for pagination, deleted/updated/created assets and deleted cache provides distinct fields struct and an id that can be used for faster lookups using the index of each field. Helper methods that generate cache keys based on applied filters helper functions that execute database queries for update/delete/update and helper methods for pagination both for manual query definition and automatically by wrapping your main query in a CTE. database validation methods for user friendly messaged that can be shown as a response
Index ¶
- Constants
- func AcceptedEncoding(accept string) string
- func AreFiltersAggregate(filters []Filters) bool
- func BuildPermissionSet(userPermissions []string) map[string]bool
- func Cors(next http.Handler) http.Handler
- func DataEncode(Accept string, Data any) ([]byte, string, error)
- func DatabaseValidation(err error) (int, error)
- func DecodeBody(contentType string, body io.Reader, data any) (int, error)
- func DynamicFilters(f []Filters, q sq.SelectBuilder, queryParams map[string][]string) sq.SelectBuilder
- func GetBool(key string, fallback bool) bool
- func GetDate(key string, fallback time.Time) time.Time
- func GetInt(key string, fallback int) int
- func GetPaginationQuery(q sq.SelectBuilder) sq.SelectBuilder
- func GetParamsApplied(filters []Filters, params map[string][]string) map[string]string
- func GetRouteKey(route string, assetID *int, args ...string) (string, string)
- func GetString(key string, fallback string) string
- func GetTime(key string, fallback time.Duration) time.Duration
- func HasPermission(access string, perms map[string]bool) bool
- func IsAccessRestricted(UserPermissions map[string]bool, basePerm, elevatedPerm string) bool
- func IsFieldJSONTag(dataStruct any, strField string) bool
- func Logging(next http.Handler) http.Handler
- func OrderValidation(orderByStr string, orderDirectionStr string, filters []Filters) string
- func ValidateParams(filters []Filters, params map[string][]string) map[Filters][]string
- func ValidateValus(filterValues map[Filters][]string) map[Filters][]string
- type Conditional
- type CreatedAssetResponse
- type DeletedCacheResponse
- type DistinctFieldNames
- type DistinctFieldNamesResponse
- type ErrorResponse
- type Filters
- type IDs
- type Middleware
- type Pagination
Constants ¶
const ( TokenLimit = "limit" TokenOffset = "offset" TokenWhere = "where" TokenHaving = "having" )
Variables ¶
This section is empty.
Functions ¶
func AcceptedEncoding ¶
AcceptedEncoding returns a default encoding if the one specified does not match the specification
func AreFiltersAggregate ¶
AreFiltersAggregate returns true if any of the provided filters use an allowedAggregateFunctions
func BuildPermissionSet ¶
BuildPermissionSet constructs a map-based set from a slice of permission strings, allowing for efficient O(1) permission lookups.
Each string in the input slice is used as a map key with a value of true. This is commonly used to preprocess user permission lists into a format suitable for fast lookup when checking access.
Example:
BuildPermissionSet([]string{"view:users", "edit:users"})
// → map[string]bool{"view:users": true, "edit:users": true}
func DataEncode ¶
DataEncode encodes structs to json and xml provides an easily scalable way to support extra encodings accross the api Returns the Data encoded, returns the Content-Type of the encoded data and error
func DatabaseValidation ¶
DatabaseValidation checks a database error and returns an appropriate error message and status code that can be directly used in the response. The underline error messaages is always logged.
func DecodeBody ¶
DecodeBody gets the body of a request and parses it
func DynamicFilters ¶
func DynamicFilters(f []Filters, q sq.SelectBuilder, queryParams map[string][]string) sq.SelectBuilder
DynamicFilters it applies dynamic filters based on the allowed filters. These are added to the specified query it can get the query params as is from the r.URL.query() method. it does not stop the user from passing multiple = params all conditions are passed as AND parameters. This is true for both having & where conditions
func GetPaginationQuery ¶
func GetPaginationQuery(q sq.SelectBuilder) sq.SelectBuilder
GetPaginationQuery provides a query that can be used for pagination. it wraps the provided query as a subquery.
func GetParamsApplied ¶ added in v0.0.13
func GetRouteKey ¶
GetRouteKey returns the Route key which is used in the GetCacheKey method and the correct Index Key which is going to be used in the SetKeyIndex Method. The Index Key will store in a json list the Route Key ie. -> properties,1 will return properties:1 for both Route Key & Index Key ie. -> properties,1,google_searches will return properties:1:google_searches for Route Key and properties:1 Index Key Provides an easier way to handle cache keys for subresources of dynamic routes ie. properties/{some_id}/google_searches When accessing stuff in deeper levels than that it is recommended to reuse the same method ,keeping the Route Key and using the first level Index Key ie. -> properties/{some_id}/google_searches/{some_other_id} should keep the index key of properties:some_id but routeKey would be properties:some_id:google_searches:some_other_id returned by GetRouteKey
func GetTime ¶
GetTime Gets env variable of a key, makes it into time.duration else returns the fallback
func HasPermission ¶
HasPermission checks whether the user has a specific permission. It takes a permission key (e.g., "view:users") and a map of the user's permissions, where each key represents a granted permission and the value is typically true.
Returns true if the permission key exists in the map and is set to true. Returns false if the permission is missing or explicitly set to false.
Example:
perms := BuildPermissionSet([]string{"view:users", "edit:posts"})
HasPermission("view:users", perms) // true
HasPermission("delete:users", perms) // false
func IsAccessRestricted ¶
IsAccessRestricted determines whether the user lacks both the base and elevated permissions required to access a resource.
This is useful in scenarios where you want to allow users with either scoped ("view") or broad ("viewAll") permissions to access a resource.
Returns true if the user has neither permission. Returns false if the user has at least one of them.
Example:
IsAccessRestricted("view:article", "viewAll:articles", userPerms)
// true → access denied
// false → access granted
func IsFieldJSONTag ¶
IsFieldJSONTag checks if the string provided matches any json tag of a struct it requires the struct provided has json tags specified
func OrderValidation ¶
OrderValidation provide a struct, the order by string and d.irection and default order by string
func ValidateParams ¶ added in v0.0.13
Types ¶
type Conditional ¶ added in v0.0.14
func BuildFilterConditions ¶
func BuildFilterConditions(filters []Filters, params map[string][]string) []Conditional
BuildFilterConditions takes in allowed filters and values to be filtered. The key of the values map must match the Filter.Name field. It returns first all where conditions (conditions that should be added in a where claus) and having conditions (all conditions that should be added in a having claus) Both can be consolidated using either sq.And() or sq.Or() or a custom method in order to be applied to a filter
func NewConditional ¶ added in v0.0.14
func NewConditional(exrp sq.Sqlizer, ConType string, values []string) Conditional
func (*Conditional) Apply ¶ added in v0.0.14
func (c *Conditional) Apply(q sq.SelectBuilder) sq.SelectBuilder
type CreatedAssetResponse ¶
type CreatedAssetResponse struct {
Status int `json:"status" xml:"status" yaml:"status" csv:"status"`
AssetID *int64 `json:"asset_id" xml:"asset_id" yaml:"asset_id" csv:"asset_id"`
CreatedAt time.Time `json:"date" xml:"date" yaml:"date" csv:"date"`
}
CreatedAssetResponse generic response when creating a new asset
type DeletedCacheResponse ¶
type DeletedCacheResponse struct {
Status int `json:"status" xml:"status" yaml:"status" csv:"status"`
KeysFlushed int `json:"keys_flushed" xml:"keys_flushed" yaml:"keys_flushed" csv:"keys_flushed"`
}
DeletedCacheResponse standard response for routes that delete cache
type DistinctFieldNames ¶
type DistinctFieldNames struct {
ID string `json:"id" xml:"id" yaml:"id" csv:"id"`
Name *string `json:"name" xml:"name" yaml:"name" csv:"name"`
Count *int `json:"count" xml:"count" yaml:"count" csv:"count"`
}
DistinctFieldNames is the basic component of DistinctFieldNamesResponse
type DistinctFieldNamesResponse ¶
type DistinctFieldNamesResponse struct {
Status int `json:"status" xml:"status" yaml:"status" csv:"status"`
Total int `json:"total" xml:"total" yaml:"total" csv:"total"`
Data []DistinctFieldNames `json:"data" xml:"data" yaml:"data" csv:"data"`
}
DistinctFieldNamesResponse response for when selecting a field from a route
type ErrorResponse ¶
type ErrorResponse struct {
Status int `json:"status" xml:"status" yaml:"status" csv:"status"`
Message string `json:"message" xml:"message" yaml:"message" csv:"message"`
}
ErrorResponse standard for errors
type Filters ¶
type Filters struct {
Name string `json:"name" xml:"name" yaml:"name" csv:"name"`
Operator string `json:"operator" xml:"operator" yaml:"operator" csv:"operator"`
DbField string `json:"db_field" xml:"db_field" yaml:"db_field" csv:"db_field"`
FieldID string `json:"field_id" xml:"field_id" yaml:"field_id" csv:"field_id"`
}
Filters standard for allowed filters
func ExtendFilters ¶
ExtendFilters takes in n filters and returns a complete filter list
func IsFieldFilter ¶
IsFieldFilter returns true if a string field matches the name of any filter in the provided filter slice
func (*Filters) ApplyNullToken ¶ added in v0.0.13
func (*Filters) HasNullOrNotNull ¶ added in v0.0.13
func (*Filters) IsAggregate ¶ added in v0.0.13
type IDs ¶
type IDs struct {
ID int `json:"id" xml:"id" yaml:"id" csv:"id"`
}
IDs generic struct for getting the IDs of assets
type Middleware ¶
Middleware takes in a middleware
func CreateStack ¶
func CreateStack(xs ...Middleware) Middleware
CreateStack provides a simple way to stack middlewares
type Pagination ¶
type Pagination struct {
TotalAssets int `json:"total_assets" xml:"total_assets" yaml:"total_assets" csv:"total_assets"`
CurrentPage int `json:"current_page" xml:"current_page" yaml:"current_page" csv:"current_page"`
TotalPages int `json:"total_pages" xml:"total_pages" yaml:"total_pages" csv:"total_pages"`
NextPage bool `json:"next_page" xml:"next_page" yaml:"next_page" csv:"next_page"`
Limit int `json:"limit" xml:"limit" yaml:"limit" csv:"limit"`
}
Pagination standard for pagination