Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/plugin/sdk/deployment_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func (c *ApplicationConfig[Spec]) parsePluginConfig(pluginName string) error {
c.pluginConfigs = nil
}()

if c.pluginConfigs == nil || c.pluginConfigs[pluginName] == nil {
// No config is set for this plugin.
// Set the Spec to the zero value of the spec type to avoid nil pointer dereference.
c.Spec = new(Spec)
return nil
// It is necessary to prepare config with default value when users don't set any config, or when plugin developers implement custom unmarshalling logic.
data := []byte("{}")

if c.pluginConfigs != nil && c.pluginConfigs[pluginName] != nil {
data = c.pluginConfigs[pluginName]
}

var spec Spec
if err := json.Unmarshal(c.pluginConfigs[pluginName], &spec); err != nil {
if err := json.Unmarshal(data, &spec); err != nil {
return fmt.Errorf("failed to unmarshal application config: plugin spec: %w", err)
}

Expand Down
39 changes: 21 additions & 18 deletions pkg/plugin/sdk/deployment_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ func TestApplicationConfig_HasStage(t *testing.T) {
}

type testPluginSpec struct {
Name string `json:"name"`
Value int `json:"value" default:"42"`
Require string `json:"require"`
Name string `json:"name"`
Value int `json:"value" default:"42"`
}

func (s *testPluginSpec) Validate() error {
if s.Require == "" {
return fmt.Errorf("require must not be empty")
if s.Value < 0 {
return fmt.Errorf("value must not be a negative value")
}
return nil
}
Expand All @@ -134,30 +133,35 @@ func TestApplicationConfig_ParsePluginConfig(t *testing.T) {
config: &ApplicationConfig[testPluginSpec]{
pluginConfigs: nil,
},
wantSpec: &testPluginSpec{},
wantErr: false,
wantSpec: &testPluginSpec{
Name: "",
Value: 42,
},
wantErr: false,
},
{
name: "empty plugin configs map",
pluginName: "test-plugin",
config: &ApplicationConfig[testPluginSpec]{
pluginConfigs: make(map[string]json.RawMessage),
},
wantSpec: &testPluginSpec{},
wantErr: false,
wantSpec: &testPluginSpec{
Name: "",
Value: 42,
},
wantErr: false,
},
{
name: "valid plugin config with defaults",
pluginName: "test-plugin",
config: &ApplicationConfig[testPluginSpec]{
pluginConfigs: map[string]json.RawMessage{
"test-plugin": json.RawMessage(`{"name": "test", "require": "yes"}`),
"test-plugin": json.RawMessage(`{"name": "test"}`),
},
},
wantSpec: &testPluginSpec{
Name: "test",
Value: 42,
Require: "yes",
Name: "test",
Value: 42,
},
wantErr: false,
},
Expand All @@ -177,7 +181,7 @@ func TestApplicationConfig_ParsePluginConfig(t *testing.T) {
pluginName: "test-plugin",
config: &ApplicationConfig[testPluginSpec]{
pluginConfigs: map[string]json.RawMessage{
"test-plugin": json.RawMessage(`{"name": "test", "value": 10}`),
"test-plugin": json.RawMessage(`{"name": "test", "value": -1}`),
},
},
wantSpec: nil,
Expand All @@ -188,13 +192,12 @@ func TestApplicationConfig_ParsePluginConfig(t *testing.T) {
pluginName: "test-plugin",
config: &ApplicationConfig[testPluginSpec]{
pluginConfigs: map[string]json.RawMessage{
"test-plugin": json.RawMessage(`{"name": "test", "value": 100, "require": "yes"}`),
"test-plugin": json.RawMessage(`{"name": "test", "value": 100}`),
},
},
wantSpec: &testPluginSpec{
Name: "test",
Value: 100,
Require: "yes",
Name: "test",
Value: 100,
},
wantErr: false,
},
Expand Down