Unverified Commit e3c26a0e authored by Christopher Speller's avatar Christopher Speller Committed by GitHub

Adding ability for plugin system to respect the defaults listed in the plugin manifest. (#9066)

parent b78b216a
......@@ -4,7 +4,6 @@
package app
import (
"encoding/json"
"runtime"
"github.com/mattermost/mattermost-server/mlog"
......@@ -98,13 +97,7 @@ func pluginSetting(pluginSettings *model.PluginSettings, plugin, key string, def
if !ok {
return defaultValue
}
var m map[string]interface{}
if b, err := json.Marshal(settings); err != nil {
return defaultValue
} else {
json.Unmarshal(b, &m)
}
if value, ok := m[key]; ok {
if value, ok := settings[key]; ok {
return value
}
return defaultValue
......
......@@ -32,8 +32,8 @@ func newTestServer() (chan string, *httptest.Server) {
func TestPluginSetting(t *testing.T) {
settings := &model.PluginSettings{
Plugins: map[string]interface{}{
"test": map[string]string{
Plugins: map[string]map[string]interface{}{
"test": map[string]interface{}{
"foo": "bar",
},
},
......
......@@ -6,30 +6,45 @@ package app
import (
"encoding/json"
"fmt"
"strings"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
)
type PluginAPI struct {
id string
app *App
logger *mlog.SugarLogger
id string
app *App
logger *mlog.SugarLogger
manifest *model.Manifest
}
func NewPluginAPI(a *App, manifest *model.Manifest) *PluginAPI {
return &PluginAPI{
id: manifest.Id,
app: a,
logger: a.Log.With(mlog.String("plugin_id", manifest.Id)).Sugar(),
id: manifest.Id,
manifest: manifest,
app: a,
logger: a.Log.With(mlog.String("plugin_id", manifest.Id)).Sugar(),
}
}
func (api *PluginAPI) LoadPluginConfiguration(dest interface{}) error {
if b, err := json.Marshal(api.app.Config().PluginSettings.Plugins[api.id]); err != nil {
finalConfig := make(map[string]interface{})
// First set final config to defaults
for _, setting := range api.manifest.SettingsSchema.Settings {
finalConfig[strings.ToLower(setting.Key)] = setting.Default
}
// If we have settings given we override the defaults with them
for setting, value := range api.app.Config().PluginSettings.Plugins[api.id] {
finalConfig[strings.ToLower(setting)] = value
}
if pluginSettingsJsonBytes, err := json.Marshal(finalConfig); err != nil {
return err
} else {
return json.Unmarshal(b, dest)
return json.Unmarshal(pluginSettingsJsonBytes, dest)
}
}
......
......@@ -1717,7 +1717,7 @@ type PluginSettings struct {
EnableUploads *bool
Directory *string
ClientDirectory *string
Plugins map[string]interface{}
Plugins map[string]map[string]interface{}
PluginStates map[string]*PluginState
}
......@@ -1747,7 +1747,7 @@ func (s *PluginSettings) SetDefaults() {
}
if s.Plugins == nil {
s.Plugins = make(map[string]interface{})
s.Plugins = make(map[string]map[string]interface{})
}
if s.PluginStates == nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment