condition

package
v0.0.0-...-fee140a Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 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., all, any) to check the state of data before applying other functions.

Documentation

Overview

Example (Inspect)
package main

import (
	"context"
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must be gzip
	cfg := config.Config{
		Type: "content",
		Settings: map[string]interface{}{
			"options": map[string]interface{}{
				"type": "application/x-gzip",
			},
		},
	}

	// inspector is retrieved from the factory
	inspector, err := condition.NewInspector(cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	// inspector is applied to capsule
	capsule := config.NewCapsule()
	capsule.SetData([]byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255})

	ok, err := inspector.Inspect(context.TODO(), capsule)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true
Example (Operate)
package main

import (
	"context"
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must have a length greater than zero and contain
	// the substring "iz"
	cfg := []config.Config{
		{
			Type: "length",
			Settings: map[string]interface{}{
				"options": map[string]interface{}{
					"type":  "less_than",
					"value": 6,
				},
			},
		},
		{
			Type: "strings",
			Settings: map[string]interface{}{
				"options": map[string]interface{}{
					"type":       "contains",
					"expression": "iz",
				},
			},
		},
	}

	// multiple inspectors are paired with an operator to
	// test many conditions at once
	opCfg := condition.Config{
		Operator:   "and",
		Inspectors: cfg,
	}

	// operator is retrieved from the factory
	operator, err := condition.NewOperator(opCfg)
	if err != nil {
		// handle err
		panic(err)
	}

	// operator is applied to capsule
	capsule := config.NewCapsule()
	capsule.SetData([]byte("fizzy"))

	ok, err := operator.Operate(context.TODO(), capsule)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func InspectBytes

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

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

Example
package main

import (
	"context"
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must be gzip
	cfg := config.Config{
		Type: "content",
		Settings: map[string]interface{}{
			"options": map[string]interface{}{
				"type": "application/x-gzip",
			},
		},
	}

	// inspector is retrieved from the factory
	inspector, err := condition.NewInspector(cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	// inspector is applied to bytes
	b := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255}
	ok, err := condition.InspectBytes(context.TODO(), b, inspector)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true

func OperateBytes

func OperateBytes(ctx context.Context, data []byte, op Operator) (bool, error)

OperateBytes is a convenience function for applying an Operator to bytes.

Example
package main

import (
	"context"
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must have a length greater than zero and contain
	// the substring "iz"
	cfg := []config.Config{
		{
			Type: "length",
			Settings: map[string]interface{}{
				"options": map[string]interface{}{
					"type":  "less_than",
					"value": 6,
				},
			},
		},
		{
			Type: "strings",
			Settings: map[string]interface{}{
				"options": map[string]interface{}{
					"type":       "contains",
					"expression": "iz",
				},
			},
		},
	}

	// multiple inspectors are paired with an operator to
	// test many conditions at once
	opCfg := condition.Config{
		Operator:   "and",
		Inspectors: cfg,
	}

	// operator is retrieved from the factory
	operator, err := condition.NewOperator(opCfg)
	if err != nil {
		// handle err
		panic(err)
	}

	// operator is applied to bytes
	b := []byte("fizzy")
	ok, err := condition.OperateBytes(context.TODO(), b, operator)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true

Types

type Config

type Config struct {
	Operator   string          `json:"operator"`
	Inspectors []config.Config `json:"inspectors"`
}

Config is used with NewOperator to produce new operators.

type Inspector

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

func NewInspector

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

NewInspector returns a configured Inspector from an Inspector configuration.

Example
package main

import (
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must be gzip
	cfg := config.Config{
		Type: "content",
		Settings: map[string]interface{}{
			"options": map[string]interface{}{
				"type": "application/x-gzip",
			},
		},
	}

	// inspector is retrieved from the factory
	inspector, err := condition.NewInspector(cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(inspector)
}
Output:

func NewInspectors

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

NewInspectors accepts one or more Inspector configurations and returns configured inspectors.

Example
package main

import (
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must be gzip
	cfg := config.Config{
		Type: "content",
		Settings: map[string]interface{}{
			"options": map[string]interface{}{
				"type": "application/x-gzip",
			},
		},
	}

	// one or more inspectors are created
	inspectors, err := condition.NewInspectors(cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	for _, ins := range inspectors {
		fmt.Println(ins)
	}
}
Output:

type Operator

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

func NewOperator

func NewOperator(cfg Config) (Operator, error)

NewOperator returns a configured Operator from an Operator configuration.

Example
package main

import (
	"fmt"

	"github.com/jshlbrd/substation/condition"
	"github.com/jshlbrd/substation/config"
)

func main() {
	// data must have a length greater than zero and contain
	// the substring "iz"
	cfg := []config.Config{
		{
			Type: "length",
			Settings: map[string]interface{}{
				"options": map[string]interface{}{
					"type":  "greater_than",
					"value": 0,
				},
			},
		},
		{
			Type: "strings",
			Settings: map[string]interface{}{
				"options": map[string]interface{}{
					"type":       "contains",
					"expression": "iz",
				},
			},
		},
	}

	// multiple inspectors are paired with an operator to
	// test many conditions at once.
	opCfg := condition.Config{
		Operator:   "and",
		Inspectors: cfg,
	}

	// operators are retrieved from the factory.
	operator, err := condition.NewOperator(opCfg)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(operator)
}
Output:

Jump to

Keyboard shortcuts

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