predicate

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2019 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

Package predicate defines an interface named Predicate that has a function named Accept that returns true or false based on the value passed to it. The package also defines a set of predicates useful for making decisions about http.Request objects. For instance, the QueryParameterEquals(name, value string) will return a Predicate that expects a *http.Request and will return true if the value of query parameter named 'name' equals 'value'.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Predicate

type Predicate interface {
	Accept(interface{}) bool
}

Predicate is a class that can accept or reject a value based on some condition.

func And

func And(predicates ...Predicate) Predicate

And returns a predicate that is true if all of the passed predicate are true for the input. Furthermore, it stops executing predicates after the first false one.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
)

func main() {
	fmt.Printf("%v\n", predicate.And(predicate.True(), predicate.True()).Accept(nil))
	fmt.Printf("%v\n", predicate.And(predicate.False(), predicate.True()).Accept(nil))
}
Output:

true
false

func ExtractedValueAccepted

func ExtractedValueAccepted(extractor Extractor, predicate Predicate) Predicate

ExtractedValueAccepted returns A predicate that extracts a value using the Extractor and passes that value to the provided predicate

func False

func False() Predicate

FalsePredicate is a predicate that returns false for all inputs.

func HeaderContains

func HeaderContains(name string, path string) Predicate

HeaderContains returns a predicate that returns true if the header named 'name' contains 'value'.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	req.Header.Add("FOO", "FOOBAR")

	fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "BAR").Accept(req))
	fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "snafu").Accept(req))
}
Output:

true
false

func HeaderContainsIgnoreCase

func HeaderContainsIgnoreCase(name string, path string) Predicate

HeaderContainsIgnoreCase returns a predicate that returns true if the header named 'name' contains 'value', ignoring case.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	req.Header.Add("FOO", "FOOBAR")

	fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "bar").Accept(req))
	fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "snafu").Accept(req))
}
Output:

true
false

func HeaderEquals

func HeaderEquals(name string, value string) Predicate

HeaderEquals returns a predicate that returns true if the header named 'name' equals 'value'

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	req.Header.Add("FOO", "FOOBAR")

	fmt.Printf("%v\n", predicate.HeaderEquals("FOO", "FOOBAR").Accept(req))
	fmt.Printf("%v\n", predicate.HeaderEquals("FOO", "snafu").Accept(req))
}
Output:

true
false

func HeaderEqualsIgnoreCase

func HeaderEqualsIgnoreCase(name string, path string) Predicate

HeaderEqualsIgnoreCase returns a predicate that returns true if the header named 'name' equals 'value', ignoring case.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	req.Header.Add("FOO", "FOOBAR")

	fmt.Printf("%v\n", predicate.HeaderEqualsIgnoreCase("FOO", "FooBar").Accept(req))
	fmt.Printf("%v\n", predicate.HeaderEqualsIgnoreCase("FOO", "snafu").Accept(req))
}
Output:

true
false

func HeaderMatches

func HeaderMatches(name string, regex *regexp.Regexp) Predicate

HeaderMatches returns a predicate that returns true if the header named 'name' matches 'regex'

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
	"regexp"
)

func main() {
	truePattern := regexp.MustCompile("FO{2}B.*")
	falsePattern := regexp.MustCompile("FO{3}B.*")
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	req.Header.Add("FOO", "FOOBAR")

	fmt.Printf("%v\n", predicate.HeaderMatches("FOO", truePattern).Accept(req))
	fmt.Printf("%v\n", predicate.HeaderMatches("FOO", falsePattern).Accept(req))
}
Output:

true
false

func HeaderStartsWith

func HeaderStartsWith(name string, path string) Predicate

HeaderStartsWith returns a predicate that returns true if the header named 'name' starts with 'value'.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	req.Header.Add("FOO", "FOOBAR")

	fmt.Printf("%v\n", predicate.HeaderStartsWith("FOO", "FOO").Accept(req))
	fmt.Printf("%v\n", predicate.HeaderStartsWith("FOO", "snafu").Accept(req))
}
Output:

true
false

func MethodIs

func MethodIs(method string) Predicate

MethodIs returns a predicate that takes a request, extracts the method, and returns true if it equals the method provided, ignoring case.

func Not

func Not(predicate Predicate) Predicate

Not returns a predicate that negates the condition defined by the passed predicate.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
)

