From 5a04066c55a6e6a04ba4df254a6f4d38753e9ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 16 Jun 2024 16:42:32 +0200 Subject: [PATCH] add PluginSettings unmarshaller. --- pkg/types/member.go | 2 +- pkg/types/plugins.go | 18 ++++++++++++++++++ pkg/types/session.go | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/types/member.go b/pkg/types/member.go index 4463f4dc..e3558060 100644 --- a/pkg/types/member.go +++ b/pkg/types/member.go @@ -23,7 +23,7 @@ type MemberProfile struct { CanSeeInactiveCursors bool `json:"can_see_inactive_cursors" mapstructure:"can_see_inactive_cursors"` // plugin scope - Plugins map[string]any `json:"plugins"` + Plugins PluginSettings `json:"plugins"` } type MemberProvider interface { diff --git a/pkg/types/plugins.go b/pkg/types/plugins.go index fa57b8f1..d0abd674 100644 --- a/pkg/types/plugins.go +++ b/pkg/types/plugins.go @@ -2,10 +2,16 @@ package types import ( "errors" + "fmt" + "github.com/demodesk/neko/pkg/utils" "github.com/spf13/cobra" ) +var ( + ErrPluginSettingsNotFound = errors.New("plugin settings not found") +) + type Plugin interface { Name() string Config() PluginConfig @@ -61,3 +67,15 @@ func (p *PluginManagers) Validate() error { return nil } + +type PluginSettings map[string]any + +func (p PluginSettings) Unmarshal(name string, def any) error { + if p == nil { + return fmt.Errorf("%w: %s", ErrPluginSettingsNotFound, name) + } + if _, ok := p[name]; !ok { + return fmt.Errorf("%w: %s", ErrPluginSettingsNotFound, name) + } + return utils.Decode(p[name], def) +} diff --git a/pkg/types/session.go b/pkg/types/session.go index 1b4d1f7f..4ec4085c 100644 --- a/pkg/types/session.go +++ b/pkg/types/session.go @@ -49,7 +49,7 @@ type Settings struct { MercifulReconnect bool `json:"merciful_reconnect"` // plugin scope - Plugins map[string]any `json:"plugins"` + Plugins PluginSettings `json:"plugins"` } type Session interface {