Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document package registry #73

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [[unpublished]](https://github.com/mlange-42/beecs/compare/v0.2.0...main)

### Documentation

- Document package registry (#73)

## [[v0.2.0]](https://github.com/mlange-42/beecs/compare/v0.1.0...v0.2.0)

### Features
Expand Down
4 changes: 4 additions & 0 deletions registry/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package registry provides a global registry for custom system, resource and observer types.
//
// Registering types allows to retrieve them for model customization, e.g. from JSON.
package registry
6 changes: 6 additions & 0 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var observerRegistry = map[string]reflect.Type{}
var resourcesRegistry = map[string]reflect.Type{}
var systemsRegistry = map[string]reflect.Type{}

// RegisterObserver registers a type as an observer.
func RegisterObserver[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
if _, ok := observerRegistry[tp.String()]; ok {
Expand All @@ -17,6 +18,7 @@ func RegisterObserver[T any]() {
observerRegistry[tp.String()] = tp
}

// RegisterResource registers a type as a resource.
func RegisterResource[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
if _, ok := resourcesRegistry[tp.String()]; ok {
Expand All @@ -25,6 +27,7 @@ func RegisterResource[T any]() {
resourcesRegistry[tp.String()] = tp
}

// RegisterSystem registers a type as a system.
func RegisterSystem[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
if _, ok := systemsRegistry[tp.String()]; ok {
Expand All @@ -33,16 +36,19 @@ func RegisterSystem[T any]() {
systemsRegistry[tp.String()] = tp
}

// GetObserver gets a registered observer type by type name ([reflect.Type.String]).
func GetObserver(name string) (reflect.Type, bool) {
t, ok := observerRegistry[name]
return t, ok
}

// GetResource gets a registered resource type by type name ([reflect.Type.String]).
func GetResource(name string) (reflect.Type, bool) {
t, ok := resourcesRegistry[name]
return t, ok
}

// GetSystem gets a registered system type by type name ([reflect.Type.String]).
func GetSystem(name string) (reflect.Type, bool) {
t, ok := systemsRegistry[name]
return t, ok
Expand Down
Loading