filter

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: MIT Imports: 8 Imported by: 12

README

Go Report Card GoDoc

Query Filter Parser for SCIM v2.0

RFC7644: Section-3.4.2.2

Implemented Operators

Attribute Operators
  • eq, ne, co, sw, ew, gt, ge, lt, le
  • pr
Logical Operators
  • and, or
  • not
  • precedence
Grouping Operators
  • ( )
  • [ ]

Case Sensitivity

Attribute names and attribute operators used in filters are case insensitive.
For example, the following two expressions will evaluate to the same logical value:

filter=userName Eq "john"
filter=Username eq "john"

Expressions Requirements

Each expression MUST contain an attribute name followed by an attribute operator and optional value.

Multiple expressions MAY be combined using logical operators.

Expressions MAY be grouped together using round brackets "(" and ")".

Filters MUST be evaluated using the following order of operations, in order of precedence:

1.  Grouping operators
2.  Attribute operators
3.  Logical operators - where "not" takes precedence over "and",
    which takes precedence over "or"

Documentation

Overview

Example (Walk)
expression, _ := ParseFilter([]byte("emails[type eq \"work\" and value co \"@example.com\"] or ims[type eq \"xmpp\" and value co \"@foo.com\"]"))
var walk func(e Expression) error
walk = func(e Expression) error {
	switch v := e.(type) {
	case *LogicalExpression:
		_ = walk(v.Left)
		_ = walk(v.Right)
	case *ValuePath:
		_ = walk(v.ValueFilter)
	case *AttributeExpression:
		fmt.Printf("%s %s %q\n", v.AttributePath, v.Operator, v.CompareValue)
	default:
		// etc...
	}
	return nil
}
_ = walk(expression)
Output:

type eq "work"
value co "@example.com"
type eq "xmpp"
value co "@foo.com"

Index

Examples

Constants

