condition

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: MIT Imports: 13 Imported by: 0

README

condition

Contains interfaces and methods for evaluating data using success or failure criteria. Conditions combine inspectors (e.g. string equals "foo", string matches "^foo") and an operator (e.g., AND, OR) to verify the state of data before applying other functions. Each inspector defines its own data processing patterns, but there are a set of common patterns shared among most inspectors:

  • evaluating unstructured data
  • evaluating JSON objects

The package can be used like this (more examples are also available):

package main

import (
	"fmt"

	"github.com/brexhq/substation/condition"
)

func main() {
	inspector := condition.Strings{
		Key:        "foo",
		Function:   "equals",
		Expression: "bar",
	}

	data := []byte(`{"foo":"bar"}`)
	ok, err := inspector.Inspect(data)
	if err != nil {
		panic(err)
	}

	if ok {
		fmt.Println("data passed inspection")
	} else {
		fmt.Println("data failed inspection")
	}
}

Information for each inspector and operator is available in the GoDoc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InspectByte added in v0.4.0

func InspectByte(ctx context.Context, data []byte, inspect Inspector) (bool, error)

InspectByte is a convenience function for applying an Inspector to bytes.

Types

type AND

type AND struct {
	Inspectors []Inspector
}

AND implements the Operator interface and applies the boolean AND logic to configured inspectors.

func (AND) Operate

func (o AND) Operate(ctx context.Context, capsule config.Capsule) (bool, error)

Operate returns true if all Inspectors return true, otherwise it returns false.

type Config added in v0.4.0

type Config struct {
	Operator   string
	Inspectors []config.Config
}

Config is used with OperatorFactory to produce new Operators from JSON configurations.

type Content added in v0.2.0

type Content struct {
	Type   string `json:"type"`
	Negate bool   `json:"negate"`
}

Content evaluates data by its content (media, MIME) type. When used in Substation pipelines, it is most effective when using processors that change the format of data (e.g., process/gzip). The inspector supports MIME types that follow this specification: https://mimesniff.spec.whatwg.org/.

The inspector has these settings:

Type:
	media type used during inspection
Negate (optional):
	if set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

The inspector supports these patterns:

data:
	[31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 203, 207, 7, 4, 0, 0, 255, 255, 33, 101, 115, 140, 3, 0, 0, 0] == application/x-gzip

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "content",
	"settings": {
		"type": "application/x-gzip"
	}
}

func (Content) Inspect added in v0.2.0

func (c Content) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the Content inspector.

type Default

type Default struct{}

Default implements the Operator interface.

func (Default) Operate

func (o Default) Operate(ctx context.Context, capsule config.Capsule) (bool, error)

Operate always returns true. This is the default operator returned by OperatorFactory.

type ForEach added in v0.6.0

type ForEach struct {
	Options ForEachOptions `json:"options"`
	Type    string         `json:"type"`
	Key     string         `json:"key"`
	Negate  bool           `json:"negate"`
}

ForEach evaluates conditions by iterating and applying a condition to each element in a JSON array.

The inspector has these settings:

Options:
	Condition inspector to be applied to all array elements.
Type:
	Method of combining the results of the conditions evaluated.
	Must be one of:
		none: none of the elements must match the condition
		any: at least one of the elements must match the condition
		all: all of the elements must match the condition
Key:
	JSON key-value to retrieve for inspection
Negate (optional):
	If set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

When loaded with a factory, the inspector uses this JSON configuration:

{
	"options": {
		"type": "strings",
		"settings": {
			"function": "endswith",
			"expression": "@example.com"
		}
	},
	"type": "all",
	"key:": "input",
	"negate": false
}

func (ForEach) Inspect added in v0.6.0

func (c ForEach) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the Content inspector.

type ForEachOptions added in v0.6.0

type ForEachOptions struct {
	Inspector config.Config `json:"inspector"`
}

ForEachOptions contains custom options for the ForEach processor:

Inspector:
	condition applied to the data

type IP

type IP struct {
	Type   string `json:"type"`
	Key    string `json:"key"`
	Negate bool   `json:"negate"`
}

IP evaluates IP addresses by their type and usage. This inspector uses the standard library's net package to identify the type and usage of the address (more information is available here: https://pkg.go.dev/net#IP).

The inspector has these settings:

Type:
	IP address type used during inspection.

	Must be one of:
		valid: valid address of any type
		loopback: valid loopback address
		multicast: valid multicast address
		multicast_link_local: valid link local multicast address
		private: valid private address
		unicast_global: valid global unicast address
		unicast_link_local: valid link local unicast address
		unspecified: valid "unspecified" address (e.g., 0.0.0.0, ::)
Key (optional):
	JSON key-value to retrieve for inspection.
Negate (optional):
	If set to true, then the inspection is negated (i.e., true becomes false, false becomes true).

	Defaults to false.

The inspector supports these patterns:

JSON:
	{"ip_address":"10.0.0.1"} == private

data:
	10.0.0.1 == private

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "ip",
	"settings": {
		"type": "private"
	}
}

func (IP) Inspect

func (c IP) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the IP inspector.

type Inspector

type Inspector interface {
	Inspect(context.Context, config.Capsule) (bool, error)
}

Inspector is the interface shared by all inspector methods.

func InspectorFactory

func InspectorFactory(cfg config.Config) (Inspector, error)

InspectorFactory returns a configured Inspector from a config. This is the recommended method for retrieving ready-to-use Inspectors.

func MakeInspectors

func MakeInspectors(cfg []config.Config) ([]Inspector, error)

MakeInspectors accepts multiple inspector configs and returns populated Inspectors. This is a convenience function for generating many Inspectors.

type JSONSchema