func main() {
	fmt.Printf("%v\n", predicate.Not(predicate.True()).Accept(nil))
	fmt.Printf("%v\n", predicate.Not(predicate.False()).Accept(nil))
}
Output:

false
true

func Or

func Or(predicates ...Predicate) Predicate

Or returns a predicate that is true if any of the passed predicate are true. Furthermore, it stops executing predicates after the first true one.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
)

func main() {
	fmt.Printf("%v\n", predicate.Or(predicate.False(), predicate.False()).Accept(nil))
	fmt.Printf("%v\n", predicate.Or(predicate.False(), predicate.True()).Accept(nil))
}
Output:

false
true

func PathEquals

func PathEquals(path string) Predicate

PathEquals returns a predicate that returns true if the path equals 'path'

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
fmt.Printf("%v\n", PathEquals("/test/foo/bar").Accept(req))
fmt.Printf("%v\n", PathEquals("/test/bar/foo").Accept(req))
Output:

true
false

func PathMatches

func PathMatches(pathRegex *regexp.Regexp) Predicate

PathMatches returns a predicate that returns true if the path matches the pathRegex.

Example
truePattern := regexp.MustCompile("fo{2}/bar")
falsePattern := regexp.MustCompile("bar/fo{2}")
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
fmt.Printf("%v\n", PathMatches(truePattern).Accept(req))
fmt.Printf("%v\n", PathMatches(falsePattern).Accept(req))
Output:

true
false

func PathStartsWith

func PathStartsWith(path string) Predicate

PathStartsWith returns a predicate that returns true if the path starts with 'path'

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
fmt.Printf("%v\n", PathStartsWith("/test/foo/").Accept(req))
fmt.Printf("%v\n", PathStartsWith("/test/bar/").Accept(req))
Output:

true
false

func QueryParamContains

func QueryParamContains(name, value string) Predicate

QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it contains the value provided.

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil)
fmt.Printf("%v\n", QueryParamContains("q", "oob").Accept(req))
fmt.Printf("%v\n", QueryParamContains("q", "snafu").Accept(req))
fmt.Printf("%v\n", QueryParamContains("x", "oob").Accept(req))
Output:

true
false
false

func QueryParamContainsIgnoreCase

func QueryParamContainsIgnoreCase(name, value string) Predicate

QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it contains the value provided, ignoring case.

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil)
fmt.Printf("%v\n", QueryParamContainsIgnoreCase("q", "OoB").Accept(req))
fmt.Printf("%v\n", QueryParamContainsIgnoreCase("q", "snafu").Accept(req))
fmt.Printf("%v\n", QueryParamContainsIgnoreCase("x", "OoB").Accept(req))
Output:

true
false
false

func QueryParamEquals

func QueryParamEquals(name, value string) Predicate

QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it equals the value provided.

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil)
fmt.Printf("%v\n", QueryParamEquals("q", "foobar").Accept(req))
fmt.Printf("%v\n", QueryParamEquals("q", "snafu").Accept(req))
fmt.Printf("%v\n", QueryParamEquals("x", "foobar").Accept(req))
Output:

true
false
false

func QueryParamEqualsIgnoreCase

func QueryParamEqualsIgnoreCase(name, value string) Predicate

QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it equals the value provided, ignoring case.

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil)
fmt.Printf("%v\n", QueryParamEqualsIgnoreCase("q", "FooBar").Accept(req))
fmt.Printf("%v\n", QueryParamEqualsIgnoreCase("q", "Snafu").Accept(req))
fmt.Printf("%v\n", QueryParamEqualsIgnoreCase("x", "FooBar").Accept(req))
Output:

true
false
false

func QueryParamMatches

func QueryParamMatches(name string, pattern *regexp.Regexp) Predicate

QueryParamMatches returns a Predicate that takes a request, extracts the query parameter specified and returns true if the value matches the pattern provided.

Example
truePattern := regexp.MustCompile("fo{2}bar")
falsePattern := regexp.MustCompile("barfo{2}")
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil)
fmt.Printf("%v\n", QueryParamMatches("q", truePattern).Accept(req))
fmt.Printf("%v\n", QueryParamMatches("q", falsePattern).Accept(req))
Output:

true
false

func QueryParamStartsWith

func QueryParamStartsWith(name string, prefix string) Predicate

