condition

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 16 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

Package condition provides functions for evaluating data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.4.0

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

type Operator

type Operator interface {
	Operate(context.Context, *message.Message) (bool, error)
}
Example
package main

import (
	"context"
	"fmt"

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

func main() {
	ctx := context.TODO()

	// Multiple inspectors can be chained together with an operator.
	// This example uses the "all" operator, which requires all inspectors to
	// return true for the operator to return true.
	cfg := condition.Config{
		Operator: "all",
		Inspectors: []config.Config{
			{
				Type: "number_length_less_than",
				Settings: map[string]interface{}{
					"value": 10,
				},
			},
			{
				Type: "string_contains",
				Settings: map[string]interface{}{
					"value": "f",
				},
			},
		},
	}

	// Operators are retrieved from the factory and
	// applied to a message.
	op, err := condition.New(ctx, cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	msg := message.New().SetData([]byte("fizzy"))
	if err != nil {
		// handle err
		panic(err)
	}

	ok, err := op.Operate(ctx, msg)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(ok)
}
Output:

true

func New added in v1.0.0

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

New returns a configured Operator from an Operator configuration.

Jump to

Keyboard shortcuts

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