Repository: answerdev/answer
File: internal/service/plugin_common/plugin_common_service.go (Go)

Code before:
	return nil
}

// GetUserPluginConfig get user plugin config
func (ps *PluginCommonService) GetUserPluginConfig(ctx context.Context, req *schema.GetUserPluginConfigReq) (
	configValue string, err error) {
	pluginUserConfig, exist, err := ps.pluginUserConfigRepo.GetPluginUserConfig(ctx, req.UserID, req.PluginSlugName)
	if err != nil {
		return "", err
	}
	if !exist {
		return "", nil
	}
	return pluginUserConfig.Value, nil
}

func (ps *PluginCommonService) initPluginData() {
	plugin.SetKVStorageDB(&plugin.Data{
		DB:    ps.data.DB,
		Cache: ps.data.Cache,
	})

	// init plugin status
	pluginStatus, err := ps.configService.GetStringValue(context.TODO(), constant.PluginStatus)
	if err != nil {
		log.Error(err)
	} else {
		if err := plugin.StatusManager.UnmarshalJSON([]byte(pluginStatus)); err != nil {
			log.Error(err)
		}
	}

	// init plugin config
	pluginConfigs, err := ps.pluginConfigRepo.GetPluginConfigAll(context.Background())
	if err != nil {
		log.Error(err)
	} else {
		for _, pluginConfig := range pluginConfigs {
			err := plugin.CallConfig(func(fn plugin.Config) error {
				if fn.Info().SlugName == pluginConfig.PluginSlugName {
					return fn.ConfigReceiver([]byte(pluginConfig.Value))
				}
				return nil
			})
			if err != nil {
				log.Errorf("parse plugin config failed: %s %v", pluginConfig.PluginSlugName, err)
			}
		}

		_ = plugin.CallCache(func(cache plugin.Cache) error {
			ps.data.Cache = cache

Review comment (performance):
This part I think can be consistent with the way other plugins are initialized. Such as

```go
		_ = plugin.CallKVStorage(func(k plugin.KVStorage) error {
		k.SetOperator(plugin.NewKVOperator(&plugin.Data{
			DB:    ps.data.DB,
			Cache: ps.data.Cache,
		}, k.Info().SlugName))
		return nil
	})
```

Code after:
	return nil
}

// GetUserPluginConfig get user plugin config
func (ps *PluginCommonService) GetUserPluginConfig(ctx context.Context, req *schema.GetUserPluginConfigReq) (
	configValue string, err error) {
	pluginUserConfig, exist, err := ps.pluginUserConfigRepo.GetPluginUserConfig(ctx, req.UserID, req.PluginSlugName)
	if err != nil {
		return "", err
	}
	if !exist {
		return "", nil
	}
	return pluginUserConfig.Value, nil
}

func (ps *PluginCommonService) initPluginData() {
	_ = plugin.CallKVStorage(func(k plugin.KVStorage) error {
		k.SetOperator(plugin.NewKVOperator(
			ps.data.DB,
			ps.data.Cache,
			k.Info().SlugName,
		))
		return nil
	})

	// init plugin status
	pluginStatus, err := ps.configService.GetStringValue(context.TODO(), constant.PluginStatus)
	if err != nil {
		log.Error(err)
	} else {
		if err := plugin.StatusManager.UnmarshalJSON([]byte(pluginStatus)); err != nil {
			log.Error(err)
		}
	}

	// init plugin config
	pluginConfigs, err := ps.pluginConfigRepo.GetPluginConfigAll(context.Background())
	if err != nil {
		log.Error(err)
	} else {
		for _, pluginConfig := range pluginConfigs {
			err := plugin.CallConfig(func(fn plugin.Config) error {
				if fn.Info().SlugName == pluginConfig.PluginSlugName {
					return fn.ConfigReceiver([]byte(pluginConfig.Value))
				}
				return nil
			})
			if err != nil {
				log.Errorf("parse plugin config failed: %s %v", pluginConfig.PluginSlugName, err)
			}