Documentation ¶
Index ¶
Constants ¶
const ( EQ = Op("eq") // = NEQ = Op("neq") // <> LT = Op("lt") // < GT = Op("gt") // > LTE = Op("lte") // <= GTE = Op("gte") // >= LIKE = Op("like") // LIKE "PATTERN" ILIKE = Op("ilike") // ILIKE "PATTERN" OR = Op("or") // disjunction AND = Op("and") // conjunction NOT = Op("not") // NOT IN = Op("in") // IN () NIN = Op("nin") // NOT IN () )
Operators that support by rql.
const ( DefaultTagName = "rql" DefaultOpPrefix = "$" DefaultFieldSep = "_" DefaultLimit = 25 DefaultMaxLimit = 100 Offset = "offset" Limit = "limit" )
Default values for configuration.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // TagName is an optional tag name for configuration. t defaults to "rql". TagName string // Model is the resource definition. The parser is configured based on the its definition. // For example, given the following struct definition: // // type User struct { // Age int `rql:"filter,sort"` // Name string `rql:"filter"` // } // // In order to create a parser for the given resource, you will do it like so: // // var QueryParser = rql.MustNewParser( // Model: User{}, // }) // Model interface{} // OpPrefix is the prefix for operators. it defaults to "$". for example, in order // to use the "gt" (greater-than) operator, you need to prefix it with "$". // It similar to the MongoDB query language. OpPrefix string // FieldSep is the separator for nested fields in a struct. For example, given the following struct: // // type User struct { // Name string `rql:"filter"` // Address struct { // City string `rql:"filter"“ // } // } // // We assume the schema for this struct contains a column named "address_city". Therefore, the default // separator is underscore ("_"). But, you can change it to "." for convenience or readability reasons. // Then you will be able to query your resource like this: // // { // "filter": { // "address.city": "DC" // } // } // // The parser will automatically convert it to underscore ("_"). If you want to control the name of // the column, use the "column" option in the struct definition. For example: // // type User struct { // Name string `rql:"filter,column=full_name"` // } // FieldSep string // ColumnFn is the function that translate the struct field string into a table column. // For example, given the following fields and their column names: // // FullName => "full_name" // HTTPPort => "http_port" // // It is preferred that you will follow the same convention that your ORM or other DB helper use. // For example, If you are using `gorm` you want to se this option like this: // // var QueryParser = rql.MustNewParser( // ColumnFn: gorm.ToDBName, // }) // ColumnFn func(string) string // ValueFn is the function that translate the struct field value into table value. // for example, given the following fields and their column values: // status => 0 // // which is a protobuf value, you want to convert it to how the data is stored in the DB, that is: // status => "pending" // ValueFn func(string) func(interface{}) interface{} // Log the the logging function used to log debug information in the initialization of the parser. // It defaults `to log.Printf`. Log func(string, ...interface{}) // DefaultLimit is the default value for the `Limit` field that returns when no limit supplied by the caller. // It defaults to 25. DefaultLimit int // LimitMaxValue is the upper boundary for the limit field. User will get an error if the given value is greater // than this value. It defaults to 100. LimitMaxValue int // DefaultSort is the default value for the 'Sort' field that returns when no sort expression is supplied by the caller. // It defaults to an empty string slice. DefaultSort []string }
Config is the configuration for the parser.
type Params ¶
type Params struct { // Limit represents the number of rows returned by the SELECT statement. Limit int // Offset specifies the offset of the first row to return. Useful for pagination. Offset int // Select contains the expression for the `SELECT` clause defined in the Query. Select string // Sort used as a parameter for the `ORDER BY` clause. For example, "age desc, name". Sort string // Search is used as a parameter for doing multi-column, case-insensitive searches. Search string // FilterExp and FilterArgs come together and used as a parameters for the `WHERE` clause. // // examples: // 1. Exp: "name = ?" // Args: "a8m" // // 2. Exp: "name = ? AND age >= ?" // Args: "a8m", 22 FilterExp string FilterArgs []interface{} }
Params is the parser output after calling to `Parse`. You should pass its field values to your query tool. For example, Suppose you use `gorm`:
params, err := p.Parse(b) if err != nil { return nil, err } var users []User err := db.Where(params.FilterExp, params.FilterArgs...). Order(params.Sort). Find(&users). Error if err != nil { return nil, err } return users, nil
type ParseError ¶
type ParseError struct {
// contains filtered or unexported fields
}
ParseError is type of error returned when there is a parsing problem.
func (ParseError) Error ¶
func (p ParseError) Error() string
type Parser ¶
type Parser struct { Config // contains filtered or unexported fields }
A Parser parses various types. The result from the Parse method is a Param object. It is safe for concurrent use by multiple goroutines except for configuration changes.
func MustNewParser ¶
MustNewParser is like NewParser but panics if the configuration is invalid. It simplifies safe initialization of global variables holding a resource parser.
type Query ¶
type Query struct { // Limit must be > 0 and <= to `LimitMaxValue`. Limit int `json:"limit,omitempty"` // Offset must be >= 0. Offset int `json:"offset,omitempty"` // Select contains the list of expressions define the value for the `SELECT` clause. // For example: // // params, err := p.Parse([]byte(`{ // "select": ["name", "age"] // }`)) // Select []string `json:"select,omitempty"` // Sort contains list of expressions define the value for the `ORDER BY` clause. // In order to return the rows in descending order you can prefix your field with `-`. // For example: // // params, err := p.Parse([]byte(`{ // "sort": ["name", "-age", "+redundant"] // }`)) // Sort []string `json:"sort,omitempty"` // Filter is the query object for building the value for the `WHERE` clause. // The full documentation of the supported operators is writtern in the README. // An example for filter object: // // params, err := p.Parse([]byte(`{ // "filter": { // "account": { "$like": "%github%" }, // "$or": [ // { "city": "TLV" }, // { "city": "NYC" } // ] // } // }`)) // Filter map[string]interface{} `json:"filter,omitempty"` // Search is an object that allows to do cross-column searches on a specific model. // The same field can be used to run search queries against search engines, such as ElasticSearch without specifying a concrete column. // An example of a search object // params, err := p.Parse([]byte(`{ // "search": { // "query": "golang" // } // }`)) Search Search `json:"search,omitempty"` }
Query is the decoded result of the user input.
func (Query) MarshalEasyJSON ¶
MarshalEasyJSON supports easyjson.Marshaler interface
func (Query) MarshalJSON ¶
MarshalJSON supports json.Marshaler interface
func (*Query) UnmarshalEasyJSON ¶
UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (*Query) UnmarshalJSON ¶
UnmarshalJSON supports json.Unmarshaler interface
type Search ¶
type Search struct {
Query string `json:"query,omitempty"`
}
Search holds all search-related fields, namely the string the user queried for, but potentially other configuration variables as well.
type ValidateFn ¶
type ValidateFn func(v interface{}) error