File: factory.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (35 lines) | stat: -rw-r--r-- 646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package options

import (
	"fmt"
	"strings"
)

type (
	Factory   func() Interface
	Factories map[string]Factory
)

var factories = make(Factories, 10)

func GetFactories() Factories {
	return factories
}

func RegisterFactory(name string, factory Factory) {
	name = strings.ToLower(name)
	factories[name] = factory
}

func GetFactory(name string) Factory {
	name = strings.ToLower(name)
	factory, ok := factories[name]
	if !ok {
		panic(fmt.Errorf("no options factory registered for %s", name))
	}
	return factory
}