register

package module
v0.0.0-...-6efd257 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2017 License: Apache-2.0 Imports: 3 Imported by: 1

README

Register

register is a package for golang to build your own register

Example

package main

var (
	clouds = NewRegister(nil)
)

type Cloud interface{
	// some interface func
}

type Config struct {
	// some config field
}

type CloudFactory func(Config) (Cloud, error)


func RegisterCloud(name string, factory CloudFactory) {
	clouds.RegisterCloud(cloud, factory)
}

func GetCloud(name string, config Config) (Cloud,error) {
	v, found := clouds.Get(name)
	if !found {
		return nil, nil
	}
	factory := v.(CloudFactory)
	return factory(config)
}

func main() {
	RegisterCloud("aws", AwsCloudFactory)
	RegisterCloud("gce", GceCloudFactory)
	RegisterCloud("azure", AzureCloudFactory)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {

	// OverrideAllowed allows the register to override
	// an already registered interface by name if it is true,
	// otherwise register will panic.
	OverrideAllowed bool
}

Config is a struct containing all config for register

type Register

type Register struct {
	// contains filtered or unexported fields
}

Register is a struct binding name and interface such as Constructor

func NewRegister

func NewRegister(config *Config) *Register

NewRegister returns a new register

func (*Register) Clear

func (r *Register) Clear()

Clear cleans up the registered items

func (*Register) Contains

func (r *Register) Contains(name string) bool

Contains returns true if name corresponds to an already registered interface

func (*Register) Get

func (r *Register) Get(name string) (interface{}, bool)

Get returns an interface registered with the given name

func (*Register) Iter

func (r *Register) Iter() map[string]interface{}

Iter returns an iterable map for <name, interface> pair

func (*Register) Keys

func (r *Register) Keys() []string

Keys returns the name of all registered interfaces

func (*Register) Lock

func (r *Register) Lock()

Lock locks the register. Generally be used before you iterating the register

func (*Register) Register

func (r *Register) Register(name string, v interface{})

Register registers a interface by name. It will panic if name corresponds to an already registered interface and the register does not allow user to override the interface.

func (*Register) Unlock

func (r *Register) Unlock()

Unlock unlocks the register. Generally be used after you iterating the register

func (*Register) Values

func (r *Register) Values() []interface{}

Values returns all registered interfaces

Jump to

Keyboard shortcuts

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