Documentation ¶
Overview ¶
Package autowire provides office initialization through a YAML config.
Example ¶
package main import ( "bytes" "context" "strings" "github.com/bounoable/postdog/autowire" "github.com/bounoable/postdog/letter" "github.com/bounoable/postdog/plugin/markdown" "github.com/bounoable/postdog/transport/smtp" ) func main() { config := ` transports: dev: provider: smtp config: host: smtp.mailtrap.io username: abcdef123456 password: 123456abcdef plugins: - name: markdown config: use: goldmark overrideHTML: true ` // Load YAML config cfg, err := autowire.Load( strings.NewReader(config), smtp.Register, // register SMTP transport factory markdown.Register, // register Markdown plugin factory ) if err != nil { panic(err) } // Initialize office po, err := cfg.Office( context.Background(), // Office options ... ) if err != nil { panic(err) } // Send mail with default transport err = po.Send( context.Background(), letter.Write( letter.From("Bob", "bob@belcher.test"), letter.To("Calvin", "calvin@fishoeder.test"), letter.To("Felix", "felix@fishoeder.test"), letter.BCC("Jimmy", "jimmy@pesto.test"), letter.Subject("Hi, buddy."), letter.Text("# Have a drink later?"), letter.MustAttach(bytes.NewReader([]byte("tasty")), "burgerrecipe.txt", letter.ContentType("text/plain")), ), ) // or use a specific transport err = po.SendWith( context.Background(), "test2", letter.Write( letter.From("Bob", "bob@belcher.test"), // ... ), ) }
Output:
Index ¶
- type Config
- func (cfg Config) Get(name string) (TransportConfig, error)
- func (cfg *Config) Load(r io.Reader) error
- func (cfg *Config) LoadFile(path string) error
- func (cfg Config) Office(ctx context.Context, opts ...postdog.Option) (*postdog.Office, error)
- func (cfg Config) RegisterPlugin(name string, factory PluginFactory)
- func (cfg Config) RegisterProvider(name string, factory TransportFactory)
- type DuplicateTransportError
- type InvalidConfigError
- type Option
- type PluginConfig
- type PluginFactory
- type PluginFactoryFunc
- type QueueConfig
- type TransportConfig
- type TransportFactory
- type TransportFactoryFunc
- type UnconfiguredTransportError
- type UnregisteredPluginError
- type UnregisteredProviderError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { TransportFactories map[string]TransportFactory Transports map[string]TransportConfig DefaultTransport string PluginFactories map[string]PluginFactory Plugins []PluginConfig Queue QueueConfig }
Config is the autowire configuration. You should use the Load() or File() functions to build the configuration.
func New ¶
New initializes a new autowire configuration. Instead of calling New() directly, you should use Load() or File() instead.
func (Config) Get ¶
func (cfg Config) Get(name string) (TransportConfig, error)
Get returns the parsed configuration for the given transport name. Calling Get() with an unconfigured transport name results in an UnconfiguredTransportError.
func (*Config) LoadFile ¶
LoadFile loads the YAML autowire configuration from the file at the given path.
func (Config) Office ¶
Office builds the *postdog.Office from the autowire configuration. You have to register the used providers with opts, if they haven't been globally registered.
func (Config) RegisterPlugin ¶ added in v0.4.0
func (cfg Config) RegisterPlugin(name string, factory PluginFactory)
RegisterPlugin registers the factory for the given plugin name.
func (Config) RegisterProvider ¶
func (cfg Config) RegisterProvider(name string, factory TransportFactory)
RegisterProvider registers a transport factory for the given provider name.
type DuplicateTransportError ¶
type DuplicateTransportError struct {
Name string
}
DuplicateTransportError means the YAML configuration contains multiple configurations for the same transport name.
func (DuplicateTransportError) Error ¶
func (err DuplicateTransportError) Error() string
type InvalidConfigError ¶
type InvalidConfigError struct { Transport string ConfigKey string Expected interface{} Provided interface{} }
InvalidConfigError means the configuration for a transport contains an invalid value.
func (InvalidConfigError) Error ¶
func (err InvalidConfigError) Error() string
type Option ¶
type Option func(*Config)
Option is an autowire constructor option.
func Provider ¶
func Provider(name string, factory TransportFactory) Option
Provider registers a transport factory for the given provider name. Providers have to be registered in order to be used in the configuration file.
type PluginConfig ¶ added in v0.4.0
PluginConfig is the configuration for a plugin.
type PluginFactory ¶ added in v0.4.0
type PluginFactory interface {
CreatePlugin(ctx context.Context, cfg map[string]interface{}) (postdog.Plugin, error)
}
PluginFactory creates the plugin from the given cfg.
type PluginFactoryFunc ¶ added in v0.4.0
PluginFactoryFunc allows functions to be used as a PluginFactory.
func (PluginFactoryFunc) CreatePlugin ¶ added in v0.4.0
func (fn PluginFactoryFunc) CreatePlugin(ctx context.Context, cfg map[string]interface{}) (postdog.Plugin, error)
CreatePlugin creates the plugin from the given cfg.
type QueueConfig ¶ added in v0.4.5
type QueueConfig struct {
Buffer int
}
QueueConfig is the send queue configuration.
type TransportConfig ¶
TransportConfig contains the parsed used-provided configuration for a single transport.
type TransportFactory ¶
type TransportFactory interface {
CreateTransport(ctx context.Context, cfg map[string]interface{}) (postdog.Transport, error)
}
TransportFactory create the transport from the given cfg.
type TransportFactoryFunc ¶
The TransportFactoryFunc allows a transport factory function to be used as a TransportFactory.
func (TransportFactoryFunc) CreateTransport ¶
func (fn TransportFactoryFunc) CreateTransport(ctx context.Context, cfg map[string]interface{}) (postdog.Transport, error)
CreateTransport creates a transport from user-provided configuration.
type UnconfiguredTransportError ¶
type UnconfiguredTransportError struct {
Name string
}
UnconfiguredTransportError is returned by Config.Get() if the given transport name hasn't been configured yet.
func (UnconfiguredTransportError) Error ¶
func (err UnconfiguredTransportError) Error() string
type UnregisteredPluginError ¶ added in v0.4.0
type UnregisteredPluginError struct {
Name string
}
UnregisteredPluginError means the autowire configuration uses a plugin that hasn't been registered.
func (UnregisteredPluginError) Error ¶ added in v0.4.0
func (err UnregisteredPluginError) Error() string
type UnregisteredProviderError ¶
type UnregisteredProviderError struct {
Name string
}
UnregisteredProviderError means the autowire configuration uses a provider that hasn't been registered.
func (UnregisteredProviderError) Error ¶
func (err UnregisteredProviderError) Error() string