Register registers a new component using the given constructor function and optional definition options. It panics if the component name already exists or if definition creation fails.
(fn ConstructorFunc, opts ...DefinitionOption)
| 77 | // Register registers a new component using the given constructor function and optional definition options. |
| 78 | // It panics if the component name already exists or if definition creation fails. |
| 79 | func Register(fn ConstructorFunc, opts ...DefinitionOption) *Registration { |
| 80 | muComponents.Lock() |
| 81 | defer muComponents.Unlock() |
| 82 | |
| 83 | def, err := MakeDefinition(fn, opts...) |
| 84 | if err != nil { |
| 85 | panic(err) |
| 86 | } |
| 87 | |
| 88 | name := def.Name() |
| 89 | |
| 90 | if _, exists := components[name]; exists { |
| 91 | panic(fmt.Errorf("component with name '%s' already exists", name)) |
| 92 | } |
| 93 | |
| 94 | component := createComponent(def) |
| 95 | |
| 96 | components[name] = component |
| 97 | return &Registration{ |
| 98 | component: component, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Load retrieves and constructs a component by its name, ensuring it matches the expected type T. |
| 103 | // It returns an error if the component is not found, if there is a type mismatch, or if construction fails. |