Documentation
¶
Overview ¶
Example (SubstationCustomConfig) ¶
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/brexhq/substation/v2"
)
// Custom applications should embed the Substation configuration and
// add additional configuration options.
type customConfig struct {
substation.Config
Auth struct {
Username string `json:"username"`
Password string `json:"password"`
} `json:"auth"`
}
// String returns an example string representation of the custom configuration.
func (c customConfig) String() string {
return fmt.Sprintf("%s:%s", c.Auth.Username, c.Auth.Password)
}
func main() {
// Substation applications rely on a context for cancellation and timeouts.
ctx := context.Background()
// Define and load the custom configuration. This config includes a username
// and password for authentication.
conf := []byte(`
{
"transforms":[
{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},
{"type":"send_stdout"}
],
"auth":{
"username":"foo",
"password":"bar"
}
}
`)
cfg := customConfig{}
if err := json.Unmarshal(conf, &cfg); err != nil {
// Handle error.
panic(err)
}
// Create a new Substation instance from the embedded configuration.
sub, err := substation.New(ctx, cfg.Config)
if err != nil {
// Handle error.
panic(err)
}
// Print the Substation configuration.
fmt.Println(sub)
// Print the custom configuration.
fmt.Println(cfg)
}
Output: {"transforms":[{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},{"type":"send_stdout","settings":null}]} foo:bar
Example (SubstationCustomTransforms) ¶
package main
import (
"context"
"encoding/json"
"github.com/brexhq/substation/v2"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
"github.com/brexhq/substation/v2/transform"
)
func main() {
// Substation applications rely on a context for cancellation and timeouts.
ctx := context.Background()
// Define and load the configuration. This config includes a transform that
// is not part of the standard Substation package.
conf := []byte(`
{
"transforms":[
{"type":"utility_duplicate"},
{"type":"send_stdout"}
]
}
`)
cfg := substation.Config{}
if err := json.Unmarshal(conf, &cfg); err != nil {
// Handle error.
panic(err)
}
// Create a new Substation instance with a custom transform factory for loading
// the custom transform.
sub, err := substation.New(ctx, cfg, substation.WithTransformFactory(customFactory))
if err != nil {
// Handle error.
panic(err)
}
msg := []*message.Message{
message.New().SetData([]byte(`{"a":"b"}`)),
message.New().AsControl(),
}
// Transform the group of messages. In this example, results are not used.
if _, err := sub.Transform(ctx, msg...); err != nil {
// Handle error.
panic(err)
}
}
// customFactory is used in the custom transform example to load the custom transform.
func customFactory(ctx context.Context, cfg config.Config) (transform.Transformer, error) {
switch cfg.Type {
case "utility_duplicate":
return &utilityDuplicate{Count: 1}, nil
}
return transform.New(ctx, cfg)
}
// Duplicates a message.
type utilityDuplicate struct {
Count int `json:"count"`
}
func (t *utilityDuplicate) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
return []*message.Message{msg}, nil
}
output := []*message.Message{msg}
for i := 0; i < t.Count; i++ {
output = append(output, msg)
}
return output, nil
}
Output: {"a":"b"} {"a":"b"}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Library string
Functions ¶
func WithTransformFactory ¶
func WithTransformFactory(fac transform.Factory) func(*Substation)
WithTransformFactory implements a custom transform factory.
Types ¶
type Config ¶
type Config struct {
// Transforms contains a list of data transformatons that are executed.
Transforms []config.Config `json:"transforms"`
}
Config is the core configuration for the application. Custom applications should embed this and add additional configuration options.
type Substation ¶
type Substation struct {
// contains filtered or unexported fields
}
Substation provides access to data transformation functions.
Example ¶
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/brexhq/substation/v2"
"github.com/brexhq/substation/v2/message"
)
func main() {
// Substation applications rely on a context for cancellation and timeouts.
ctx := context.Background()
// Define a configuration. For native Substation applications, this is managed by Jsonnet.
//
// This example copies an object's value and prints the data to stdout.
conf := []byte(`
{
"transforms":[
{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},
{"type":"send_stdout"}
]
}
`)
cfg := substation.Config{}
if err := json.Unmarshal(conf, &cfg); err != nil {
// Handle error.
panic(err)
}
// Create a new Substation instance.
sub, err := substation.New(ctx, cfg)
if err != nil {
// Handle error.
panic(err)
}
// Print the Substation configuration.
fmt.Println(sub)
// Substation instances process data defined as a Message. Messages can be processed
// individually or in groups. This example processes multiple messages as a group.
msg := []*message.Message{
// The first message is a data message. Only data messages are transformed.
message.New().SetData([]byte(`{"a":"b"}`)),
// The second message is a ctrl message. ctrl messages flush the pipeline.
message.New().AsControl(),
}
// Transform the group of messages. In this example, results are not used.
if _, err := sub.Transform(ctx, msg...); err != nil {
// Handle error.
panic(err)
}
}
Output: {"transforms":[{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},{"type":"send_stdout","settings":null}]} {"a":"b","c":"b"}
func New ¶
func New(ctx context.Context, cfg Config, opts ...func(*Substation)) (*Substation, error)
New returns a new Substation instance.
func (*Substation) String ¶
func (s *Substation) String() string
String returns a JSON representation of the configuration.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
aws/lambda/autoscale
command
|
|
|
aws/lambda/substation
command
|
|
|
aws/lambda/validate
command
|
|
|
gcp/function/substation
command
|
|
|
substation
command
|
|
|
Package config provides structures for building configurations.
|
Package config provides structures for building configurations. |
|
internal
|
|
|
config
package config provides configuration types and functions for Substation.
|
package config provides configuration types and functions for Substation. |
|
file
package file provides functions that can be used to retrieve files from local and remote locations.
|
package file provides functions that can be used to retrieve files from local and remote locations. |
|
log
Package log wraps logrus and provides global logging only debug logging should be used in condition/, process/, and internal/ to reduce the likelihood of corrupting output for apps debug and info logging can be used in cmd/
|
Package log wraps logrus and provides global logging only debug logging should be used in condition/, process/, and internal/ to reduce the likelihood of corrupting output for apps debug and info logging can be used in cmd/ |
|
media
package media provides capabilities for inspecting the content of data and identifying its media (Multipurpose Internet Mail Extensions, MIME) type.
|
package media provides capabilities for inspecting the content of data and identifying its media (Multipurpose Internet Mail Extensions, MIME) type. |
|
secrets
Package secrets provides functions for retrieving local and remote secrets and interpolating them into configuration files.
|
Package secrets provides functions for retrieving local and remote secrets and interpolating them into configuration files. |
|
Package message provides functions for managing data used by conditions and transforms.
|
Package message provides functions for managing data used by conditions and transforms. |
|
Package transform provides functions for transforming messages.
|
Package transform provides functions for transforming messages. |
Click to show internal directories.
Click to hide internal directories.

