operator

package
v0.0.0-...-c537d22 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidDelegate = errors.New("invalid delegate")
)
View Source
var (
	ErrParserInvalidMatch = errors.New("could not parser regexp matches")
)

Functions

func GetOperatorValues

func GetOperatorValues(ops []Operator) []string

Types

type Modifier

type Modifier rune
var (
	ModifierOr  Modifier = '|'
	ModifierNot Modifier = '!'
	ModifierAnd Modifier = '&'

	Modifiers = []Modifier{
		ModifierOr, ModifierNot, ModifierAnd,
	}
)

Default Modifiers

func (Modifier) Matches

func (m Modifier) Matches(r byte) bool

type Operator

type Operator struct {
	// Values representative of this operator.
	Values []string

	// Modifiers associated with the operator.
	Modifiers []Modifier
}

Operator defines a set of values and modifiers that go along with those values. Most operators contain only a single value, but prepare to handle multiple values.

2022-09-28 - Note that this is a breaking change from previous versions.

func (*Operator) Equals

func (o *Operator) Equals(b Operator) bool

Equals returns true of the provided operator is the same as the current one.

func (*Operator) Has

func (o *Operator) Has(m Modifier) bool

type Operators

type Operators struct {
	// Map of key to operator
	Values map[string][]Operator

	// Remainders
	Remainders []Operator

	// Any Errors related to parsing of the operator.
	Errors []error
}

func (*Operators) AddOperator

func (o *Operators) AddOperator(key string, op Operator)

func (*Operators) AddRemainder

func (o *Operators) AddRemainder(str ...string)

func (*Operators) Equals

func (o *Operators) Equals(b *Operators) bool

Equals returns true of the provided operators is the same as the current one.

func (*Operators) Get

func (o *Operators) Get(keys ...string) []Operator

func (*Operators) GetOperator

func (o *Operators) GetOperator(key string) []Operator

func (*Operators) GetOperatorValues

func (o *Operators) GetOperatorValues(key string) []string

type ParseMapKeyError

type ParseMapKeyError struct {
	// The error in question
	Base error

	// InputKey which caused the error.
	InputKey string
}

ParseMapKeyError is an error related to parsing of a map key. It wraps the error returned from ParserRegexpDelegate.ParseMapKey

func (*ParseMapKeyError) Error

func (e *ParseMapKeyError) Error() string

Error string

type ParseMatchError

type ParseMatchError struct {
	// Matches that were returned, if any.
	Matches []string

	// The error in question
	Base error
}

ParseMatchError is an error related to the parsing of a match. It wraps the error returned from ParserRegexpDelegate.ParseMatch

func (*ParseMatchError) Error

func (e *ParseMatchError) Error() string

Error string

type ParseRegexpError

type ParseRegexpError struct {
	// The error in question
	Base error
}

ParseRegexpError occurs when regexp could not be parsed properly for Parser.Parse.

func (*ParseRegexpError) Error

func (e *ParseRegexpError) Error() string

Error string

type Parser

type Parser struct {
	// Delegate modifies the operation of the parser.
	//
	// TODO: Currently, only a Regexp parser is supported, but in the future, this should be extracted to extended to
	//   allow for other types of parsers.
	Delegate ParserRegexpDelegate
}

Parser parses maps/strings into operators using the provided delegate to modify its behaviour. Currently, the only delegate possible is the Regexp delegate. This delegate will ParseMatch based on a Regexp returned by the delegate and a corresponding ParseMatch function.

func NewParser

func NewParser(delegate interface{}) (*Parser, error)

NewParser generates a new parser based on the provided delegate. If defined, it will also set default modifiers.

Unfortunately, the delegate does not work because we used to take in ParserConfig (note: missing the pointer). However, it currently wants &ParserConfig. Thus, we use interface{}.

func (*Parser) Parse

func (p *Parser) Parse(str string) Operators

Parse parses a string into operators based on the regexp generated from the parser config.

func (*Parser) ParseMap

func (p *Parser) ParseMap(m map[string][]string) Operators

ParseMap parses a map into operators. This can be used, for example, by passing the query string url.Values as operators. In this implementation, any query string parameters that do not have a key are ignored. Thus, no "remainders" will be returned.

type ParserConfig

type ParserConfig struct {
	// String start and end characters.
	StringStart string
	StringEnd   string

	// Key delimiting character
	KeyDelimiter string

	// List of modifier runes.
	Modifiers []Modifier
	// contains filtered or unexported fields
}

ParserConfig implements ParserRegexpDelegate. It looks for operators of the form

[modifier-character][key][key-delimiter][str-start][stuff][str-end]

For example, if StringStart and StringEnd were " and the keyDelimiter was =, an example operator would be

key="value"
!key="value"

The key and the string delimiters are optional.

Naming of ParserConfig is to ensure backwards compatibility for users that still use this. In the future, it will be changed to DefaultParserDelegate.

@DEPRECATED

func (*ParserConfig) ParseMapKey

func (c *ParserConfig) ParseMapKey(inputKey string) (mods []Modifier, key string, err error)

ParseMapKey decodes the input key by splitting out its modifiers.This is used for the Parser.ParseMap functionality.

func (*ParserConfig) ParseMatch

func (c *ParserConfig) ParseMatch(match []string) (mods []Modifier, key string, value []string, err error)

ParseMatch parses the provided match to return: - modifiers - key - value

Match has the same values as regexp.FindSubmatchString

func (*ParserConfig) Regexp

func (c *ParserConfig) Regexp() (r *regexp.Regexp, err error)

Regexp function should return a single regular expression which parses for 1 operator, keeping in mind remainders and modifiers.

func (*ParserConfig) SetDefaultModifiers

func (c *ParserConfig) SetDefaultModifiers(m ...Modifier)

type ParserDelegateSatDefaultModifiers

type ParserDelegateSatDefaultModifiers interface {
	// SetDefaultModifiers should store the provided modifiers as the modifiers used in the REGEXP and the ParseMapKey
	// function IF another set of modifiers hasn't already been set.
	SetDefaultModifiers(m ...Modifier)
}

ParserDelegateSatDefaultModifiers is an optional interface allowing setting of "default" modifiers (e.g., in the case none are provided). This is for backwards compatibility and will be removed in the future.

@DEPRECATED

type ParserRegexpDelegate

type ParserRegexpDelegate interface {
	// Regexp function should return a single regular expression which parses for 1 operator, keeping in mind
	// remainders and modifiers.
	Regexp() (*regexp.Regexp, error)

	// ParseMatch parses the provided match to return:
	// - modifiers
	// - key
	// - value
	//
	// Depending on the return values, the Parser will either:
	// - exclude it
	// - include it as an operator
	// - include it as a remainder.
	ParseMatch(match []string) (mods []Modifier, key string, value []string, err error)

	// ParseMapKey decodes the input key by splitting out its modifiers.This is used for the Parser.ParseMap
	// functionality.
	ParseMapKey(inputKey string) (mods []Modifier, key string, err error)
}

ParserRegexpDelegate is an interface that the Parser uses to help perform its work in producing operators. The Parser will delegate responsibility to the provided implementation.

Jump to

Keyboard shortcuts

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