autowire

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 7 Imported by: 0

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

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 File

func File(path string, opts ...Option) (Config, error)

File reads the configuration from the file at path and returns the autowire config.

func Load

func Load(r io.Reader, opts ...Option) (Config, error)

Load reads the configuration from r and returns the autowire config.

func New

func New(opts ...Option) Config

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) Load

func (cfg *Config) Load(r io.Reader) error

Load loads the YAML autowire configuration from r.

func (*Config) LoadFile

func (cfg *Config) LoadFile(path string) error

LoadFile loads the YAML autowire configuration from the file at the given path.

func (Config) Office

func (cfg Config) Office(ctx context.Context, opts ...postdog.Option) (*postdog.Office, error)

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

type PluginConfig struct {
	Name   string
	Config map[string]interface{}
}

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

type PluginFactoryFunc func(context.Context, map[string]interface{}) (postdog.Plugin, error)

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

type TransportConfig struct {
	Provider string
	Config   map[string]interface{}
}

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

type TransportFactoryFunc func(context.Context, map[string]interface{}) (postdog.Transport, error)

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

Jump to

Keyboard shortcuts

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