Skip to content

Commit

Permalink
Fix plugin serialization and export plugin object on config (#157)
Browse files Browse the repository at this point in the history
* Fix plugin serialization and export plugin object on config

* Update changelog

* Remove unneeded Type field

* Fix tests
  • Loading branch information
camdencheek authored Oct 7, 2020
1 parent bb2acd5 commit 79fb34c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions operator/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 28 additions & 4 deletions plugin/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugin

import (
"encoding/json"
"fmt"
"strings"

Expand All @@ -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"`
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -69,13 +71,26 @@ 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{}
if err := unmarshal(&m); err != nil {
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
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion plugin/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -65,7 +66,7 @@ output: stdout
OperatorType: "my_plugin",
},
},
plugin: plugin,
Plugin: plugin,
Parameters: map[string]interface{}{
"unused_param": "test_unused",
},
Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down

0 comments on commit 79fb34c

Please sign in to comment.