Documentation ¶
Index ¶
- Constants
- func EmptyEndDate() validation.ErrorDetails
- func EmptyOrderColumnMap() validation.ErrorDetails
- func EmptyStartDate() validation.ErrorDetails
- func InvalidLimit() validation.ErrorDetails
- func InvalidOrdering() validation.ErrorDetails
- func Register(columnToString map[types.Column]string, orderColumns map[string]types.Column)
- type InterfaceOption
- type Option
- type Pagination
- type QueryOption
- type SelectOption
- type ShortDateFormattedType
- type StringOption
- type TimeRange
- type Uint64Option
- type UpdateOption
Constants ¶
const ( // UnableToValidate means that validation cannot be done because // of some reasons like lack of initiative data. UnableToValidate = "Internal error: unable to validate" StatusField = "status" StartDateField = "start_date" EndDateField = "end_date" )
Variables ¶
This section is empty.
Functions ¶
func EmptyEndDate ¶
func EmptyEndDate() validation.ErrorDetails
func EmptyOrderColumnMap ¶
func EmptyOrderColumnMap() validation.ErrorDetails
func EmptyStartDate ¶
func EmptyStartDate() validation.ErrorDetails
func InvalidLimit ¶
func InvalidLimit() validation.ErrorDetails
func InvalidOrdering ¶
func InvalidOrdering() validation.ErrorDetails
func Register ¶
Register accepts order columns map and column to string map. It assigns input arguments to private local variables. This function should be called in the init function to register the columns. Validations like pagination use these maps to validate the user inputs. This is an example how to use it. First create a *.go file in `common' package in your project and define columns like this:
package common import ( "bitbucket.org/ouadreams/common/types" "bitbucket.org/ouadreams/common/optionx" ) const ( _ types.Column = iota createdAtColumn symbolColumn statusColumn ) func init() { columnToStringTest := map[types.Column]string{ createdAtColumn: "created_at", symbolColumn: "symbol", statusColumn: "status", } orderColumnsTest := map[string]types.Column{ "created_at": createdAtColumn, } optionx.Register(columnToStringTest, orderColumnsTest) }
Then you should import this package in you main file to inject to register columns:
package main import _ "bitbucket.org/ouadreams/[your-repo-name]/internal/common"
This is you need to do to register your columns in optionx package.
Types ¶
type InterfaceOption ¶
type InterfaceOption Option
const ( WhereEq InterfaceOption WhereGt WhereGtOrEq WhereLt WhereLtOEq Set )
type Pagination ¶
type Pagination struct { Offset *uint64 `query:"offset"` Limit *uint64 `query:"limit"` Ordering *types.Ordering `query:"ordering"` }
Pagination represents data pagination.
func (Pagination) AttachSelectBuilder ¶
func (p Pagination) AttachSelectBuilder(builder sq.SelectBuilder) sq.SelectBuilder
func (*Pagination) Validate ¶
func (p *Pagination) Validate() *validation.Result
type QueryOption ¶
type QueryOption interface { SelectOption UpdateOption }
QueryOption is a mechanism to add new sql query/keyword to an existing query. It is fully compatible with 'squirrel' query builder.
func NewOrderOption ¶
func NewOrderOption(opt StringOption, o *types.Ordering) QueryOption
NewOrderOption makes new instance of QueryOption that is able to attach 'order by` query to the input 'SelectBuilder'. It accepts Ordering as input param.
func NewSetOption ¶
func NewSetOption(opt InterfaceOption, c types.Column, in interface{}) QueryOption
NewSetOption makes new instance of QueryOption that is able to create `set` queries, and attach them to the input 'UpdateBuilder'. It accept column and the value as inputs.
func NewUint64Option ¶
func NewUint64Option(opt Uint64Option, n *uint64) QueryOption
NewUint64Option makes new instance of QueryOption that is able to create any queries that accepts an uint64 as input i.e., limit, offset, etc. And attach it to the input 'SelectBuilder'.
func NewWhereOption ¶
func NewWhereOption(opt InterfaceOption, c types.Column, in interface{}) QueryOption
NewWhereOption makes new instance of QueryOption that is able to create `where` clause queries, and attach them to the input query builder. It accept column and the value as inputs.
type SelectOption ¶
type SelectOption interface { // AttachSelectBuilder creates a new builder and attaches the query option to it. AttachSelectBuilder(sq.SelectBuilder) sq.SelectBuilder }
SelectOption is an interface that provide a mechanism to create sql query parts and attach them to a squirrel.SelectBuilder object.
type ShortDateFormattedType ¶
func (*ShortDateFormattedType) UnmarshalParam ¶
func (s *ShortDateFormattedType) UnmarshalParam(param string) error
type TimeRange ¶
type TimeRange struct { StartDate *ShortDateFormattedType `json:"start_date" query:"start_date"` EndDate *ShortDateFormattedType `json:"end_date" query:"end_date"` Column types.Column `json:"-"` }
TimeRange represents a time range for a database column column with timestamp type. It uses to bind APIs query params. Also, It implemented the SelectOption interface.
func (TimeRange) AttachSelectBuilder ¶
func (t TimeRange) AttachSelectBuilder(builder sq.SelectBuilder) sq.SelectBuilder
func (*TimeRange) Validate ¶
func (t *TimeRange) Validate() *validation.Result
type UpdateOption ¶
type UpdateOption interface { // AttachUpdateBuilder creates a new builder and attaches the query option to it. AttachUpdateBuilder(builder sq.UpdateBuilder) sq.UpdateBuilder }
UpdateOption is an interface that provide a mechanism to create sql query parts and attach them to a squirrel.UpdateBuilder object.