resource

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var ErrorPluginIncorrectType = errors.New("plugin is incorrect type")
View Source
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

func GetPlugin[T interface{ types.Plugin }](h types.InternalHandle, name string) (T, error)

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
}

type DatabaseProvider

type DatabaseProvider interface {
	*types.DB | *sql.DB | *pgx.Conn | *pgxpool.Pool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL