Skip to content

Commit 13ac3d3

Browse files
committed
Channel default value by changing default logic
In a nutshell, the newly introduced plugin.PopulateDefaults function populates all fields of a Plugin-implementing struct with those fields from Info.ConfigAttributes where ConfigOption.Default is set. Thus, a single function call before parsing the user-submitted configuration within the Plugin.SetConfig method sets default values. As a result of the discussion between the Go and the Web team, summarized in #205, Web does not store key-value pairs to be omitted. Prior, an already JSON-encoded version of the ConfigOption slice was present in plugin.Info. Thus, this data wasn't easily available anymore. As the new code now needs to access this field, it was changed and a custom sql driver.Valuer was implemented for a slice type. While working on this, all ConfigOptions were put in the same order as the struct fields. Requires <Icinga/icinga-notifications-web#230>. Closes #205.
1 parent 1281dc0 commit 13ac3d3

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

notifications/plugin/plugin.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plugin
22

33
import (
4+
"database/sql/driver"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -69,13 +70,23 @@ type ConfigOption struct {
6970
Max types.Int `json:"max,omitempty"`
7071
}
7172

73+
// ConfigOptions describes all ConfigOption entries.
74+
//
75+
// This type became necessary to implement the database.sql.driver.Valuer to marshal it into JSON.
76+
type ConfigOptions []ConfigOption
77+
78+
// Value implements database.sql's driver.Valuer to represent all ConfigOptions as a JSON array.
79+
func (c ConfigOptions) Value() (driver.Value, error) {
80+
return json.Marshal(c)
81+
}
82+
7283
// Info contains plugin information.
7384
type Info struct {
74-
Type string `db:"type" json:"-"`
75-
Name string `db:"name" json:"name"`
76-
Version string `db:"version" json:"version"`
77-
Author string `db:"author" json:"author"`
78-
ConfigAttributes json.RawMessage `db:"config_attrs" json:"config_attrs"` // ConfigOption(s) as json-encoded list
85+
Type string `db:"type" json:"-"`
86+
Name string `db:"name" json:"name"`
87+
Version string `db:"version" json:"version"`
88+
Author string `db:"author" json:"author"`
89+
ConfigAttributes ConfigOptions `db:"config_attrs" json:"config_attrs"`
7990
}
8091

8192
// TableName implements the contracts.TableNamer interface.
@@ -131,6 +142,25 @@ type Plugin interface {
131142
SendNotification(req *NotificationRequest) error
132143
}
133144

145+
// PopulateDefaults sets the struct fields from Info.ConfigAttributes where ConfigOption.Default is set.
146+
//
147+
// It should be called from each channel plugin within its Plugin.SetConfig before doing any further configuration.
148+
func PopulateDefaults(typePtr Plugin) error {
149+
defaults := make(map[string]any)
150+
for _, confAttr := range typePtr.GetInfo().ConfigAttributes {
151+
if confAttr.Default != nil {
152+
defaults[confAttr.Name] = confAttr.Default
153+
}
154+
}
155+
156+
defaultConf, err := json.Marshal(defaults)
157+
if err != nil {
158+
return err
159+
}
160+
161+
return json.Unmarshal(defaultConf, typePtr)
162+
}
163+
134164
// RunPlugin reads the incoming stdin requests, processes and writes the responses to stdout
135165
func RunPlugin(plugin Plugin) {
136166
encoder := json.NewEncoder(os.Stdout)

0 commit comments

Comments
 (0)