registry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 2 Imported by: 2

README

Register

register is a package for golang to build your own register

Example

package main

import "github.com/zoumo/golib/registry"

var (
	clouds = registry.New(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 registry to override
	// an already registered interface by name if it is true,
	// otherwise registry will panic.
	OverrideAllowed bool
}

Config is a struct containing all config for registry

type Registry

type Registry interface {
	// Register registers a interface by name.
	// It will panic if name corresponds to an already registered interface
	// and the registry does not allow user to override the interface.
	Register(name string, v interface{}) error

	// Get returns an interface registered with the given name
	Get(name string) (interface{}, bool)

	// Range calls f sequentially for each key and value present in the registry.
	// If f returns false, range stops the iteration.
	Range(func(key string, value interface{}) bool)

	// Keys returns the name of all registered interfaces
	Keys() []string

	// Values returns all registered interfaces
	Values() []interface{}
}

Registry provides a place binding name and interface{}

func New

func New(config *Config) Registry

New returns a new registry

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