Documentation
¶
Overview ¶
Package resource provides an alternate method of acquiring resources.
All functions will panic if h is not from type internal/manager.Manager.
Index ¶
- Variables
- func GetConfig[T ConfigProvider](h types.InternalHandle) (T, error)
- func GetDB[T DatabaseProvider](h types.InternalHandle) (T, error)
- func GetPlugin[T interface{ ... }](h types.InternalHandle, name string) (T, error)
- func GetPluginMap(h types.InternalHandle) map[string]types.Plugin
- type ConfigHandle
- type ConfigProvider
- type DatabaseProvider
Constants ¶
This section is empty.
Variables ¶
var ErrorPluginIncorrectType = errors.New("plugin is incorrect type")
var ErrorPluginNotExist = errors.New("plugin does not exist")
Functions ¶
func GetConfig ¶
func GetConfig[T ConfigProvider](h types.InternalHandle) (T, error)
GetConfig returns the configuration for the plugin.
A ConfigHandle is simply an *os.File, but this is not an API guarantee.
func GetDB ¶
func GetDB[T DatabaseProvider](h types.InternalHandle) (T, error)
GetDB returns a database interface that is safe to use within the plugin, insofar as it is guaranteed not to be used unsafely elsewhere.
Currently, *types.DB and *sql.DB are shared between plugins, as they are pooled connections; and *pgx.Conn and *pgxpool.Pool are created on demand for the plugin requesting it.
Thread safety is a property of the specific T requested; specifically, *pgx.Conn is not thread-safe.
func GetPlugin ¶
Gets a plugin from the plugin map and checks if it fulfills the provided interface T. Note that if used in Plugin.Init, the other plugin might not be loaded yet. Use at runtime e.g. inside a route.
Compare errors ErrorPluginNotExist and ErrorPluginIncorrectType with errors.Is.
Example:
h := pa.Unstable.GetResHandle() avapi, err := resource.GetPlugin[avatar.AvatarPlugin](h, avatar.Plugin.Name()) if err != nil { p.logger.Errorf("Failed to get avatar plugin: %s", err.Error()) return } url, err := avapi.XGetAvatarURL(context.Background(), "username") if err != nil { p.logger.Errorf("Could not fetch avatar: %s", err.Error()) return } p.logger.Infof("URL: %s", url)
func GetPluginMap ¶
func GetPluginMap(h types.InternalHandle) map[string]types.Plugin
Types ¶
type ConfigHandle ¶
type ConfigHandle struct{ io.ReadSeekCloser }
type ConfigProvider ¶
type ConfigProvider interface { ConfigHandle | *viper.Viper }