Repository: mlange-42/arche
Pull request #318 (2024-01-11 00:20:59)

Replace function `ComponentType` by more comprehensive `ComponentInfo`

coveralls: ## Pull Request Test Coverage Report for [Build 7482461632](https://coveralls.io/builds/65003741)


* **0** of **0**   changed or added relevant lines in **0** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.08%**) to **99.922%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/65003741/badge)](https://coveralls.io/builds/65003741) |
| :-- | --: |
| Change from base [Build 7482409437](https://coveralls.io/builds/65003634): |  -0.08% |
| Covered Lines: | 3856 |
| Relevant Lines: | 3859 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 071d2ba..9efe76b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,8 +15,8 @@ This change was necessary to get the same performance as before, despite the mor
 
 ### Features
 
-* Adds functions `ComponentType(*World, ID) reflect.Type` and `ResourceType(*World, ID) reflect.Type` (#315)
-* Adds methods `World.Ids(Entity) []ID` and `Query.Ids() []ID` (#315)
+* Adds functions `ComponentInfo(*World, ID)` and `ResourceType(*World, ResID)` (#315, #318)
+* Adds methods `World.Ids(Entity)` and `Query.Ids()` (#315)
 
 ### Other
 
diff --git a/ecs/types.go b/ecs/types.go
index 47049f4..cc47923 100644
--- a/ecs/types.go
+++ b/ecs/types.go
@@ -25,3 +25,11 @@ type componentType struct {
 	ID
 	Type reflect.Type
 }
+
+// CompInfo provides information about a registered component.
+// Returned by [ComponentInfo].
+type CompInfo struct {
+	ID         ID
+	Type       reflect.Type
+	IsRelation bool
+}
diff --git a/ecs/world.go b/ecs/world.go
index d320ab8..90f0264 100644
--- a/ecs/world.go
+++ b/ecs/world.go
@@ -25,9 +25,18 @@ func TypeID(w *World, tp reflect.Type) ID {
 	return w.componentID(tp)
 }
 
-// ComponentType returns the reflect.Type for a component [ID], and whether the ID is assigned.
-func ComponentType(w *World, id ID) (reflect.Type, bool) {
-	return w.registry.ComponentType(id)
+// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.
+func ComponentInfo(w *World, id ID) (CompInfo, bool) {
+	tp, ok := w.registry.ComponentType(id)
+	if !ok {
+		return CompInfo{}, false
+	}
+
+	return CompInfo{
+		ID:         id,
+		Type:       tp,
+		IsRelation: w.registry.IsRelation.Get(id),
+	}, true
 }
 
 // ResourceID returns the [ResID] for a resource type via generics.


Test patch:
diff --git a/ecs/world_test.go b/ecs/world_test.go
index a770040..93d84e3 100644
--- a/ecs/world_test.go
+++ b/ecs/world_test.go
@@ -182,22 +182,22 @@ func TestWorldComponents(t *testing.T) {
 	assert.Panics(t, func() { w.Get(newEntityGen(1, 0), posID) })
 }
 
-func TestWorldTypes(t *testing.T) {
+func TestWorldComponentInfo(t *testing.T) {
 	w := NewWorld()
 	_ = ComponentID[Velocity](&w)
 	posID := ComponentID[Position](&w)
 
-	tp, ok := ComponentType(&w, posID)
+	info, ok := ComponentInfo(&w, posID)
 	assert.True(t, ok)
-	assert.Equal(t, tp, reflect.TypeOf(Position{}))
+	assert.Equal(t, info.Type, reflect.TypeOf(Position{}))
 
-	tp, ok = ComponentType(&w, 3)
+	info, ok = ComponentInfo(&w, 3)
 	assert.False(t, ok)
-	assert.Equal(t, tp, nil)
+	assert.Equal(t, info, CompInfo{})
 
 	resID := ResourceID[Velocity](&w)
 
-	tp, ok = ResourceType(&w, resID)
+	tp, ok := ResourceType(&w, resID)
 	assert.True(t, ok)
 	assert.Equal(t, tp, reflect.TypeOf(Velocity{}))