From 79fb34c99929b64f0936974b3f2604f18802abad Mon Sep 17 00:00:00 2001 From: Camden Cheek Date: Wed, 7 Oct 2020 12:06:38 -0400 Subject: [PATCH] Fix plugin serialization and export plugin object on config (#157) * Fix plugin serialization and export plugin object on config * Update changelog * Remove unneeded Type field * Fix tests --- CHANGELOG.md | 4 ++++ operator/buffer/buffer.go | 8 ++++++++ plugin/config.go | 32 ++++++++++++++++++++++++++++---- plugin/config_test.go | 3 ++- plugin/plugin.go | 2 +- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8175524c..f7cb1b5cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.12.3] - 2020-10-07 +### Fixed +- (De)serialization of JSON for plugin config structs + ## [0.12.2] - 2020-10-06 ### Added - New Relic Logs output operator diff --git a/operator/buffer/buffer.go b/operator/buffer/buffer.go index bdf23280d..0b2f19171 100644 --- a/operator/buffer/buffer.go +++ b/operator/buffer/buffer.go @@ -65,5 +65,13 @@ func (bc *Config) unmarshal(unmarshal func(interface{}) error) error { } } +func (bc Config) MarshalYAML() (interface{}, error) { + return bc.Builder, nil +} + +func (bc Config) MarshalJSON() ([]byte, error) { + return json.Marshal(bc.Builder) +} + // FlushFunc is a function that can be called to mark the returned entries as flushed type FlushFunc func() error diff --git a/plugin/config.go b/plugin/config.go index 902a7cbd6..e035015e9 100644 --- a/plugin/config.go +++ b/plugin/config.go @@ -1,6 +1,7 @@ package plugin import ( + "encoding/json" "fmt" "strings" @@ -17,7 +18,7 @@ var _ operator.MultiBuilder = (*Config)(nil) // Config is the config values for the plugin type Config struct { helper.WriterConfig - plugin *Plugin + Plugin *Plugin `json:"-" yaml:"-"` Parameters map[string]interface{} `json:",squash" yaml:",squash"` } @@ -28,7 +29,7 @@ func (c *Config) BuildMulti(bc operator.BuildContext) ([]operator.Operator, erro } params := c.getRenderParams(bc) - pipelineConfigBytes, err := c.plugin.Render(params) + pipelineConfigBytes, err := c.Plugin.Render(params) if err != nil { return nil, err } @@ -53,6 +54,7 @@ func (c *Config) getRenderParams(bc operator.BuildContext) map[string]interface{ // Add ID and output to params params["input"] = bc.PrependNamespace(c.ID()) + params["id"] = c.ID() params["output"] = c.yamlOutputs(bc) return params } @@ -69,6 +71,15 @@ func (c *Config) yamlOutputs(bc operator.BuildContext) string { return fmt.Sprintf("[%s]", strings.Join(namespacedOutputs, ",")) } +func (c *Config) UnmarshalJSON(raw []byte) error { + var m map[string]interface{} + if err := json.Unmarshal(raw, &m); err != nil { + return err + } + + return c.unmarshalMap(m) +} + // UnmarshalYAML unmarshals YAML func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { var m map[string]interface{} @@ -76,6 +87,10 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } + return c.unmarshalMap(m) +} + +func (c *Config) unmarshalMap(m map[string]interface{}) error { if id, ok := m["id"]; ok { if idString, ok := id.(string); ok { c.OperatorID = idString @@ -109,12 +124,21 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { // MarshalYAML marshals YAML func (c Config) MarshalYAML() (interface{}, error) { - var m map[string]interface{} + return c.toMap(), nil +} + +// MarshalJSON marshals JSON +func (c Config) MarshalJSON() ([]byte, error) { + return json.Marshal(c.toMap()) +} + +func (c Config) toMap() map[string]interface{} { + m := make(map[string]interface{}) for k, v := range c.Parameters { m[k] = v } m["id"] = c.ID() m["type"] = c.Type() m["output"] = c.OutputIDs - return m, nil + return m } diff --git a/plugin/config_test.go b/plugin/config_test.go index 87fe8f861..6462526e7 100644 --- a/plugin/config_test.go +++ b/plugin/config_test.go @@ -26,6 +26,7 @@ func TestGetRenderParams(t *testing.T) { "param1": "value1", "param2": "value2", "input": "$.test", + "id": "test", "output": "[$.out1,$.out2]", } require.Equal(t, expected, params) @@ -65,7 +66,7 @@ output: stdout OperatorType: "my_plugin", }, }, - plugin: plugin, + Plugin: plugin, Parameters: map[string]interface{}{ "unused_param": "test_unused", }, diff --git a/plugin/plugin.go b/plugin/plugin.go index 304d45582..a6c14977d 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -26,7 +26,7 @@ type Plugin struct { // NewBuilder creates a new, empty config that can build into an operator func (p *Plugin) NewBuilder() operator.MultiBuilder { return &Config{ - plugin: p, + Plugin: p, } }