// DO NOT EDIT: This file was auto-generated by "go generate"
// To regenerate run:
//   go install github.com/coreos/dex/cmd/genconfig
//   go generate <<fully qualified package name>>

package email

import (
	"encoding/json"
	"errors"
	"fmt"
)

type NewEmailerConfigFunc func() EmailerConfig

var (
	emailerTypes map[string]NewEmailerConfigFunc
)

func RegisterEmailerConfigType(emailerType string, fn NewEmailerConfigFunc) {
	if emailerTypes == nil {
		emailerTypes = make(map[string]NewEmailerConfigFunc)
	}

	if _, ok := emailerTypes[emailerType]; ok {
		panic(fmt.Sprintf("emailer config type %q already registered", emailerType))
	}

	emailerTypes[emailerType] = fn
}

func NewEmailerConfigFromType(emailerType string) (EmailerConfig, error) {
	fn, ok := emailerTypes[emailerType]
	if !ok {
		return nil, fmt.Errorf("unrecognized emailer config type %q", emailerType)
	}

	return fn(), nil
}

func newEmailerConfigFromMap(m map[string]interface{}) (EmailerConfig, error) {
	ityp, ok := m["type"]
	if !ok {
		return nil, errors.New("emailer config type not set")
	}
	typ, ok := ityp.(string)
	if !ok {
		return nil, errors.New("emailer config type not string")
	}
	cfg, err := NewEmailerConfigFromType(typ)
	if err != nil {
		return nil, err
	}
	b, err := json.Marshal(m)
	if err != nil {
		return nil, err
	}
	if err = json.Unmarshal(b, cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}