View Source
const (
	// PR is an abbreviation for 'present'.
	PR CompareOperator = "pr"
	// EQ is an abbreviation for 'equals'.
	EQ CompareOperator = "eq"
	// NE is an abbreviation for 'not equals'.
	NE CompareOperator = "ne"
	// CO is an abbreviation for 'contains'.
	CO CompareOperator = "co"
	// SW is an abbreviation for 'starts with'.
	SW CompareOperator = "sw"
	// EW an abbreviation for 'ends with'.
	EW CompareOperator = "ew"
	// GT is an abbreviation for 'greater than'.
	GT CompareOperator = "gt"
	// LT is an abbreviation for 'less than'.
	LT CompareOperator = "lt"
	// GE is an abbreviation for 'greater or equal than'.
	GE CompareOperator = "ge"
	// LE is an abbreviation for 'less or equal than'.
	LE CompareOperator = "le"

	// AND is the logical operation and (&&).
	AND LogicalOperator = "and"
	// OR is the logical operation or (||).
	OR LogicalOperator = "or"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AttributeExpression

type AttributeExpression struct {
	AttributePath AttributePath
	Operator      CompareOperator
	CompareValue  interface{}
}

AttributeExpression represents an attribute expression/filter.

func ParseAttrExp

func ParseAttrExp(raw []byte) (AttributeExpression, error)

ParseAttrExp parses the given raw data as an AttributeExpression.

Example (Pr)
fmt.Println(ParseAttrExp([]byte("userName pr")))
Output:

userName pr <nil>
Example (Sw)
fmt.Println(ParseAttrExp([]byte("userName sw \"J\"")))
Output:

userName sw "J" <nil>

func ParseAttrExpNumber added in v2.2.0

func ParseAttrExpNumber(raw []byte) (AttributeExpression, error)

ParseAttrExpNumber parses the given raw data as an AttributeExpression with json.Number.

func (AttributeExpression) String

func (e AttributeExpression) String() string

type AttributePath

type AttributePath struct {
	URIPrefix     *string
	AttributeName string
	SubAttribute  *string
}

AttributePath represents an attribute path. Both URIPrefix and SubAttr are optional values and can be nil. e.g. urn:ietf:params:scim:schemas:core:2.0:User:name.givenName

^                                          ^    ^
URIPrefix                                  |    SubAttribute
                                           AttributeName

func ParseAttrPath

func ParseAttrPath(raw []byte) (AttributePath, error)

ParseAttrPath parses the given raw data as an AttributePath.

Example
fmt.Println(ParseAttrPath([]byte("urn:ietf:params:scim:schemas:core:2.0:User:name.familyName")))
Output:

urn:ietf:params:scim:schemas:core:2.0:User:name.familyName <nil>

func (AttributePath) String

func (p AttributePath) String() string

func (*AttributePath) SubAttributeName

func (p *AttributePath) SubAttributeName() string

SubAttributeName returns the sub attribute name if present. Returns an empty string otherwise.

func (*AttributePath) URI

func (p *AttributePath) URI() string

URI returns the URI if present. Returns an empty string otherwise.

type CompareOperator

type CompareOperator string

CompareOperator represents a compare operation.

type Expression

type Expression interface {
	// contains filtered or unexported methods
}

Expression is a type to assign to implemented expressions. Valid expressions are:

  • ValuePath
  • AttributeExpression
  • LogicalExpression
  • NotExpression

func ParseFilter

func ParseFilter(raw []byte) (Expression, error)

ParseFilter parses the given raw data as an Expression.

Example (And)
fmt.Println(ParseFilter([]byte("title pr and userType eq \"Employee\"")))
Output:

title pr and userType eq "Employee" <nil>
Example (AttrExp)
fmt.Println(ParseFilter([]byte("schemas eq \"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\"")))
Output:

schemas eq "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" <nil>
Example (CaseInsensitivity)
fmt.Println(ParseFilter([]byte("NAME PR AND NOT (FIRST EQ \"test\") AND ANOTHER NE \"test\"")))
Output:

NAME pr and not(FIRST eq "test") and ANOTHER ne "test" <nil>
Example (Not)
fmt.Println(ParseFilter([]byte("not (emails co \"example.com\" or emails.value co \"example.org\")")))
Output:

not(emails co "example.com" or emails.value co "example.org") <nil>
Example (Or)
fmt.Println(ParseFilter([]byte("title pr or userType eq \"Intern\"")))
Output:

title pr or userType eq "Intern" <nil>
Example (Parentheses)
fmt.Println(ParseFilter([]byte("(emails.type eq \"work\")")))
Output:

emails.type eq "work" <nil>
Example (ValuePath)
fmt.Println(ParseFilter([]byte("emails[type eq \"work\" and value co \"@example.com\"]")))
Output:

emails[type eq "work" and value co "@example.com"] <nil>

func ParseFilterNumber added in v2.2.0

func ParseFilterNumber(raw []byte) (Expression, error)

ParseFilterNumber parses the given raw data as an Expression with json.Number.

type LogicalExpression

type LogicalExpression struct {
	Left, Right Expression
	Operator    LogicalOperator
}

LogicalExpression represents an 'and' / 'or' node.

func (LogicalExpression) String

func (e LogicalExpression) String() string

type LogicalOperator

type LogicalOperator string

LogicalOperator represents a logical operation such as 'and' / 'or'.

type NotExpression

type NotExpression struct {
	Expression Expression
}

NotExpression represents an 'not' node.

func (NotExpression) String

func (e NotExpression) String() string

type Path

type Path struct {
	AttributePath   AttributePath
	ValueExpression Expression
	SubAttribute    *string
}

Path describes the target of a PATCH operation. Path can have an optional ValueExpression and SubAttribute. e.g. members[value eq "2819c223-7f76-453a-919d-413861904646"].displayName

^       ^                                                ^
|       ValueExpression                                  SubAttribute
AttributePath

func ParsePath

func ParsePath(raw []byte) (Path, error)

ParsePath parses the given raw data as an Path.

Example (AttrPath)
fmt.Println(ParsePath([]byte("members")))
fmt.Println(ParsePath([]byte("name.familyName")))
Output:

members <nil>
name.familyName <nil>
Example (ValuePath)
fmt.Println(ParsePath([]byte("members[value eq \"2819c223-7f76-453a-919d-413861904646\"]")))
fmt.Println(ParsePath([]byte("members[value eq \"2819c223-7f76-453a-919d-413861904646\"].displayName")))
Output:

members[value eq "2819c223-7f76-453a-919d-413861904646"] <nil>
members[value eq "2819c223-7f76-453a-919d-413861904646"].displayName <nil>

func ParsePathNumber added in v2.2.0

func ParsePathNumber(raw []byte) (Path, error)

ParsePathNumber parses the given raw data as an Path with json.Number.

func (Path) String

func (p Path) String() string

func (*Path) SubAttributeName

func (p *Path) SubAttributeName() string

SubAttributeName returns the sub attribute name if present. Returns an empty string otherwise.

type ValuePath

type ValuePath struct {
	AttributePath AttributePath
	ValueFilter   Expression
}

ValuePath represents a filter on a attribute path.

func ParseValuePath

func ParseValuePath(raw []byte) (ValuePath, error)

ParseValuePath parses the given raw data as an ValuePath.

Example
fmt.Println(ParseValuePath([]byte("emails[type eq \"work\"]")))
fmt.Println(ParseValuePath([]byte("emails[not (type eq \"work\")]")))
fmt.Println(ParseValuePath([]byte("emails[type eq \"work\" and value co \"@example.com\"]")))
Output:

emails[type eq "work"] <nil>
emails[not(type eq "work")] <nil>
emails[type eq "work" and value co "@example.com"] <nil>

func ParseValuePathNumber added in v2.2.0

func ParseValuePathNumber(raw []byte) (ValuePath, error)

ParseValuePathNumber parses the given raw data as an ValuePath with json.Number.

func (ValuePath) String

func (e ValuePath) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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