transform

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 63 Imported by: 0

README

transform

Contains interfaces and methods for transforming data.

Documentation

Overview

Package transform provides functions for transforming messages.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, tf []Transformer, msgs ...*message.Message) ([]*message.Message, error)

Applies one or more transform functions to one or more messages.

Types

type Factory

type Factory func(context.Context, config.Config) (Transformer, error)

Factory can be used to implement custom transform factory functions.

type Transformer

type Transformer interface {
	Transform(context.Context, *message.Message) ([]*message.Message, error)
}

Transformer is the interface implemented by all transforms and provides the ability to transform a message.

Example
package main

import (
	"context"
	"fmt"

	"github.com/brexhq/substation/v2/config"
	"github.com/brexhq/substation/v2/message"
	"github.com/brexhq/substation/v2/transform"
)

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

	// Copies the value of key "a" into key "b".
	cfg := config.Config{
		Type: "object_copy",
		Settings: map[string]interface{}{
			"object": map[string]interface{}{
				"source_key": "a",
				"target_key": "b",
			},
		},
	}

	tf, err := transform.New(ctx, cfg)
	if err != nil {
		// handle err
		panic(err)
	}

	// Transformer is applied to a message.
	msg := message.New().SetData([]byte(`{"a":1}`))
	results, err := tf.Transform(ctx, msg)
	if err != nil {
		// handle err
		panic(err)
	}

	for _, c := range results {
		fmt.Println(string(c.Data()))
	}

}
Output:

{"a":1,"b":1}

func New

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

New is a factory function for returning a configured Transformer.

Source Files

Jump to

Keyboard shortcuts

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