Skip to content

Commit

Permalink
Add ability to create slice in plugin (#573)
Browse files Browse the repository at this point in the history
* Add ability to create slice in plugin

* Add basic tests
  • Loading branch information
Mrod1598 authored Feb 24, 2022
1 parent ccd85d9 commit c100556
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ pipeline:
Writing a plugin is as easy as pulling out a set of operators in a working configuration file, then templatizing it with any parts of the config that need to be treated as variable. In the example of the Tomcat access log plugin above, that just means adding variables for `path` and `output`.

Plugins use Go's [`text/template`](https://golang.org/pkg/text/template/) package for template rendering. All fields from the plugin configuration are available as variables in the templates except the `type` field.

### Additional Template Functions

| Function Key | Argument(s) | Description |
| --- | --- | --- |
| `makeSlice` | `...Interface{}` | Takes any amount of interfaces and adds them into a slice and returns it. |
| `default` | `default interface{}`, `value interface{}` | Returns the default if the value is nil. |
6 changes: 6 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func RegisterPlugins(pluginDir string, registry *operator.Registry) []error {
func pluginFuncs() template.FuncMap {
funcs := make(map[string]interface{})
funcs["default"] = defaultPluginFunc
funcs["makeSlice"] = makeSlice
return funcs
}

Expand All @@ -250,3 +251,8 @@ func defaultPluginFunc(def interface{}, val interface{}) interface{} {
}
return val
}

// makeSlice is a plugin function returns a slice of all the given arguements
func makeSlice(args ...interface{}) []interface{} {
return args
}
10 changes: 10 additions & 0 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@ func TestDefaultPluginFuncWithoutValue(t *testing.T) {
require.Equal(t, "default_value", result)
}

func TestMakeSlicePluginFuncWithString(t *testing.T) {
result := makeSlice("string1", "string2", "string3", "string4")
require.Equal(t, []interface{}{"string1", "string2", "string3", "string4"}, result)
}

func TestDefaultPluginFuncWithInteger(t *testing.T) {
result := makeSlice(1, 2, 3, 4, 5)
require.Equal(t, []interface{}{1, 2, 3, 4, 5}, result)
}

func TestSplitPluginFile(t *testing.T) {
cases := map[int]struct {
input string
Expand Down

0 comments on commit c100556

Please sign in to comment.