Skip to content

Commit

Permalink
simplify runtime registry
Browse files Browse the repository at this point in the history
- removed the ability to register the crypto driver along with the engine runtime
- refactored FetchEngineRuntime to FetchRuntime for consistency
  • Loading branch information
sarvalabs-manish committed Sep 25, 2023
1 parent c257b97 commit f1aef0e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 49 deletions.
21 changes: 21 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,24 @@ type EngineRuntime interface {
// ErrorResult that is used by the engine runtime
DecodeErrorResult([]byte) (ErrorResult, error)
}

// registry is an in-memory registry of supported EngineRuntime instances.
// Support for different engine runtimes is only available if they are first registered with this package.
var registry = map[EngineKind]EngineRuntime{}

// RegisterRuntime registers an EngineRuntime with the package.
// If a runtime instance already exists for the EngineKind, it is overwritten.
func RegisterRuntime(runtime EngineRuntime) {
registry[runtime.Kind()] = runtime
}

// FetchRuntime retrieves an EngineRuntime for a given EngineKind.
// If the runtime for the engine kind is not registered, returns false.
func FetchRuntime(kind EngineKind) (EngineRuntime, bool) {
runtime, exists := registry[kind]
if !exists {
return nil, false
}

return runtime, true
}
14 changes: 7 additions & 7 deletions registry_test.go → engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
func TestRegisterEngineRuntime(t *testing.T) {
// register a mock engine runtime
mock := &mockEngineRuntime{kind: PISA}
RegisterRuntime(mock, nil)
RegisterRuntime(mock)

// verify that the engine runtime was registered correctly
r, ok := FetchEngineRuntime(PISA)
r, ok := FetchRuntime(PISA)
require.True(t, ok, "Expected engine runtime to be registered")
require.Equal(t, r.Kind(), PISA, "Expected registered engine runtime to have kind %s, but got %s", PISA, r.Kind())

// overwrite the mock engine runtime with a new one
mock2 := &mockEngineRuntime{kind: PISA}
RegisterRuntime(mock2, nil)
RegisterRuntime(mock2)

// verify that the new engine runtime was registered correctly
r, ok = FetchEngineRuntime(PISA)
r, ok = FetchRuntime(PISA)
require.True(t, ok, "Expected engine runtime to be registered")
require.Equal(t, r.Kind(), PISA, "Expected registered engine runtime to have kind %s, but got %s", PISA, r.Kind())

Expand All @@ -30,15 +30,15 @@ func TestRegisterEngineRuntime(t *testing.T) {

func TestFetchEngineRuntime(t *testing.T) {
// fetch an unregistered engine runtime
_, ok := FetchEngineRuntime(MERU)
_, ok := FetchRuntime(MERU)
require.False(t, ok, "Expected engine runtime not to be registered")

// register a mock engine runtime
mock := &mockEngineRuntime{kind: MERU}
RegisterRuntime(mock, nil)
RegisterRuntime(mock)

// fetch the registered engine runtime
r, ok := FetchEngineRuntime(MERU)
r, ok := FetchRuntime(MERU)
require.True(t, ok, "Expected engine runtime to be registered")
require.Equal(t, r.Kind(), MERU, "Expected registered engine runtime to have kind %s, but got %s", MERU, r.Kind())
}
Expand Down
8 changes: 4 additions & 4 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (header ManifestHeader) validate() error {
return errors.New("unsupported manifest syntax")
}

if _, ok := FetchEngineRuntime(header.LogicEngine()); !ok {
if _, ok := FetchRuntime(header.LogicEngine()); !ok {
return errors.New("unsupported manifest engine: element registry not found")
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (manifest *Manifest) Depolorize(depolorizer *polo.Depolorizer) (err error)
return err
}

runtime, _ := FetchEngineRuntime(manifest.Header().LogicEngine())
runtime, _ := FetchRuntime(manifest.Header().LogicEngine())

manifest.Elements = make([]ManifestElement, 0, len(raw.Elements))

Expand Down Expand Up @@ -264,7 +264,7 @@ func (manifest *Manifest) UnmarshalJSON(data []byte) (err error) {
return err
}

runtime, _ := FetchEngineRuntime(manifest.Header().LogicEngine())
runtime, _ := FetchRuntime(manifest.Header().LogicEngine())

manifest.Elements = make([]ManifestElement, 0, len(raw.Elements))

Expand Down Expand Up @@ -314,7 +314,7 @@ func (manifest *Manifest) UnmarshalYAML(node *yaml.Node) error {
return err
}

runtime, _ := FetchEngineRuntime(manifest.Header().LogicEngine())
runtime, _ := FetchRuntime(manifest.Header().LogicEngine())

manifest.Elements = make([]ManifestElement, 0, len(raw.Elements))

Expand Down
38 changes: 0 additions & 38 deletions registry.go

This file was deleted.

0 comments on commit f1aef0e

Please sign in to comment.