condition

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 14 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/brexhq/substation/condition"
	"github.com/brexhq/substation/config"
)

func main() {
	ctx := context.TODO()
	// 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(ctx, 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(ctx, capsule)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true
Example (Operate)
package main

import (
	"context"
	"fmt"

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

func main() {
	ctx := context.TODO()
	// 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(ctx, opCfg)
	if err != nil {
		// handle err
		panic(err)
	}

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

	ok, err := operator.Operate(ctx, 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 added in v0.8.0

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/brexhq/substation/condition"
	"github.com/brexhq/substation/config"
)

func main() {
	ctx := context.TODO()
	// 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(ctx, 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(ctx, b, inspector)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true

func OperateBytes added in v0.8.0

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/brexhq/substation/condition"
	"github.com/brexhq/substation/config"
)

func main() {
	ctx := context.TODO()
	// 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(ctx, opCfg)
	if err != nil {
		// handle err
		panic(err)
	}

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

	fmt.Println(ok)
}
Output:

true

Types

type Config added in v0.4.0

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 added in v0.8.0

func NewInspector(ctx context.Context, cfg config.Config) (Inspector, error)

NewInspector returns a configured Inspector from an Inspector configuration.

Example
package main

import (
	"context"
	"fmt"

	"github.com/brexhq/substation/condition"
	"github.com/brexhq/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(context.TODO(), cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(inspector)
}
Output:

func NewInspectors added in v0.8.0

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

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

Example
package main

import (
	"context"
	"fmt"

	"github.com/brexhq/substation/condition"
	"github.com/brexhq/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(context.TODO(), 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 added in v0.8.0

func NewOperator(ctx context.Context, cfg Config) (Operator, error)

NewOperator returns a configured Operator from an Operator configuration.

Example
package main

import (
	"context"
	"fmt"

	"github.com/brexhq/substation/condition"
	"github.com/brexhq/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(context.TODO(), 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