• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 13639292298

03 Mar 2025 07:56PM CUT coverage: 98.495%. Remained the same
13639292298

push

github

web-flow
Optimize matching of tables against relations (#115)

1 of 1 new or added line in 1 file covered. (100.0%)

5694 of 5781 relevant lines covered (98.5%)

34789.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/ecs/relation.go
1
package ecs
2

3
import "fmt"
4

5
var relationType = typeOf[Relation]()
6

7
// Relation is a marker for entity relation components.
8
// It must be embedded as first field of a component that represent an entity relation
9
// (see the example).
10
//
11
// Entity relations allow for fast queries using entity relationships.
12
// E.g. to iterate over all entities that are the child of a certain parent entity.
13
// Currently, each entity can only have a single relation component.
14
type Relation struct{}
15

16
// RelationID specifies an entity relation target by component [ID].
17
//
18
// It is used in Ark's unsafe, ID-based API.
19
type RelationID struct {
20
        component ID
21
        target    Entity
22
}
23

24
// RelID creates a new [RelationID].
25
func RelID(id ID, target Entity) RelationID {
41✔
26
        return RelationID{
41✔
27
                component: id,
41✔
28
                target:    target,
41✔
29
        }
41✔
30
}
41✔
31

32
// RelationIndex specifies an entity relation target by component index.
33
//
34
// Note that the index refers to the position of the component in the generics
35
// of e.g. a [Map2] or [Filter2].
36
// This should not be confused with component [ID] as obtained by [ComponentID]!
37
type RelationIndex struct {
38
        index  uint8
39
        target Entity
40
}
41

42
// Rel creates a new [RelationIndex].
43
func Rel(index int, target Entity) RelationIndex {
726✔
44
        return RelationIndex{
726✔
45
                index:  uint8(index),
726✔
46
                target: target,
726✔
47
        }
726✔
48
}
726✔
49

50
// Helper for converting relations
51
type relations []RelationIndex
52

53
func (r relations) toRelations(reg *componentRegistry, ids []ID, base []RelationID, out []RelationID) []RelationID {
2,931✔
54
        out = out[:0]
2,931✔
55
        out = append(out, base...)
2,931✔
56
        for _, rel := range r {
3,656✔
57
                id := ids[rel.index]
725✔
58
                if !reg.IsRelation[id.id] {
725✔
59
                        panic(fmt.Sprintf("component at index %d is not a relation component", rel.index))
×
60
                }
61
                out = append(out, RelationID{
725✔
62
                        component: id,
725✔
63
                        target:    rel.target,
725✔
64
                })
725✔
65
        }
66
        return out
2,931✔
67
}
68

69
// Helper for converting relations
70
type relationEntities []Entity
71

72
func (r relationEntities) toRelation(id ID, out []RelationID) []RelationID {
501,806✔
73
        out = out[:0]
501,806✔
74
        for _, rel := range r {
501,839✔
75
                out = append(out, RelationID{
33✔
76
                        component: id,
33✔
77
                        target:    rel,
33✔
78
                })
33✔
79
        }
33✔
80
        return out
501,806✔
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc