builder

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: Apache-2.0 Imports: 6 Imported by: 2

README

Expression Builder 🔨

GoDoc Version Build Status Go Report Card Codecov

Safe queries from URL query strings

Expression Builder works with the exp expression library to define templates that are safely populated with data from a URL query string. More documentation to come.


func Handler(r *http.Request, w http.ResponseWriter) {

	// Define the URL arguments you want to allow and their types
	b := NewBuilder().
		String("firstName").
		String("lastName").
		Int("publishDate").
		Bool("isPublished")

	// Create a usable expression from the URL query string
	expression := b.Evaluate(r.URL.Query())

	// Next, safely pass it into the database, or something...
}

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 🔨

Documentation

Overview

Package builder generates dynamic expressions based on a set of url.Values and a map of allowed fields. It supports several different data types, along with a simple list of operators that can be applied to each predicate.

Index

Constants

View Source
const DataTypeBool = "BOOL"

DataTypeFloat is a constant that represents a boolean parameter

View Source
const DataTypeInt = "INT"

DataTypeInt is a constant that represents an integer parameter

View Source
const DataTypeInt64 = "INT64"

DataTypeFloat is a constant that represents a 64-bit integer parameter

View Source
const DataTypeObjectID = "OBJECTID"

DataTypeFloat is a constant that represents a mongodb "ObjectId" parameter (24-byte hex string)

View Source
const DataTypeString = "STRING"

DataTypeString is a constant that represents a string parameter

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder map[string]Field

func NewBuilder

func NewBuilder() Builder

func (Builder) Bool

func (b Builder) Bool(name string, options ...FieldOption) Builder

Bool adds a boolean-based parameter to the expression Builder

func (Builder) Evaluate

func (b Builder) Evaluate(values url.Values) exp.Expression

Evaluate returns an Expression based on the specific url.Values provided

func (Builder) EvaluateAll added in v0.5.0

func (b Builder) EvaluateAll(values url.Values) (exp.Expression, error)

EvaluateAll returns an Expression that requires ALL of the values in the builder are present.

func (Builder) EvaluateField added in v0.7.0

func (b Builder) EvaluateField(field Field, values []string) exp.Expression

func (Builder) HasURLParams added in v0.5.0

func (b Builder) HasURLParams(values url.Values) bool

HasURLParams returns TRUE if the URL contains any parameters that match the Builder. It does not test the validity of those values.

func (Builder) Int

func (b Builder) Int(name string, options ...FieldOption) Builder

Int adds an integer-based parameter to the expression Builder

func (Builder) Int64 added in v0.7.0

func (b Builder) Int64(name string, options ...FieldOption) Builder

Int64 adds an 64-bit integer-based parameter to the expression Builder

func (Builder) ObjectID

func (b Builder) ObjectID(name string, options ...FieldOption) Builder

ObjectID adds a mongodb ObjectID-based parameter to the expression Builder

func (Builder) String

func (b Builder) String(name string, options ...FieldOption) Builder

String adds a string-based parameter to the expression Builder

type Field added in v0.7.7

type Field struct {
	Name     string
	DataType string
	Operator string
}

func NewField added in v0.7.7

func NewField(name string, dataType string, options ...FieldOption) Field

type FieldOption added in v0.7.7

type FieldOption func(*Field)

func WithAlias added in v0.7.7

func WithAlias(name string) FieldOption

WithAlias defines the field name to use when creating an expression, which may be different from the field name to read from the URL.

func WithDefaultOpContains added in v0.7.7

func WithDefaultOpContains() FieldOption

WithDefaultOpEqual defines "CONTAINS" as the default operator to use when creating an expression.

func WithDefaultOpEqual added in v0.7.7

func WithDefaultOpEqual() FieldOption

WithDefaultOpEqual defines "EQUAL" as the default operator to use when creating an expression.

func WithDefaultOperator added in v0.7.7

func WithDefaultOperator(operator string) FieldOption

WithDefaultOperator defines the default operator to use when creating an expression

Jump to

Keyboard shortcuts

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