QueryParamStartsWith returns a Predicate that takes a request, extracts the query parameter specified and returns true if the value starts with the prefix provided.

Example
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil)
fmt.Printf("%v\n", QueryParamStartsWith("q", "foo").Accept(req))
fmt.Printf("%v\n", QueryParamStartsWith("q", "snafu").Accept(req))
fmt.Printf("%v\n", QueryParamStartsWith("x", "foo").Accept(req))
Output:

true
false
false

func RequestURIEquals

func RequestURIEquals(path string) Predicate

RequestURIEquals returns a predicate that returns true if the request URI equals the path.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	fmt.Printf("%v\n", predicate.RequestURIEquals("/test/foo/bar?q=5&l=3").Accept(req))
	fmt.Printf("%v\n", predicate.RequestURIEquals("/test/foo/bar?q=6&l=3").Accept(req))
}
Output:

true
false

func RequestURIMatches

func RequestURIMatches(pathRegex *regexp.Regexp) Predicate

RequestURIMatches returns a predicate that returns true if the request URI matches the pathRegex.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
	"regexp"
)

func main() {
	truePattern := regexp.MustCompile("bar\\?q=\\d*")
	falsePattern := regexp.MustCompile("foo\\?q\\d*")
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	fmt.Printf("%v\n", predicate.RequestURIMatches(truePattern).Accept(req))
	fmt.Printf("%v\n", predicate.RequestURIMatches(falsePattern).Accept(req))
}
Output:

true
false

func RequestURIStartsWith

func RequestURIStartsWith(path string) Predicate

RequestURIStartsWith returns a predicate that returns true if the request URI starts with the path.

Example
package main

import (
	"fmt"
	"github.com/bluesoftdev/go-http-matchers/predicate"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil)
	fmt.Printf("%v\n", predicate.RequestURIStartsWith("/test/foo").Accept(req))
	fmt.Printf("%v\n", predicate.RequestURIStartsWith("/test/bar").Accept(req))
}
Output:

true
false

func StringContains

func StringContains(value string) Predicate

StringContains returns a predicate that returns true if the value passed contains a substring matching 'value'.

Example
fmt.Printf("%v\n", StringContains("oob").Accept("foobar"))
fmt.Printf("%v\n", StringContains("snafu").Accept("foobar"))
Output:

true
false

func StringEndsWith

func StringEndsWith(value string) Predicate

StringEndsWith returns a predicate that returns true if the value passed ends with a substring matching 'value'.

Example
fmt.Printf("%v\n", StringEndsWith("bar").Accept("foobar"))
fmt.Printf("%v\n", StringEndsWith("foo").Accept("foobar"))
Output:

true
false

func StringEquals

func StringEquals(value string) Predicate

StringEquals returns a predicate that returns true if the value passed is a string and is equal to the value of 'value'

Example
fmt.Printf("%v\n", StringEquals("foobar").Accept("foobar"))
fmt.Printf("%v\n", StringEquals("barfoo").Accept("foobar"))
Output:

true
false

func StringMatches

func StringMatches(regex *regexp.Regexp) Predicate

StringMatches returns a predicate that returns true if the regex matches 'value'.

Example
truePattern := regexp.MustCompile("fo{2}bar")
falsePattern := regexp.MustCompile("barfo{2}")
fmt.Printf("%v\n", StringMatches(truePattern).Accept("foobar"))
fmt.Printf("%v\n", StringMatches(falsePattern).Accept("foobar"))
Output:

true
false

func StringStartsWith

func StringStartsWith(value string) Predicate

StringStartsWith returns a predicate that returns true if the value passed starts with a substring matching 'value'.

Example
fmt.Printf("%v\n", StringStartsWith("foo").Accept("foobar"))
fmt.Printf("%v\n", StringStartsWith("bar").Accept("foobar"))
Output:

true
false

func True

func True() Predicate

TruePredicate is a predicate that returns true for all inputs.

type PredicateFunc

type PredicateFunc func(interface{}) bool

PredicateFunc is an implementation of Predicate that is a function and calls itself on a call to Accept

func (PredicateFunc) Accept

func (pf PredicateFunc) Accept(v interface{}) bool

PredicateFunc.Accept calls the predicate func with the passed value.

Jump to

Keyboard shortcuts

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