[MM-52924] Implement ConfigurationWillBeSaved plugin hook (#23567)
* Implement ConfigurationWillBeSaved plugin hook * Add comment * Update comment * Fix potential nil dereference if plugin environment is unset * Address PR review
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
d0ad46b496
Коммит
62a3ee8adc
@@ -138,6 +138,10 @@ type GetTopicMetadataByIdsIFace interface {
|
||||
GetTopicMetadataByIds(c *Context, topicType string, topicIds []string) (map[string]*model.TopicMetadata, error)
|
||||
}
|
||||
|
||||
type ConfigurationWillBeSavedIFace interface {
|
||||
ConfigurationWillBeSaved(newCfg *model.Config) (*model.Config, error)
|
||||
}
|
||||
|
||||
type HooksAdapter struct {
|
||||
implemented map[int]struct{}
|
||||
productHooks any
|
||||
@@ -430,6 +434,15 @@ func NewAdapter(productHooks any) (*HooksAdapter, error) {
|
||||
return nil, errors.New("hook has GetTopicMetadataByIds method but does not implement plugin.GetTopicMetadataByIds interface")
|
||||
}
|
||||
|
||||
// Assessing the type of the productHooks if it individually implements ConfigurationWillBeSaved interface.
|
||||
tt = reflect.TypeOf((*ConfigurationWillBeSavedIFace)(nil)).Elem()
|
||||
|
||||
if ft.Implements(tt) {
|
||||
a.implemented[ConfigurationWillBeSavedID] = struct{}{}
|
||||
} else if _, ok := ft.MethodByName("ConfigurationWillBeSaved"); ok {
|
||||
return nil, errors.New("hook has ConfigurationWillBeSaved method but does not implement plugin.ConfigurationWillBeSaved interface")
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
@@ -711,3 +724,12 @@ func (a *HooksAdapter) GetTopicMetadataByIds(c *Context, topicType string, topic
|
||||
return a.productHooks.(GetTopicMetadataByIdsIFace).GetTopicMetadataByIds(c, topicType, topicIds)
|
||||
|
||||
}
|
||||
|
||||
func (a *HooksAdapter) ConfigurationWillBeSaved(newCfg *model.Config) (*model.Config, error) {
|
||||
if _, ok := a.implemented[ConfigurationWillBeSavedID]; !ok {
|
||||
panic("product hooks must implement ConfigurationWillBeSaved")
|
||||
}
|
||||
|
||||
return a.productHooks.(ConfigurationWillBeSavedIFace).ConfigurationWillBeSaved(newCfg)
|
||||
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user