Skip to content

Commit

Permalink
Add hierarchical module IDs
Browse files Browse the repository at this point in the history
In preparation for submodules in Mir this commit introduces structured
module IDs. They are backwards-compatible with the old unstructured
ones, which are considered to be identifiers of top-level modules.

Signed-off-by: Matej Pavlovic <[email protected]>
  • Loading branch information
matejpavlovic committed Aug 17, 2022
1 parent f89f005 commit 648968d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
31 changes: 31 additions & 0 deletions pkg/types/moduleid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package types

import "strings"

const Separator = "/"

// ModuleID represents an identifier of a module.
// It corresponds to a path in the module hierarchy.
type ModuleID string

// Pb converts a ModuleID to a type used in a Protobuf message.
func (mid ModuleID) Pb() string {
return string(mid)
}

// Top returns the ID of the top-level module of the path, stripped of the IDs of the submodules.
func (mid ModuleID) Top() ModuleID {
top, _, _ := strings.Cut(string(mid), Separator)
return ModuleID(top)
}

// Sub returns the identifier of a submodule within the top-level module, stripped of the top-level module identifier.
func (mid ModuleID) Sub() ModuleID {
_, sub, _ := strings.Cut(string(mid), Separator)
return ModuleID(sub)
}

// Then combines the module ID with a relative path to its submodule in a single module ID.
func (mid ModuleID) Then(submodule ModuleID) ModuleID {
return ModuleID(string(mid) + Separator + string(submodule))
}
11 changes: 0 additions & 11 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,6 @@ func (td TimeDuration) Pb() uint64 {
return uint64(td)
}

// ================================================================================

// ModuleID represents an identifier of a module.
// Modules are stored under their identifiers in modules.Modules
type ModuleID string

// Pb converts a ModuleID to a type used in a Protobuf message.
func (mid ModuleID) Pb() string {
return string(mid)
}

// ================================================================================
// Auxiliary functions
// ================================================================================
Expand Down
2 changes: 1 addition & 1 deletion workitems.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (wi workItems) AddEvents(events *events.EventList) error {
for event := iter.Next(); event != nil; event = iter.Next() {

// Look up the corresponding module's buffer and add the event to it.
if buffer, ok := wi[t.ModuleID(event.DestModule)]; ok {
if buffer, ok := wi[t.ModuleID(event.DestModule).Top()]; ok {
buffer.PushBack(event)
} else {
return fmt.Errorf("no buffer for module %v (adding event of type %T)", event.DestModule, event.Type)
Expand Down

0 comments on commit 648968d

Please sign in to comment.