type JSONSchema struct {
	Schema []struct {
		Key  string `json:"key"`
		Type string `json:"type"`
	} `json:"schema"`
	Negate bool `json:"negate"`
}

JSONSchema evaluates JSON objects against a minimal schema parser.

The inspector has these settings:

Schema.Key:
	JSON key-value to retrieve for inspection
Schema.Type:
	value type used during inspection of the Schema.Key
	must be one of:
		string
		number (float, int)
		boolean (true, false)
		json
Negate (optional):
	if set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

The inspector supports these patterns:

JSON:
	{"foo":"bar","baz":123} == string,number

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "json_schema",
	"settings": {
		"schema": [
			{
				"key": "foo",
				"type": "string"
			},
			{
				"key": "bar",
				"type": "number"
			}
		]
	}
}

func (JSONSchema) Inspect

func (c JSONSchema) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the JSONSchema inspector.

type JSONValid

type JSONValid struct {
	Negate bool `json:"negate"`
}

JSONValid evaluates JSON objects for validity.

The inspector has these settings:

Negate (optional):
	if set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

The inspector supports these patterns:

data:
	{"foo":"bar","baz":123} == valid
	foo == invalid

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "json_valid"
}

func (JSONValid) Inspect

func (c JSONValid) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the JSONValid inspector.

type Length added in v0.4.0

type Length struct {
	Function string `json:"function"`
	Value    int    `json:"value"`
	Type     string `json:"type"`
	Key      string `json:"key"`
	Negate   bool   `json:"negate"`
}

Length evaluates data using len functions. This inspector supports evaluating byte and rune (character) length of strings. If a JSON array is input, then the length is evaluated against the number of elements in the array.

The inspector has these settings:

Function:
	length evaluation function used during inspection
	must be one of:
		equals
		greaterthan
		lessthan
Value:
	length value used during inspection
Type (optional):
	length type used during inspection
	must be one of:
		byte (number of bytes)
		rune (number of characters)
	defaults to byte
Key (optional):
	JSON key-value to retrieve for inspection
Negate (optional):
	if set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

The inspector supports these patterns:

JSON:
	{"foo":"bar"} == 3
	{"foo":["bar","baz","qux"]} == 3
data:
	bar == 3

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "length",
	"settings": {
		"function": "equals",
		"value": 3
	}
}

func (Length) Inspect added in v0.4.0

func (c Length) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the Length inspector.

type NAND

type NAND struct {
	Inspectors []Inspector
}

NAND implements the Operator interface and applies the boolean NAND logic to configured inspectors.

func (NAND) Operate

func (o NAND) Operate(ctx context.Context, capsule config.Capsule) (bool, error)

Operate returns true if all Inspectors return false, otherwise it returns true.

type NOR

type NOR struct {
	Inspectors []Inspector
}

NOR implements the Operator interface and applies the boolean NOR logic to configured inspectors.

func (NOR) Operate

func (o NOR) Operate(ctx context.Context, capsule config.Capsule) (bool, error)

Operate returns true if any Inspectors return false, otherwise it returns true.

type OR

type OR struct {
	Inspectors []Inspector
}

OR implements the Operator interface and applies the boolean OR logic to configured inspectors.

func (OR) Operate

func (o OR) Operate(ctx context.Context, capsule config.Capsule) (bool, error)

Operate returns true if any Inspectors return true, otherwise it returns false.

type Operator

type Operator interface {
	Operate(context.Context, config.Capsule) (bool, error)
}

Operator is the interface shared by all operator methods. Operators apply a series of Inspectors to and verify the state (aka "condition") of data.

func OperatorFactory

func OperatorFactory(cfg Config) (Operator, error)

OperatorFactory returns a configured Operator from a config. This is the recommended method for retrieving ready-to-use Operators.

type Random added in v0.4.0

type Random struct{}

Random evaluates data based on a random choice. This inspector uses the standard library's rand package. This is best paired with the drop processor and can be used to integration test new configurations and deployments.

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "random"
}

func (Random) Inspect added in v0.4.0

func (c Random) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the Random inspector.

type RegExp

type RegExp struct {
	Expression string `json:"expression"`
	Key        string `json:"key"`
	Negate     bool   `json:"negate"`
}

RegExp evaluates data using a regular expression. This inspector uses a regexp cache provided by internal/regexp.

The inspector has these settings:

Expression:
	regular expression to use during inspection
Key (optional):
	JSON key-value to retrieve for inspection
Negate (optional):
	if set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

The inspector supports these patterns:

JSON:
	{"foo":"bar"} == ^bar
data:
	bar == ^bar

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "regexp",
	"settings": {
		"expression": "^bar"
	},
}

func (RegExp) Inspect

func (c RegExp) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the RegExp inspector.

type Strings

type Strings struct {
	Function   string `json:"function"`
	Expression string `json:"expression"`
	Key        string `json:"key"`
	Negate     bool   `json:"negate"`
}

Strings evaluates data using string functions. This inspector uses the standard library's strings package.

The inspector has these settings:

Function:
	string evaluation function to use during inspection
	must be one of:
		equals
		contains
		endswith
		startswith
Expression:
	substring expression to use during inspection
Key (optional):
	JSON key-value to retrieve for inspection
Negate (optional):
	if set to true, then the inspection is negated (i.e., true becomes false, false becomes true)
	defaults to false

The inspector supports these patterns:

JSON:
	{"foo":"bar"} == bar
data:
	bar == bar

When loaded with a factory, the inspector uses this JSON configuration:

{
	"type": "strings",
	"settings": {
		"function": "endswith",
		"expression": "bar"
	}
}

func (Strings) Inspect

func (c Strings) Inspect(ctx context.Context, capsule config.Capsule) (output bool, err error)

Inspect evaluates encapsulated data with the Strings inspector.

Jump to

Keyboard shortcuts

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