filters

package
v0.0.0-...-37076da Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package filters will be used to filter the results of a query.

That package will contains the FilterConditions interface and the filters struct.

The filters struct is private but has the NewFilterConditions factory who returns filters instance.

That package was made for easy use with https://gorm.io

Example
package main

import (
	"fmt"

	"github.com/jeanmolossi/vigilant-waddle/src/pkg/filters"
)

func main() {
	f := filters.NewConditions()
	f.AddFields([]string{"course_published", "course_name"})
	f.WithCondition("course_published", true)
	f.WithCondition("course_name", "Effective Eureka")
	f.WithComplexCondition("course_id", filters.IN, []interface{}{"1", "2"}, filters.OR)

	fields, hasFields := f.WithFields("prefixed")
	statement, values := f.Conditions()
	fmt.Println(fields, hasFields)
	fmt.Println(statement)
	fmt.Println(values)

}
Output:

[prefixed.course_published prefixed.course_name] true
course_published = ? AND course_name = ? OR course_id IN (?)
[true Effective Eureka [1 2]]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assertion

type Assertion string

Assertion is the value assertion to where clause.

Useful to change the assertion condition. Like:

EQ	// =
NEQ	// !=
GT	// >
GTE	// >=
// ...
const (
	EQ      Assertion = "="
	NEQ     Assertion = "!="
	GT      Assertion = ">"
	GTE     Assertion = ">="
	LT      Assertion = "<"
	LTE     Assertion = "<="
	IN      Assertion = "IN"
	NULL    Assertion = "IS NULL"
	NOTNULL Assertion = "IS NOT NULL"
)

type Condition

type Condition interface {
	// Statement build statement string to ConditionMap
	//
	// It receives "isFirst" param to define if it
	// will receive preposition on statement.
	//
	// In cases who isFirst becomes true, the preposition
	// will be ignored in final statment
	Statement(isFirst bool) string
	// Value will handle received value to return
	// a single value if is a single value defined
	// in the Condition configuration
	//
	// To slices or multiple values it will handle
	// to return value as received
	Value() interface{}
}

Condition is the interface to handle a single Condition inside the ConditionMap

func NewCondition

func NewCondition(
	index int,
	f string,
	a Assertion,
	p Preposition,
	values ...interface{},
) Condition

type ConditionMap

type ConditionMap interface {
	// Len returns counting of how many conditions are set
	Len() int
	// Values will result in a slice of received condition values
	Values() []interface{}
	// Statement returns formatted statment string.
	//
	// Like:
	// 	`id = ? AND name = ? OR ip IN (?)`
	Statement() string
	// AppendCondition will append a condition with received config inside
	// ConditionMap
	//
	// Usage:
	// 	AppendCondition("id", filters.IN, filters.AND, []string{"1","2"})
	// Statment will be:
	// 	`id IN (?)`
	// Values will be
	// 	[]interface{"1", "2"}
	AppendCondition(field string, assertion Assertion, preposition Preposition, values ...interface{})
	// DelCondition will remove a field defined condition
	DelCondition(field string)
	// GetCondition will get a field condition configuration
	//
	// If condition does not set will return false on the second returned value
	GetCondition(field string) (interface{}, bool)
}

ConditionMap will handle all conditions and how it will be compiled and formated.

func NewConditionMap

func NewConditionMap() ConditionMap

NewConditionMap will return ConditionMap implementation

type FilterConditions

type FilterConditions interface {
	// WithFields will return the fields to be used in the query
	// and if has fields to be used.
	//
	// If WithFields returns false, it means that there are no fields to be used.
	WithFields(prefix string) ([]string, bool)
	// HasConditions will return true if there are conditions to be used in the query.
	// If HasConditions returns false should NOT use the following methods:
	//  - Conditions
	//  - GetCondition
	HasConditions() bool
	// Conditions will build the string statement and a values slice to be used in the query.
	//
	// On the following example:
	//
	// 		conditions := NewFilterConditions()
	// 		conditions.WithCondition("course_published", true)
	// 		conditions.WithCondition("course_name", "Effective Eureka")
	//
	// Conditions will return the following:
	//
	// 		statement := "course_published = ? AND course_name = ?"
	// 		values := []interface{}{true, "Effective Eureka"}
	Conditions() (string, []interface{})
	// GetCondition will return the value and if exists a condition.
	//
	// On the following example:
	//
	// 		conditions := NewFilterConditions()
	// 		conditions.WithCondition("course_published", true)
	// 		conditions.WithCondition("course_name", "Effective Eureka")
	//      cond := conditions.GetCondition("course_name")
	//
	// GetCondition will return the following to cond:
	//
	// 		"Effective Eureka", true
	GetCondition(key string) (interface{}, bool)
	// WithCondition will add a condition to be used in the query.
	WithCondition(field string, value interface{})
	// WithComplexCondition will add a complex condition to conditionMap
	//
	// It can set the ASSERTION type [EQ, NEQ, GT, GTE, etc] and set PREPOSITION
	// in Where clause [AND, OR]
	//
	// Like:
	// 	WithComplexCondition("id", EQ, "1", AND)
	WithComplexCondition(field string, assertion Assertion, value interface{}, preposition Preposition)
	// RemoveCondition will remove a condition to be used in the query.
	RemoveCondition(field string)
	// AddField will add a field to be used in the query.
	AddField(field string)
	// AddFields will add a slice of fields to be used in the query.
	AddFields(fields []string)
}

FilterConditions will be used to filter the results of a query.

func NewConditions

func NewConditions() FilterConditions

NewConditions will return FilterCondition instance.

type Preposition

type Preposition string

Preposition is the aggregation preposition to Where clouse.

Useful to change assertion conditions between AND or OR

const (
	AND Preposition = "AND"
	OR  Preposition = "OR"
)

Jump to

Keyboard shortcuts

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