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
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
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 ¶
func NewInspector ¶ added in v0.8.0
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)
}
func NewInspectors ¶ added in v0.8.0
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)
}
}
type Operator ¶
func NewOperator ¶ added in v0.8.0
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)
}
Click to show internal directories.
Click to hide internal directories.