diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c51d2f..abb88cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/registry/doc.go b/registry/doc.go new file mode 100644 index 0000000..fc8d8a2 --- /dev/null +++ b/registry/doc.go @@ -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 diff --git a/registry/registry.go b/registry/registry.go index ac195a1..4b3e7b3 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -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 { @@ -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 { @@ -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 { @@ -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