mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
Full rebrand of the SigNoz observability platform: - Go module: github.com/SigNoz/signoz → github.com/hanzoai/o11y - Frontend: @signozhq/* → @hanzo/o11y-*, posthog-js → @hanzo/insights - Docker: signoz/* → ghcr.io/hanzoai/o11y* - ClickHouse → Hanzo Datastore (hanzoai/datastore) - Env vars: SIGNOZ_* → HANZO_* - DB tables: signoz_* → o11y_* - All branding, URLs, docs updated - 2172 files changed
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package factory
|
|
|
|
import (
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
)
|
|
|
|
// Named is implemented by all types of factories.
|
|
type Named interface {
|
|
Name() Name
|
|
}
|
|
|
|
type NamedMap[T Named] struct {
|
|
factories map[Name]T
|
|
factoriesInOrder []T
|
|
}
|
|
|
|
// NewNamedMap creates a new NamedMap from a list of factories.
|
|
// It returns an error if the factories have duplicate names.
|
|
func NewNamedMap[T Named](factories ...T) (NamedMap[T], error) {
|
|
fmap := make(map[Name]T)
|
|
for _, factory := range factories {
|
|
if _, ok := fmap[factory.Name()]; ok {
|
|
return NamedMap[T]{}, errors.NewInvalidInputf(
|
|
errors.CodeInvalidInput,
|
|
"cannot build factory map, duplicate name %q found",
|
|
factory.Name(),
|
|
)
|
|
}
|
|
|
|
fmap[factory.Name()] = factory
|
|
}
|
|
|
|
return NamedMap[T]{factories: fmap, factoriesInOrder: factories}, nil
|
|
}
|
|
|
|
// MustNewNamedMap creates a new NamedMap from a list of factories.
|
|
// It panics if the factories have duplicate names.
|
|
func MustNewNamedMap[T Named](factories ...T) NamedMap[T] {
|
|
nm, err := NewNamedMap(factories...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return nm
|
|
}
|
|
|
|
// Get returns the factory for the given name by string.
|
|
// It returns an error if the factory is not found or the name is invalid.
|
|
func (n *NamedMap[T]) Get(namestr string) (t T, err error) {
|
|
name, err := NewName(namestr)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
factory, ok := n.factories[name]
|
|
if !ok {
|
|
err = errors.NewNotFoundf(
|
|
errors.CodeNotFound,
|
|
"factory %q not found or not registered",
|
|
name,
|
|
)
|
|
return
|
|
}
|
|
|
|
t = factory
|
|
return
|
|
}
|
|
|
|
// Add adds a factory to the NamedMap.
|
|
// It returns an error if the factory already exists.
|
|
func (n *NamedMap[T]) Add(factory T) (err error) {
|
|
name := factory.Name()
|
|
if _, ok := n.factories[name]; ok {
|
|
return errors.Newf(
|
|
errors.TypeAlreadyExists,
|
|
errors.CodeAlreadyExists,
|
|
"factory %q already exists",
|
|
name,
|
|
)
|
|
}
|
|
|
|
n.factories[name] = factory
|
|
n.factoriesInOrder = append(n.factoriesInOrder, factory)
|
|
return nil
|
|
}
|
|
|
|
// GetInOrder returns the factories in the order they were added.
|
|
func (n *NamedMap[T]) GetInOrder() []T {
|
|
return n.factoriesInOrder
|
|
}
|