Files
tasks/chasm/registrable_component.go
Yichao YangandGitHub 1cf1881e69 CHASM: Implement Ref method (#7872)
## What changed?
- Properly Implement Ref() method and populate all necessary fields
- Refactor ComponentRef and related methods

## Why?
- CHASM workstream

## How did you test it?
- [x] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [x] added new unit test(s)
- [ ] added new functional test(s)
2025-06-13 13:53:22 -07:00

69 lines
1.6 KiB
Go

package chasm
import (
"reflect"
)
type (
RegistrableComponent struct {
componentType string
library namer
goType reflect.Type
ephemeral bool
singleCluster bool
shardingFn func(EntityKey) string
}
RegistrableComponentOption func(*RegistrableComponent)
)
func NewRegistrableComponent[C Component](
componentType string,
opts ...RegistrableComponentOption,
) *RegistrableComponent {
rc := &RegistrableComponent{
componentType: componentType,
goType: reflect.TypeFor[C](),
shardingFn: defaultShardingFn,
}
for _, opt := range opts {
opt(rc)
}
return rc
}
func WithEphemeral() RegistrableComponentOption {
return func(rc *RegistrableComponent) {
rc.ephemeral = true
}
}
// Is there any use case where we don't want to replicate certain instances of a archetype?
func WithSingleCluster() RegistrableComponentOption {
return func(rc *RegistrableComponent) {
rc.singleCluster = true
}
}
func WithShardingFn(
shardingFn func(EntityKey) string,
) RegistrableComponentOption {
return func(rc *RegistrableComponent) {
if shardingFn != nil {
rc.shardingFn = shardingFn
}
}
}
// fqType returns the fully qualified name of the component, which is a combination of
// the library name and the component type. This is used to uniquely identify
// the component in the registry.
func (rc RegistrableComponent) fqType() string {
if rc.library == nil {
// this should never happen because the component is only accessible from the library.
panic("component is not registered to a library")
}
return fullyQualifiedName(rc.library.Name(), rc.componentType)
}