github.com/Jeffail/benthos/v3@v3.65.0/public/bloblang/example_plugins_test.go (about)

     1  package bloblang_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  	"strings"
     8  
     9  	"github.com/Jeffail/benthos/v3/public/bloblang"
    10  )
    11  
    12  // This example demonstrates how to create a Bloblang function with the simpler
    13  // V1 API and execute them with a Bloblang mapping. Note that functions
    14  // registered this way will NOT support named parameters, for named parameters
    15  // use the V2 register API.
    16  func Example_bloblangFunctionPluginV1() {
    17  	if err := bloblang.RegisterFunction("add_but_always_slightly_wrong", func(args ...interface{}) (bloblang.Function, error) {
    18  		var left, right float64
    19  
    20  		if err := bloblang.NewArgSpec().
    21  			Float64Var(&left).
    22  			Float64Var(&right).
    23  			Extract(args); err != nil {
    24  			return nil, err
    25  		}
    26  
    27  		return func() (interface{}, error) {
    28  			return left + right + 0.02, nil
    29  		}, nil
    30  	}); err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	mapping := `
    35  root.num = add_but_always_slightly_wrong(this.a, this.b)
    36  `
    37  
    38  	exe, err := bloblang.Parse(mapping)
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	res, err := exe.Query(map[string]interface{}{
    44  		"a": 1.2, "b": 2.6,
    45  	})
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	jsonBytes, err := json.Marshal(res)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  	fmt.Println(string(jsonBytes))
    56  	// Output: {"num":3.82}
    57  }
    58  
    59  // This example demonstrates how to create Bloblang methods with the simpler API
    60  // and execute them with a Bloblang mapping. Note that methods registered this
    61  // way will NOT support named parameters, for named parameters use the V2
    62  // register API.
    63  func Example_bloblangMethodPluginV1() {
    64  	if err := bloblang.RegisterMethod("cuddle", func(args ...interface{}) (bloblang.Method, error) {
    65  		var prefix string
    66  		var suffix string
    67  
    68  		if err := bloblang.NewArgSpec().
    69  			StringVar(&prefix).
    70  			StringVar(&suffix).
    71  			Extract(args); err != nil {
    72  			return nil, err
    73  		}
    74  
    75  		return bloblang.StringMethod(func(s string) (interface{}, error) {
    76  			return prefix + s + suffix, nil
    77  		}), nil
    78  	}); err != nil {
    79  		panic(err)
    80  	}
    81  
    82  	if err := bloblang.RegisterMethod("shuffle", func(_ ...interface{}) (bloblang.Method, error) {
    83  		rand := rand.New(rand.NewSource(0))
    84  		return bloblang.ArrayMethod(func(in []interface{}) (interface{}, error) {
    85  			out := make([]interface{}, len(in))
    86  			copy(out, in)
    87  			rand.Shuffle(len(out), func(i, j int) {
    88  				out[i], out[j] = out[j], out[i]
    89  			})
    90  			return out, nil
    91  		}), nil
    92  	}); err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	mapping := `
    97  root.new_summary = this.summary.cuddle("meow", "woof")
    98  root.shuffled = this.names.shuffle()
    99  `
   100  
   101  	exe, err := bloblang.Parse(mapping)
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  
   106  	res, err := exe.Query(map[string]interface{}{
   107  		"summary": "quack",
   108  		"names":   []interface{}{"denny", "pixie", "olaf", "jen", "spuz"},
   109  	})
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  
   114  	jsonBytes, err := json.Marshal(res)
   115  	if err != nil {
   116  		panic(err)
   117  	}
   118  
   119  	fmt.Println(string(jsonBytes))
   120  	// Output: {"new_summary":"meowquackwoof","shuffled":["olaf","jen","pixie","denny","spuz"]}
   121  }
   122  
   123  // This example demonstrates how to create and use an isolated Bloblang
   124  // environment with some standard functions removed.
   125  func Example_bloblangRestrictedEnvironment() {
   126  	env := bloblang.NewEnvironment().WithoutFunctions("env", "file")
   127  
   128  	if err := env.RegisterMethod("custom_thing", func(args ...interface{}) (bloblang.Method, error) {
   129  		return bloblang.StringMethod(func(s string) (interface{}, error) {
   130  			return strings.ToUpper(s), nil
   131  		}), nil
   132  	}); err != nil {
   133  		panic(err)
   134  	}
   135  
   136  	mapping := `
   137  root.foo = this.foo.string()
   138  root.bar = this.bar + this.baz
   139  root.buz = this.buz.content.custom_thing()
   140  `
   141  
   142  	exe, err := env.Parse(mapping)
   143  	if err != nil {
   144  		panic(err)
   145  	}
   146  
   147  	res, err := exe.Query(map[string]interface{}{
   148  		"foo": 50.0,
   149  		"bar": "first bit ",
   150  		"baz": "second bit",
   151  		"buz": map[string]interface{}{
   152  			"id":      "XXXX",
   153  			"content": "some nested content",
   154  		},
   155  	})
   156  	if err != nil {
   157  		panic(err)
   158  	}
   159  
   160  	jsonBytes, err := json.Marshal(res)
   161  	if err != nil {
   162  		panic(err)
   163  	}
   164  
   165  	fmt.Println(string(jsonBytes))
   166  	// Output: {"bar":"first bit second bit","buz":"SOME NESTED CONTENT","foo":"50"}
   167  }