condition

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2022 License: MIT Imports: 7 Imported by: 0

README

condition

Contains interfaces and methods for evaluating data for user-defined success or failure criteria. Conditions are a combination of operators (e.g., AND, OR) and inspectors (e.g. string equals "foo", regular expression matches "^foo") that can be used by applications that need to verify data before applying other processing functions.

The package can be used like this:

package main

import (
	"fmt"

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

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

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

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

In Substation applications, conditions adhere to these rules:

  • condition must pass (return true) to apply processors
  • NOT statements are achieved by using negation operators (NAND, NOR) or negation settings in inspectors

operators

Conditions rely on boolean operators to evaluate data. When using the OperatorFactory, the default behavior is to always return true no matter what data is provided as input.

Operator Description
AND All inspectors must return true to pass
OR Any inspector must return true to pass
NAND All inspectors must return false to pass
NOR Any inspector must return false to pass

inspectors

Conditions use inspectors, which are atomic data inspection methods, to evaluate data. Inspectors can be independently used in other non-Substation applications to evaluate data. By default, all inspectors return false if the evaluation does not succeed, but this can be flipped by setting negate to true.

Inspector Description
IP Evaluates an IP address by type and usage
JSONSchema Evaluates JSON key values by type
JSONValid Evaluates whether data is valid JSON
RegExp Evaluates data with a regular expression
Strings Evaluates data with string functions
ip

Inspects IP addresses and evaluates 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). The inspector supports these evaluations:

  • 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, ::)

The inspector uses this Jsonnet configuration:

// returns true if the IP address value stored in JSON key "foo" is not a private address
{
  type: 'ip',
  settings: {
    key: 'foo',
    function: 'private',
    negate: true,
  },
}
json_schema

Inspects JSON data and compares the key-value pairs against a user-defined schema. The inspector supports these schema types:

  • string
  • number (float, integer)
  • boolean (true, false)
  • json

The inspector uses this Jsonnet configuration:

// returns true if the value stored in JSON key "foo" is a string and the value stored in JSON key "bar" is an integer or float
{
  type: 'json_schema',
  settings: {
    negate: false,
    schema: [
      {
        key: "foo",
        type: "string",
      },
      {
        key: "bar",
        type: "number",
      }
    ],
  },
}
json_valid

Inspects JSON data for validity. This inspector is a useful evaluation to use before attempting to sink data to systems that only support JSON.

The inspector uses this Jsonnet configuration:

// returns true if the data is valid JSON
{
  type: 'json_valid',
  settings: {
    negate: false,
  },
}
regexp

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

The inspector uses this Jsonnet configuration:

// returns true if the value stored in JSON key "foo" matches the regular expression
{
  type: 'regexp',
  settings: {
    key: 'foo',
    expression: '^bar',
    negate: false,
  },
}
strings

Inspects data and evaluates it using string functions. This inspector uses the standard library's strings package. The inspector supports these string functions:

  • equals: data equals the string expression
  • contains: data contains the string expression
  • endswith: data ends with the string expression
  • startswith: data starts with the string expression

The inspector uses this Jsonnet configuration:

// returns true if the value stored in JSON key "foo" ends with the substring "bar"
{
  type: 'strings',
  settings: {
    key: 'foo',
    expression: 'bar',
    function: 'endswith',
    negate: false,
  },
}

Documentation

Index

Constants

View Source
const InspectorInvalidFactoryConfig = errors.Error("InspectorInvalidFactoryConfig")

InspectorInvalidFactoryConfig is used when an unsupported Inspector is referenced in InspectorFactory

View Source
const OperatorInvalidFactoryConfig = errors.Error("OperatorInvalidFactoryConfig")

OperatorInvalidFactoryConfig is used when an unsupported Operator is referenced in OperatorFactory

View Source
const OperatorMissingInspectors = errors.Error("OperatorMissingInspectors")

OperatorMissingInspectors is used when an Operator that requres Inspectors is created with no inspectors

Variables

This section is empty.

Functions

This section is empty.

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(data []byte) (bool, error)

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

type Default

type Default struct{}

Default implements the Operator interface.

func (Default) Operate

func (o Default) Operate(data []byte) (bool, error)

Operate always returns true. This operator cannot be called directly and is chosen from the OperatorFactory if no valid operator is provided.

type IP

type IP struct {
	Key      string `mapstructure:"key"`
	Function string `mapstructure:"function"`
	Negate   bool   `mapstructure:"negate"`
}

IP implements the Inspector interface for evaluating IP address data. More information is available in the README.

func (IP) Inspect

func (c IP) Inspect(data []byte) (output bool, err error)

Inspect evaluates the type and usage of an IP address.

type Inspector

type Inspector interface {
	Inspect([]byte) (bool, error)
}

Inspector is the interface shared by all inspector methods.

func InspectorFactory

func InspectorFactory(cfg InspectorConfig) (Inspector, error)

InspectorFactory loads Inspectors from an InspectorConfig. This is the recommended function for retrieving ready-to-use Inspectors.

func MakeInspectors

func MakeInspectors(cfg []InspectorConfig) ([]Inspector, error)

MakeInspectors is a convenience function for making several Inspectors.

type InspectorConfig

type InspectorConfig struct {
	Type     string
	Settings map[string]interface{}
}

InspectorConfig contains arbitrary JSON settings for Inspectors loaded via mapstructure.

type JSONSchema

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

JSONSchema implements the Inspector interface for evaluating JSON schemas. More information is available in the README.

func (JSONSchema) Inspect

func (c JSONSchema) Inspect(data []byte) (output bool, err error)

Inspect evaluates the JSON object against a provided schema.

type JSONValid

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

JSONValid implements the Inspector interface for evaluating the validity of JSON data. More information is available in the README.

func (JSONValid) Inspect

func (c JSONValid) Inspect(data []byte) (output bool, err error)

Inspect evaluates data as a valid JSON object.

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(data []byte) (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(data []byte) (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(data []byte) (bool, error)

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

type Operator

type Operator interface {
	Operate([]byte) (bool, error)
}

Operator is the interface shared by all operator methods. Most operators contain a list of Inspectors that the operand applies to.

func OperatorFactory

func OperatorFactory(cfg OperatorConfig) (Operator, error)

OperatorFactory loads Operators from an OperatorConfig. This is the recommended function for retrieving ready-to-use Operators.

type OperatorConfig

type OperatorConfig struct {
	Operator   string
	Inspectors []InspectorConfig
}

OperatorConfig contains an array of InspectorConfig that are used to evaluate data.

type RegExp

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

RegExp implements the Inspector interface for evaluating data with a regular expression. More information is available in the README.

func (RegExp) Inspect

func (c RegExp) Inspect(data []byte) (output bool, err error)

Inspect evaluates the data with a user-defined regular expression.

type Strings

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

Strings implements the Inspector interface for evaluating data using string functions. More information is available in the README.

func (Strings) Inspect

func (c Strings) Inspect(data []byte) (output bool, err error)

Inspect evaluates the data using string functions.

Jump to

Keyboard shortcuts

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